t / helper / test-date.con commit Merge branch 'sg/travis-specific-cc' (51b9966)
   1#include "test-tool.h"
   2#include "cache.h"
   3
   4static const char *usage_msg = "\n"
   5"  test-tool date relative [time_t]...\n"
   6"  test-tool date human [time_t]...\n"
   7"  test-tool date show:<format> [time_t]...\n"
   8"  test-tool date parse [date]...\n"
   9"  test-tool date approxidate [date]...\n"
  10"  test-tool date timestamp [date]...\n"
  11"  test-tool date is64bit\n"
  12"  test-tool date time_t-is64bit\n";
  13
  14static void show_relative_dates(const char **argv, struct timeval *now)
  15{
  16        struct strbuf buf = STRBUF_INIT;
  17
  18        for (; *argv; argv++) {
  19                time_t t = atoi(*argv);
  20                show_date_relative(t, now, &buf);
  21                printf("%s -> %s\n", *argv, buf.buf);
  22        }
  23        strbuf_release(&buf);
  24}
  25
  26static void show_human_dates(const char **argv)
  27{
  28        for (; *argv; argv++) {
  29                time_t t = atoi(*argv);
  30                printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(HUMAN)));
  31        }
  32}
  33
  34static void show_dates(const char **argv, const char *format)
  35{
  36        struct date_mode mode;
  37
  38        parse_date_format(format, &mode);
  39        for (; *argv; argv++) {
  40                char *arg;
  41                timestamp_t t;
  42                int tz;
  43
  44                /*
  45                 * Do not use our normal timestamp parsing here, as the point
  46                 * is to test the formatting code in isolation.
  47                 */
  48                t = parse_timestamp(*argv, &arg, 10);
  49                while (*arg == ' ')
  50                        arg++;
  51                tz = atoi(arg);
  52
  53                printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
  54        }
  55}
  56
  57static void parse_dates(const char **argv, struct timeval *now)
  58{
  59        struct strbuf result = STRBUF_INIT;
  60
  61        for (; *argv; argv++) {
  62                timestamp_t t;
  63                int tz;
  64
  65                strbuf_reset(&result);
  66                parse_date(*argv, &result);
  67                if (sscanf(result.buf, "%"PRItime" %d", &t, &tz) == 2)
  68                        printf("%s -> %s\n",
  69                               *argv, show_date(t, tz, DATE_MODE(ISO8601)));
  70                else
  71                        printf("%s -> bad\n", *argv);
  72        }
  73        strbuf_release(&result);
  74}
  75
  76static void parse_approxidate(const char **argv, struct timeval *now)
  77{
  78        for (; *argv; argv++) {
  79                timestamp_t t;
  80                t = approxidate_relative(*argv, now);
  81                printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
  82        }
  83}
  84
  85static void parse_approx_timestamp(const char **argv, struct timeval *now)
  86{
  87        for (; *argv; argv++) {
  88                timestamp_t t;
  89                t = approxidate_relative(*argv, now);
  90                printf("%s -> %"PRItime"\n", *argv, t);
  91        }
  92}
  93
  94int cmd__date(int argc, const char **argv)
  95{
  96        struct timeval now;
  97        const char *x;
  98
  99        x = getenv("GIT_TEST_DATE_NOW");
 100        if (x) {
 101                now.tv_sec = atoi(x);
 102                now.tv_usec = 0;
 103        }
 104        else
 105                gettimeofday(&now, NULL);
 106
 107        argv++;
 108        if (!*argv)
 109                usage(usage_msg);
 110        if (!strcmp(*argv, "relative"))
 111                show_relative_dates(argv+1, &now);
 112        else if (!strcmp(*argv, "human"))
 113                show_human_dates(argv+1);
 114        else if (skip_prefix(*argv, "show:", &x))
 115                show_dates(argv+1, x);
 116        else if (!strcmp(*argv, "parse"))
 117                parse_dates(argv+1, &now);
 118        else if (!strcmp(*argv, "approxidate"))
 119                parse_approxidate(argv+1, &now);
 120        else if (!strcmp(*argv, "timestamp"))
 121                parse_approx_timestamp(argv+1, &now);
 122        else if (!strcmp(*argv, "is64bit"))
 123                return sizeof(timestamp_t) == 8 ? 0 : 1;
 124        else if (!strcmp(*argv, "time_t-is64bit"))
 125                return sizeof(time_t) == 8 ? 0 : 1;
 126        else
 127                usage(usage_msg);
 128        return 0;
 129}