Merge branch 'jt/fast-export-copy-modify-fix' into maint
[git/gitweb.git] / t / helper / test-date.c
blobf414a3ac670fb2d77645f5d7301b6aa8f603379e
1 #include "cache.h"
3 static const char *usage_msg = "\n"
4 " test-date relative [time_t]...\n"
5 " test-date show:<format> [time_t]...\n"
6 " test-date parse [date]...\n"
7 " test-date approxidate [date]...\n"
8 " test-date is64bit\n"
9 " test-date time_t-is64bit\n";
11 static void show_relative_dates(const char **argv, struct timeval *now)
13 struct strbuf buf = STRBUF_INIT;
15 for (; *argv; argv++) {
16 time_t t = atoi(*argv);
17 show_date_relative(t, 0, now, &buf);
18 printf("%s -> %s\n", *argv, buf.buf);
20 strbuf_release(&buf);
23 static void show_dates(const char **argv, const char *format)
25 struct date_mode mode;
27 parse_date_format(format, &mode);
28 for (; *argv; argv++) {
29 char *arg;
30 timestamp_t t;
31 int tz;
34 * Do not use our normal timestamp parsing here, as the point
35 * is to test the formatting code in isolation.
37 t = parse_timestamp(*argv, &arg, 10);
38 while (*arg == ' ')
39 arg++;
40 tz = atoi(arg);
42 printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
46 static void parse_dates(const char **argv, struct timeval *now)
48 struct strbuf result = STRBUF_INIT;
50 for (; *argv; argv++) {
51 timestamp_t t;
52 int tz;
54 strbuf_reset(&result);
55 parse_date(*argv, &result);
56 if (sscanf(result.buf, "%"PRItime" %d", &t, &tz) == 2)
57 printf("%s -> %s\n",
58 *argv, show_date(t, tz, DATE_MODE(ISO8601)));
59 else
60 printf("%s -> bad\n", *argv);
62 strbuf_release(&result);
65 static void parse_approxidate(const char **argv, struct timeval *now)
67 for (; *argv; argv++) {
68 timestamp_t t;
69 t = approxidate_relative(*argv, now);
70 printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
74 int cmd_main(int argc, const char **argv)
76 struct timeval now;
77 const char *x;
79 x = getenv("TEST_DATE_NOW");
80 if (x) {
81 now.tv_sec = atoi(x);
82 now.tv_usec = 0;
84 else
85 gettimeofday(&now, NULL);
87 argv++;
88 if (!*argv)
89 usage(usage_msg);
90 if (!strcmp(*argv, "relative"))
91 show_relative_dates(argv+1, &now);
92 else if (skip_prefix(*argv, "show:", &x))
93 show_dates(argv+1, x);
94 else if (!strcmp(*argv, "parse"))
95 parse_dates(argv+1, &now);
96 else if (!strcmp(*argv, "approxidate"))
97 parse_approxidate(argv+1, &now);
98 else if (!strcmp(*argv, "is64bit"))
99 return sizeof(timestamp_t) == 8 ? 0 : 1;
100 else if (!strcmp(*argv, "time_t-is64bit"))
101 return sizeof(time_t) == 8 ? 0 : 1;
102 else
103 usage(usage_msg);
104 return 0;