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