config.mak.dev: add -Wformat-security
[git.git] / t / helper / test-date.c
bloba0837371aba17956331ddcdb04847c6c9edeefd2
1 #include "test-tool.h"
2 #include "cache.h"
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);
22 strbuf_release(&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++) {
31 char *arg;
32 timestamp_t t;
33 int tz;
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);
40 while (*arg == ' ')
41 arg++;
42 tz = atoi(arg);
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++) {
53 timestamp_t t;
54 int tz;
56 strbuf_reset(&result);
57 parse_date(*argv, &result);
58 if (sscanf(result.buf, "%"PRItime" %d", &t, &tz) == 2)
59 printf("%s -> %s\n",
60 *argv, show_date(t, tz, DATE_MODE(ISO8601)));
61 else
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++) {
70 timestamp_t t;
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++) {
79 timestamp_t t;
80 t = approxidate_relative(*argv, now);
81 printf("%s -> %"PRItime"\n", *argv, t);
85 int cmd__date(int argc, const char **argv)
87 struct timeval now;
88 const char *x;
90 x = getenv("TEST_DATE_NOW");
91 if (x) {
92 now.tv_sec = atoi(x);
93 now.tv_usec = 0;
95 else
96 gettimeofday(&now, NULL);
98 argv++;
99 if (!*argv)
100 usage(usage_msg);
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;
115 else
116 usage(usage_msg);
117 return 0;