Add `human` format to test-tool
[git/git.git] / t / helper / test-date.c
CommitLineData
a801a7cf 1#include "test-tool.h"
89967023
LT
2#include "cache.h"
3
4f6552ea 4static const char *usage_msg = "\n"
a801a7cf 5" test-tool date relative [time_t]...\n"
b841d4ff 6" test-tool date human [time_t]...\n"
a801a7cf
NTND
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 is64bit\n"
12" test-tool date time_t-is64bit\n";
4f6552ea 13
d4c6375f 14static void show_relative_dates(const char **argv, struct timeval *now)
89967023 15{
7d29afd4 16 struct strbuf buf = STRBUF_INIT;
4f6552ea
JK
17
18 for (; *argv; argv++) {
19 time_t t = atoi(*argv);
7d29afd4
JN
20 show_date_relative(t, 0, now, &buf);
21 printf("%s -> %s\n", *argv, buf.buf);
4f6552ea 22 }
7d29afd4 23 strbuf_release(&buf);
4f6552ea 24}
89967023 25
b841d4ff
SS
26static void show_human_dates(const char **argv)
27{
28 for (; *argv; argv++) {
29 time_t t = atoi(*argv);
30 printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(HUMAN)));
31 }
32}
33
d4c6375f 34static void show_dates(const char **argv, const char *format)
36d67921
JK
35{
36 struct date_mode mode;
37
38 parse_date_format(format, &mode);
39 for (; *argv; argv++) {
d4c6375f 40 char *arg;
dddbad72 41 timestamp_t t;
36d67921
JK
42 int tz;
43
44 /*
45 * Do not use our normal timestamp parsing here, as the point
46 * is to test the formatting code in isolation.
47 */
1aeb7e75 48 t = parse_timestamp(*argv, &arg, 10);
36d67921
JK
49 while (*arg == ' ')
50 arg++;
51 tz = atoi(arg);
52
53 printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
54 }
55}
56
3f2e2297 57static void parse_dates(const char **argv, struct timeval *now)
4f6552ea 58{
c33ddc2e
JK
59 struct strbuf result = STRBUF_INIT;
60
4f6552ea 61 for (; *argv; argv++) {
dddbad72 62 timestamp_t t;
6b097788 63 int tz;
89967023 64
c33ddc2e
JK
65 strbuf_reset(&result);
66 parse_date(*argv, &result);
cb71f8bd 67 if (sscanf(result.buf, "%"PRItime" %d", &t, &tz) == 2)
6b097788 68 printf("%s -> %s\n",
a5481a6c 69 *argv, show_date(t, tz, DATE_MODE(ISO8601)));
6b097788
JK
70 else
71 printf("%s -> bad\n", *argv);
4f6552ea 72 }
c33ddc2e 73 strbuf_release(&result);
4f6552ea 74}
f30c95dd 75
3f2e2297 76static void parse_approxidate(const char **argv, struct timeval *now)
4f6552ea
JK
77{
78 for (; *argv; argv++) {
dddbad72 79 timestamp_t t;
4f6552ea 80 t = approxidate_relative(*argv, now);
a5481a6c 81 printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
4f6552ea
JK
82 }
83}
84
5f967424
HM
85static void parse_approx_timestamp(const char **argv, struct timeval *now)
86{
87 for (; *argv; argv++) {
88 timestamp_t t;
89 t = approxidate_relative(*argv, now);
90 printf("%s -> %"PRItime"\n", *argv, t);
91 }
92}
93
a801a7cf 94int cmd__date(int argc, const char **argv)
4f6552ea
JK
95{
96 struct timeval now;
97 const char *x;
98
b841d4ff 99 x = getenv("GIT_TEST_DATE_NOW");
4f6552ea
JK
100 if (x) {
101 now.tv_sec = atoi(x);
102 now.tv_usec = 0;
89967023 103 }
4f6552ea
JK
104 else
105 gettimeofday(&now, NULL);
106
107 argv++;
108 if (!*argv)
109 usage(usage_msg);
fdba2cde
JK
110 if (!strcmp(*argv, "relative"))
111 show_relative_dates(argv+1, &now);
b841d4ff
SS
112 else if (!strcmp(*argv, "human"))
113 show_human_dates(argv+1);
36d67921
JK
114 else if (skip_prefix(*argv, "show:", &x))
115 show_dates(argv+1, x);
4f6552ea
JK
116 else if (!strcmp(*argv, "parse"))
117 parse_dates(argv+1, &now);
118 else if (!strcmp(*argv, "approxidate"))
119 parse_approxidate(argv+1, &now);
5f967424
HM
120 else if (!strcmp(*argv, "timestamp"))
121 parse_approx_timestamp(argv+1, &now);
a07fb050 122 else if (!strcmp(*argv, "is64bit"))
dddbad72 123 return sizeof(timestamp_t) == 8 ? 0 : 1;
efac8ac8
JS
124 else if (!strcmp(*argv, "time_t-is64bit"))
125 return sizeof(time_t) == 8 ? 0 : 1;
4f6552ea
JK
126 else
127 usage(usage_msg);
89967023
LT
128 return 0;
129}