Merge branch 'jc/maint-1.6.6-pathspec-stdin-and-cmdline'
[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         char buf[128];
11
12         for (; *argv; argv++) {
13                 time_t t = atoi(*argv);
14                 show_date_relative(t, 0, now, buf, sizeof(buf));
15                 printf("%s -> %s\n", *argv, buf);
16         }
17 }
18
19 static void parse_dates(char **argv, struct timeval *now)
20 {
21         for (; *argv; argv++) {
22                 char result[100];
23                 unsigned long t;
24                 int tz;
25
26                 result[0] = 0;
27                 parse_date(*argv, result, sizeof(result));
28                 if (sscanf(result, "%lu %d", &t, &tz) == 2)
29                         printf("%s -> %s\n",
30                                *argv, show_date(t, tz, DATE_ISO8601));
31                 else
32                         printf("%s -> bad\n", *argv);
33         }
34 }
35
36 static void parse_approxidate(char **argv, struct timeval *now)
37 {
38         for (; *argv; argv++) {
39                 time_t t;
40                 t = approxidate_relative(*argv, now);
41                 printf("%s -> %s\n", *argv, show_date(t, 0, DATE_ISO8601));
42         }
43 }
44
45 int main(int argc, char **argv)
46 {
47         struct timeval now;
48         const char *x;
49
50         x = getenv("TEST_DATE_NOW");
51         if (x) {
52                 now.tv_sec = atoi(x);
53                 now.tv_usec = 0;
54         }
55         else
56                 gettimeofday(&now, NULL);
57
58         argv++;
59         if (!*argv)
60                 usage(usage_msg);
61         if (!strcmp(*argv, "show"))
62                 show_dates(argv+1, &now);
63         else if (!strcmp(*argv, "parse"))
64                 parse_dates(argv+1, &now);
65         else if (!strcmp(*argv, "approxidate"))
66                 parse_approxidate(argv+1, &now);
67         else
68                 usage(usage_msg);
69         return 0;
70 }