4 static 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";
15 static void show_relative_dates(const char **argv, struct timeval *now)
17 struct strbuf buf = STRBUF_INIT;
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);
27 static void show_human_dates(const char **argv)
29 for (; *argv; argv++) {
30 time_t t = atoi(*argv);
31 printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(HUMAN)));
35 static void show_dates(const char **argv, const char *format)
37 struct date_mode mode;
39 parse_date_format(format, &mode);
40 for (; *argv; argv++) {
46 * Do not use our normal timestamp parsing here, as the point
47 * is to test the formatting code in isolation.
49 t = parse_timestamp(*argv, &arg, 10);
54 printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
58 static void parse_dates(const char **argv, struct timeval *now)
60 struct strbuf result = STRBUF_INIT;
62 for (; *argv; argv++) {
66 strbuf_reset(&result);
67 parse_date(*argv, &result);
68 if (sscanf(result.buf, "%"PRItime" %d", &t, &tz) == 2)
70 *argv, show_date(t, tz, DATE_MODE(ISO8601)));
72 printf("%s -> bad\n", *argv);
74 strbuf_release(&result);
77 static void parse_approxidate(const char **argv, struct timeval *now)
79 for (; *argv; argv++) {
81 t = approxidate_relative(*argv, now);
82 printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
86 static void parse_approx_timestamp(const char **argv, struct timeval *now)
88 for (; *argv; argv++) {
90 t = approxidate_relative(*argv, now);
91 printf("%s -> %"PRItime"\n", *argv, t);
95 static void getnanos(const char **argv)
97 double seconds = getnanotime() / 1.0e9;
100 seconds -= strtod(*argv, NULL);
101 printf("%lf\n", seconds);
104 int cmd__date(int argc, const char **argv)
109 x = getenv("GIT_TEST_DATE_NOW");
111 now.tv_sec = atoi(x);
115 gettimeofday(&now, NULL);
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, &now);
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"))
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;