Merge branch 'jc/unpack-trees-plug-leak'
[git/git-svn.git] / test-date.c
blob94a6997a8f6aa40cd86859dbd3eb27704e46b034
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 struct strbuf result = STRBUF_INIT;
24 for (; *argv; argv++) {
25 unsigned long t;
26 int tz;
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);
36 strbuf_release(&result);
39 static void parse_approxidate(char **argv, struct timeval *now)
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));
48 int main(int argc, char **argv)
50 struct timeval now;
51 const char *x;
53 x = getenv("TEST_DATE_NOW");
54 if (x) {
55 now.tv_sec = atoi(x);
56 now.tv_usec = 0;
58 else
59 gettimeofday(&now, NULL);
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;