Merge branch 'jl/commit-v-strip-marker' into maint
[git] / test-date.c
1 #include "cache.h"
2
3 static const char *usage_msg = "\n"
4 "  test-date show [time_t]...\n"
5 "  test-date parse [date]...\n"
6 "  test-date approxidate [date]...\n";
7
8 static void show_dates(char **argv, struct timeval *now)
9 {
10         struct strbuf buf = STRBUF_INIT;
11
12         for (; *argv; argv++) {
13                 time_t t = atoi(*argv);
14                 show_date_relative(t, 0, now, &buf);
15                 printf("%s -> %s\n", *argv, buf.buf);
16         }
17         strbuf_release(&buf);
18 }
19
20 static void parse_dates(char **argv, struct timeval *now)
21 {
22         for (; *argv; argv++) {
23                 char result[100];
24                 unsigned long t;
25                 int tz;
26
27                 result[0] = 0;
28                 parse_date(*argv, result, sizeof(result));
29                 if (sscanf(result, "%lu %d", &t, &tz) == 2)
30                         printf("%s -> %s\n",
31                                *argv, show_date(t, tz, DATE_ISO8601));
32                 else
33                         printf("%s -> bad\n", *argv);
34         }
35 }
36
37 static void parse_approxidate(char **argv, struct timeval *now)
38 {
39         for (; *argv; argv++) {
40                 time_t t;
41                 t = approxidate_relative(*argv, now);
42                 printf("%s -> %s\n", *argv, show_date(t, 0, DATE_ISO8601));
43         }
44 }
45
46 int main(int argc, char **argv)
47 {
48         struct timeval now;
49         const char *x;
50
51         x = getenv("TEST_DATE_NOW");
52         if (x) {
53                 now.tv_sec = atoi(x);
54                 now.tv_usec = 0;
55         }
56         else
57                 gettimeofday(&now, NULL);
58
59         argv++;
60         if (!*argv)
61                 usage(usage_msg);
62         if (!strcmp(*argv, "show"))
63                 show_dates(argv+1, &now);
64         else if (!strcmp(*argv, "parse"))
65                 parse_dates(argv+1, &now);
66         else if (!strcmp(*argv, "approxidate"))
67                 parse_approxidate(argv+1, &now);
68         else
69                 usage(usage_msg);
70         return 0;
71 }