Merge branch 'jn/pager-lv-default-env'
[alt-git.git] / test-date.c
blob10afaabbfaed9ee1b4a6a498a2110fb904d48645
1 #include "cache.h"
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";
8 static void show_dates(char **argv, struct timeval *now)
10 struct strbuf buf = STRBUF_INIT;
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);
17 strbuf_release(&buf);
20 static void parse_dates(char **argv, struct timeval *now)
22 for (; *argv; argv++) {
23 char result[100];
24 unsigned long t;
25 int tz;
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);
37 static void parse_approxidate(char **argv, struct timeval *now)
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));
46 int main(int argc, char **argv)
48 struct timeval now;
49 const char *x;
51 x = getenv("TEST_DATE_NOW");
52 if (x) {
53 now.tv_sec = atoi(x);
54 now.tv_usec = 0;
56 else
57 gettimeofday(&now, NULL);
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;