Merge branch 'sh/rebase-i-reread-todo-after-exec'
[alt-git.git] / t / helper / test-date.c
blob506054bcd5dfbd76c8aec85382f35794514b9db9
1 #include "cache.h"
3 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";
9 static void show_relative_dates(const char **argv, struct timeval *now)
11 struct strbuf buf = STRBUF_INIT;
13 for (; *argv; argv++) {
14 time_t t = atoi(*argv);
15 show_date_relative(t, 0, now, &buf);
16 printf("%s -> %s\n", *argv, buf.buf);
18 strbuf_release(&buf);
21 static void show_dates(const char **argv, const char *format)
23 struct date_mode mode;
25 parse_date_format(format, &mode);
26 for (; *argv; argv++) {
27 char *arg;
28 time_t t;
29 int tz;
32 * Do not use our normal timestamp parsing here, as the point
33 * is to test the formatting code in isolation.
35 t = strtol(*argv, &arg, 10);
36 while (*arg == ' ')
37 arg++;
38 tz = atoi(arg);
40 printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
44 static void parse_dates(const char **argv, struct timeval *now)
46 struct strbuf result = STRBUF_INIT;
48 for (; *argv; argv++) {
49 unsigned long t;
50 int tz;
52 strbuf_reset(&result);
53 parse_date(*argv, &result);
54 if (sscanf(result.buf, "%lu %d", &t, &tz) == 2)
55 printf("%s -> %s\n",
56 *argv, show_date(t, tz, DATE_MODE(ISO8601)));
57 else
58 printf("%s -> bad\n", *argv);
60 strbuf_release(&result);
63 static void parse_approxidate(const char **argv, struct timeval *now)
65 for (; *argv; argv++) {
66 time_t t;
67 t = approxidate_relative(*argv, now);
68 printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
72 int cmd_main(int argc, const char **argv)
74 struct timeval now;
75 const char *x;
77 x = getenv("TEST_DATE_NOW");
78 if (x) {
79 now.tv_sec = atoi(x);
80 now.tv_usec = 0;
82 else
83 gettimeofday(&now, NULL);
85 argv++;
86 if (!*argv)
87 usage(usage_msg);
88 if (!strcmp(*argv, "relative"))
89 show_relative_dates(argv+1, &now);
90 else if (skip_prefix(*argv, "show:", &x))
91 show_dates(argv+1, x);
92 else if (!strcmp(*argv, "parse"))
93 parse_dates(argv+1, &now);
94 else if (!strcmp(*argv, "approxidate"))
95 parse_approxidate(argv+1, &now);
96 else
97 usage(usage_msg);
98 return 0;