clone: use free() instead of UNLEAK()
[git.git] / t / helper / test-date.c
blob45951b1df87c7bf90a25cc1497344681ae42a161
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "date.h"
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);
25 strbuf_release(&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++) {
42 char *arg;
43 timestamp_t t;
44 int tz;
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);
51 while (*arg == ' ')
52 arg++;
53 tz = atoi(arg);
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++) {
66 timestamp_t t;
67 int tz;
69 strbuf_reset(&result);
70 parse_date(*argv, &result);
71 if (sscanf(result.buf, "%"PRItime" %d", &t, &tz) == 2)
72 printf("%s -> %s\n",
73 *argv, show_date(t, tz, DATE_MODE(ISO8601)));
74 else
75 printf("%s -> bad\n", *argv);
77 strbuf_release(&result);
80 static void parse_approxidate(const char **argv)
82 for (; *argv; argv++) {
83 timestamp_t t;
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++) {
92 timestamp_t t;
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;
102 if (*argv)
103 seconds -= strtod(*argv, NULL);
104 printf("%lf\n", seconds);
107 int cmd__date(int argc, const char **argv)
109 const char *x;
111 argv++;
112 if (!*argv)
113 usage(usage_msg);
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"))
121 parse_dates(argv+1);
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"))
127 getnanos(argv+1);
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;
132 else
133 usage(usage_msg);
134 return 0;