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