4 static const char *usage_msg
= "\n"
5 " test-tool date relative [time_t]...\n"
6 " test-tool date show:<format> [time_t]...\n"
7 " test-tool date parse [date]...\n"
8 " test-tool date approxidate [date]...\n"
9 " test-tool date timestamp [date]...\n"
10 " test-tool date is64bit\n"
11 " test-tool date time_t-is64bit\n";
13 static void show_relative_dates(const char **argv
, struct timeval
*now
)
15 struct strbuf buf
= STRBUF_INIT
;
17 for (; *argv
; argv
++) {
18 time_t t
= atoi(*argv
);
19 show_date_relative(t
, 0, now
, &buf
);
20 printf("%s -> %s\n", *argv
, buf
.buf
);
25 static void show_dates(const char **argv
, const char *format
)
27 struct date_mode mode
;
29 parse_date_format(format
, &mode
);
30 for (; *argv
; argv
++) {
36 * Do not use our normal timestamp parsing here, as the point
37 * is to test the formatting code in isolation.
39 t
= parse_timestamp(*argv
, &arg
, 10);
44 printf("%s -> %s\n", *argv
, show_date(t
, tz
, &mode
));
48 static void parse_dates(const char **argv
, struct timeval
*now
)
50 struct strbuf result
= STRBUF_INIT
;
52 for (; *argv
; argv
++) {
56 strbuf_reset(&result
);
57 parse_date(*argv
, &result
);
58 if (sscanf(result
.buf
, "%"PRItime
" %d", &t
, &tz
) == 2)
60 *argv
, show_date(t
, tz
, DATE_MODE(ISO8601
)));
62 printf("%s -> bad\n", *argv
);
64 strbuf_release(&result
);
67 static void parse_approxidate(const char **argv
, struct timeval
*now
)
69 for (; *argv
; argv
++) {
71 t
= approxidate_relative(*argv
, now
);
72 printf("%s -> %s\n", *argv
, show_date(t
, 0, DATE_MODE(ISO8601
)));
76 static void parse_approx_timestamp(const char **argv
, struct timeval
*now
)
78 for (; *argv
; argv
++) {
80 t
= approxidate_relative(*argv
, now
);
81 printf("%s -> %"PRItime
"\n", *argv
, t
);
85 int cmd__date(int argc
, const char **argv
)
90 x
= getenv("TEST_DATE_NOW");
96 gettimeofday(&now
, NULL
);
101 if (!strcmp(*argv
, "relative"))
102 show_relative_dates(argv
+1, &now
);
103 else if (skip_prefix(*argv
, "show:", &x
))
104 show_dates(argv
+1, x
);
105 else if (!strcmp(*argv
, "parse"))
106 parse_dates(argv
+1, &now
);
107 else if (!strcmp(*argv
, "approxidate"))
108 parse_approxidate(argv
+1, &now
);
109 else if (!strcmp(*argv
, "timestamp"))
110 parse_approx_timestamp(argv
+1, &now
);
111 else if (!strcmp(*argv
, "is64bit"))
112 return sizeof(timestamp_t
) == 8 ? 0 : 1;
113 else if (!strcmp(*argv
, "time_t-is64bit"))
114 return sizeof(time_t) == 8 ? 0 : 1;