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