5 static const char *usage_msg
= "\n"
6 " test-tool date relative [time_t]...\n"
7 " test-tool date human [time_t]...\n"
8 " test-tool date show:<format> [time_t]...\n"
9 " test-tool date parse [date]...\n"
10 " test-tool date approxidate [date]...\n"
11 " test-tool date timestamp [date]...\n"
12 " test-tool date getnanos [start-nanos]\n"
13 " test-tool date is64bit\n"
14 " test-tool date time_t-is64bit\n";
16 static void show_relative_dates(const char **argv
)
18 struct strbuf buf
= STRBUF_INIT
;
20 for (; *argv
; argv
++) {
21 time_t t
= atoi(*argv
);
22 show_date_relative(t
, &buf
);
23 printf("%s -> %s\n", *argv
, buf
.buf
);
28 static void show_human_dates(const char **argv
)
30 for (; *argv
; argv
++) {
31 time_t t
= atoi(*argv
);
32 printf("%s -> %s\n", *argv
, show_date(t
, 0, DATE_MODE(HUMAN
)));
36 static void show_dates(const char **argv
, const char *format
)
38 struct date_mode mode
= DATE_MODE_INIT
;
40 parse_date_format(format
, &mode
);
41 for (; *argv
; argv
++) {
47 * Do not use our normal timestamp parsing here, as the point
48 * is to test the formatting code in isolation.
50 t
= parse_timestamp(*argv
, &arg
, 10);
55 printf("%s -> %s\n", *argv
, show_date(t
, tz
, &mode
));
58 date_mode_release(&mode
);
61 static void parse_dates(const char **argv
)
63 struct strbuf result
= STRBUF_INIT
;
65 for (; *argv
; argv
++) {
69 strbuf_reset(&result
);
70 parse_date(*argv
, &result
);
71 if (sscanf(result
.buf
, "%"PRItime
" %d", &t
, &tz
) == 2)
73 *argv
, show_date(t
, tz
, DATE_MODE(ISO8601
)));
75 printf("%s -> bad\n", *argv
);
77 strbuf_release(&result
);
80 static void parse_approxidate(const char **argv
)
82 for (; *argv
; argv
++) {
84 t
= approxidate_relative(*argv
);
85 printf("%s -> %s\n", *argv
, show_date(t
, 0, DATE_MODE(ISO8601
)));
89 static void parse_approx_timestamp(const char **argv
)
91 for (; *argv
; argv
++) {
93 t
= approxidate_relative(*argv
);
94 printf("%s -> %"PRItime
"\n", *argv
, t
);
98 static void getnanos(const char **argv
)
100 double seconds
= getnanotime() / 1.0e9
;
103 seconds
-= strtod(*argv
, NULL
);
104 printf("%lf\n", seconds
);
107 int cmd__date(int argc
, const char **argv
)
114 if (!strcmp(*argv
, "relative"))
115 show_relative_dates(argv
+1);
116 else if (!strcmp(*argv
, "human"))
117 show_human_dates(argv
+1);
118 else if (skip_prefix(*argv
, "show:", &x
))
119 show_dates(argv
+1, x
);
120 else if (!strcmp(*argv
, "parse"))
122 else if (!strcmp(*argv
, "approxidate"))
123 parse_approxidate(argv
+1);
124 else if (!strcmp(*argv
, "timestamp"))
125 parse_approx_timestamp(argv
+1);
126 else if (!strcmp(*argv
, "getnanos"))
128 else if (!strcmp(*argv
, "is64bit"))
129 return sizeof(timestamp_t
) == 8 ? 0 : 1;
130 else if (!strcmp(*argv
, "time_t-is64bit"))
131 return sizeof(time_t) == 8 ? 0 : 1;