1#include "cache.h"
23
static 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";
89
static void show_relative_dates(const char **argv, struct timeval *now)
10{
11struct strbuf buf = STRBUF_INIT;
1213
for (; *argv; argv++) {
14time_t t = atoi(*argv);
15show_date_relative(t, 0, now, &buf);
16printf("%s -> %s\n", *argv, buf.buf);
17}
18strbuf_release(&buf);
19}
2021
static void show_dates(const char **argv, const char *format)
22{
23struct date_mode mode;
2425
parse_date_format(format, &mode);
26for (; *argv; argv++) {
27char *arg;
28time_t t;
29int tz;
3031
/*
32* Do not use our normal timestamp parsing here, as the point
33* is to test the formatting code in isolation.
34*/
35t = strtol(*argv, &arg, 10);
36while (*arg == ' ')
37arg++;
38tz = atoi(arg);
3940
printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
41}
42}
4344
static void parse_dates(const char **argv, struct timeval *now)
45{
46struct strbuf result = STRBUF_INIT;
4748
for (; *argv; argv++) {
49unsigned long t;
50int tz;
5152
strbuf_reset(&result);
53parse_date(*argv, &result);
54if (sscanf(result.buf, "%lu %d", &t, &tz) == 2)
55printf("%s -> %s\n",
56*argv, show_date(t, tz, DATE_MODE(ISO8601)));
57else
58printf("%s -> bad\n", *argv);
59}
60strbuf_release(&result);
61}
6263
static void parse_approxidate(const char **argv, struct timeval *now)
64{
65for (; *argv; argv++) {
66time_t t;
67t = approxidate_relative(*argv, now);
68printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
69}
70}
7172
int cmd_main(int argc, const char **argv)
73{
74struct timeval now;
75const char *x;
7677
x = getenv("TEST_DATE_NOW");
78if (x) {
79now.tv_sec = atoi(x);
80now.tv_usec = 0;
81}
82else
83gettimeofday(&now, NULL);
8485
argv++;
86if (!*argv)
87usage(usage_msg);
88if (!strcmp(*argv, "relative"))
89show_relative_dates(argv+1, &now);
90else if (skip_prefix(*argv, "show:", &x))
91show_dates(argv+1, x);
92else if (!strcmp(*argv, "parse"))
93parse_dates(argv+1, &now);
94else if (!strcmp(*argv, "approxidate"))
95parse_approxidate(argv+1, &now);
96else
97usage(usage_msg);
98return 0;
99}