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