Merge branch 'rj/no-xopen-source-for-cygwin' 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         struct strbuf result = STRBUF_INIT;
23
24         for (; *argv; argv++) {
25                 unsigned long t;
26                 int tz;
27
28                 strbuf_reset(&result);
29                 parse_date(*argv, &result);
30                 if (sscanf(result.buf, "%lu %d", &t, &tz) == 2)
31                         printf("%s -> %s\n",
32                                *argv, show_date(t, tz, DATE_ISO8601));
33                 else
34                         printf("%s -> bad\n", *argv);
35         }
36         strbuf_release(&result);
37 }
38
39 static void parse_approxidate(char **argv, struct timeval *now)
40 {
41         for (; *argv; argv++) {
42                 time_t t;
43                 t = approxidate_relative(*argv, now);
44                 printf("%s -> %s\n", *argv, show_date(t, 0, DATE_ISO8601));
45         }
46 }
47
48 int main(int argc, char **argv)
49 {
50         struct timeval now;
51         const char *x;
52
53         x = getenv("TEST_DATE_NOW");
54         if (x) {
55                 now.tv_sec = atoi(x);
56                 now.tv_usec = 0;
57         }
58         else
59                 gettimeofday(&now, NULL);
60
61         argv++;
62         if (!*argv)
63                 usage(usage_msg);
64         if (!strcmp(*argv, "show"))
65                 show_dates(argv+1, &now);
66         else if (!strcmp(*argv, "parse"))
67                 parse_dates(argv+1, &now);
68         else if (!strcmp(*argv, "approxidate"))
69                 parse_approxidate(argv+1, &now);
70         else
71                 usage(usage_msg);
72         return 0;
73 }