t / helper / test-date.con commit sha1dc: update from upstream (9936c1b)
   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
   9static void show_relative_dates(const char **argv, struct timeval *now)
  10{
  11        struct strbuf buf = STRBUF_INIT;
  12
  13        for (; *argv; argv++) {
  14                time_t t = atoi(*argv);
  15                show_date_relative(t, 0, now, &buf);
  16                printf("%s -> %s\n", *argv, buf.buf);
  17        }
  18        strbuf_release(&buf);
  19}
  20
  21static void show_dates(const char **argv, const char *format)
  22{
  23        struct date_mode mode;
  24
  25        parse_date_format(format, &mode);
  26        for (; *argv; argv++) {
  27                char *arg;
  28                time_t t;
  29                int tz;
  30
  31                /*
  32                 * Do not use our normal timestamp parsing here, as the point
  33                 * is to test the formatting code in isolation.
  34                 */
  35                t = strtol(*argv, &arg, 10);
  36                while (*arg == ' ')
  37                        arg++;
  38                tz = atoi(arg);
  39
  40                printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
  41        }
  42}
  43
  44static void parse_dates(const char **argv, struct timeval *now)
  45{
  46        struct strbuf result = STRBUF_INIT;
  47
  48        for (; *argv; argv++) {
  49                unsigned long t;
  50                int tz;
  51
  52                strbuf_reset(&result);
  53                parse_date(*argv, &result);
  54                if (sscanf(result.buf, "%lu %d", &t, &tz) == 2)
  55                        printf("%s -> %s\n",
  56                               *argv, show_date(t, tz, DATE_MODE(ISO8601)));
  57                else
  58                        printf("%s -> bad\n", *argv);
  59        }
  60        strbuf_release(&result);
  61}
  62
  63static void parse_approxidate(const char **argv, struct timeval *now)
  64{
  65        for (; *argv; argv++) {
  66                time_t t;
  67                t = approxidate_relative(*argv, now);
  68                printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
  69        }
  70}
  71
  72int cmd_main(int argc, const char **argv)
  73{
  74        struct timeval now;
  75        const char *x;
  76
  77        x = getenv("TEST_DATE_NOW");
  78        if (x) {
  79                now.tv_sec = atoi(x);
  80                now.tv_usec = 0;
  81        }
  82        else
  83                gettimeofday(&now, NULL);
  84
  85        argv++;
  86        if (!*argv)
  87                usage(usage_msg);
  88        if (!strcmp(*argv, "relative"))
  89                show_relative_dates(argv+1, &now);
  90        else if (skip_prefix(*argv, "show:", &x))
  91                show_dates(argv+1, x);
  92        else if (!strcmp(*argv, "parse"))
  93                parse_dates(argv+1, &now);
  94        else if (!strcmp(*argv, "approxidate"))
  95                parse_approxidate(argv+1, &now);
  96        else
  97                usage(usage_msg);
  98        return 0;
  99}