git: add `--super-prefix` to usage string
[alt-git.git] / t / helper / test-date.c
blob099eff4f0fc880689d06c0b8b6301c3e83dff747
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 human [time_t]...\n"
7 " test-tool date show:<format> [time_t]...\n"
8 " test-tool date parse [date]...\n"
9 " test-tool date approxidate [date]...\n"
10 " test-tool date timestamp [date]...\n"
11 " test-tool date getnanos [start-nanos]\n"
12 " test-tool date is64bit\n"
13 " test-tool date time_t-is64bit\n";
15 static void show_relative_dates(const char **argv)
17 struct strbuf buf = STRBUF_INIT;
19 for (; *argv; argv++) {
20 time_t t = atoi(*argv);
21 show_date_relative(t, &buf);
22 printf("%s -> %s\n", *argv, buf.buf);
24 strbuf_release(&buf);
27 static void show_human_dates(const char **argv)
29 for (; *argv; argv++) {
30 time_t t = atoi(*argv);
31 printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(HUMAN)));
35 static void show_dates(const char **argv, const char *format)
37 struct date_mode mode;
39 parse_date_format(format, &mode);
40 for (; *argv; argv++) {
41 char *arg;
42 timestamp_t t;
43 int tz;
46 * Do not use our normal timestamp parsing here, as the point
47 * is to test the formatting code in isolation.
49 t = parse_timestamp(*argv, &arg, 10);
50 while (*arg == ' ')
51 arg++;
52 tz = atoi(arg);
54 printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
58 static void parse_dates(const char **argv)
60 struct strbuf result = STRBUF_INIT;
62 for (; *argv; argv++) {
63 timestamp_t t;
64 int tz;
66 strbuf_reset(&result);
67 parse_date(*argv, &result);
68 if (sscanf(result.buf, "%"PRItime" %d", &t, &tz) == 2)
69 printf("%s -> %s\n",
70 *argv, show_date(t, tz, DATE_MODE(ISO8601)));
71 else
72 printf("%s -> bad\n", *argv);
74 strbuf_release(&result);
77 static void parse_approxidate(const char **argv)
79 for (; *argv; argv++) {
80 timestamp_t t;
81 t = approxidate_relative(*argv);
82 printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
86 static void parse_approx_timestamp(const char **argv)
88 for (; *argv; argv++) {
89 timestamp_t t;
90 t = approxidate_relative(*argv);
91 printf("%s -> %"PRItime"\n", *argv, t);
95 static void getnanos(const char **argv)
97 double seconds = getnanotime() / 1.0e9;
99 if (*argv)
100 seconds -= strtod(*argv, NULL);
101 printf("%lf\n", seconds);
104 int cmd__date(int argc, const char **argv)
106 const char *x;
108 argv++;
109 if (!*argv)
110 usage(usage_msg);
111 if (!strcmp(*argv, "relative"))
112 show_relative_dates(argv+1);
113 else if (!strcmp(*argv, "human"))
114 show_human_dates(argv+1);
115 else if (skip_prefix(*argv, "show:", &x))
116 show_dates(argv+1, x);
117 else if (!strcmp(*argv, "parse"))
118 parse_dates(argv+1);
119 else if (!strcmp(*argv, "approxidate"))
120 parse_approxidate(argv+1);
121 else if (!strcmp(*argv, "timestamp"))
122 parse_approx_timestamp(argv+1);
123 else if (!strcmp(*argv, "getnanos"))
124 getnanos(argv+1);
125 else if (!strcmp(*argv, "is64bit"))
126 return sizeof(timestamp_t) == 8 ? 0 : 1;
127 else if (!strcmp(*argv, "time_t-is64bit"))
128 return sizeof(time_t) == 8 ? 0 : 1;
129 else
130 usage(usage_msg);
131 return 0;