t0006: test various date formats

We ended up testing some of these date formats throughout the rest of the suite (e.g., via for-each-ref's "$(authordate:...)" format), but we never did so systematically. t0006 is the right place for unit-testing of our date-handling code. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 20, 2016 at 17:11 UTC 36d6792157cc064607147b942c80bc3716dd339b
2 files changed +47
t/t0006-date.sh
+21
@@ -27,6 +27,27 @@ check_relative 630000000 '20 years ago'
27 check_relative 31449600 '12 months ago'
28 check_relative 62985600 '2 years ago'
29
30 +check_show () {
31 + format=$1
32 + time=$2
33 + expect=$3
34 + test_expect_${4:-success} "show date ($format:$time)" '
35 + echo "$time -> $expect" >expect &&
36 + test-date show:$format "$time" >actual &&
37 + test_cmp expect actual
38 + '
39 +}
40 +
41 +# arbitrary but sensible time for examples
42 +TIME='1466000000 +0200'
43 +check_show iso8601 "$TIME" '2016-06-15 16:13:20 +0200'
44 +check_show iso8601-strict "$TIME" '2016-06-15T16:13:20+02:00'
45 +check_show rfc2822 "$TIME" 'Wed, 15 Jun 2016 16:13:20 +0200'
46 +check_show short "$TIME" '2016-06-15'
47 +check_show default "$TIME" 'Wed Jun 15 16:13:20 2016 +0200'
48 +check_show raw "$TIME" '1466000000 +0200'
49 +check_show iso-local "$TIME" '2016-06-15 14:13:20 +0000'
50 +
51 check_parse() {
52 echo "$1 -> $2" >expect
53 test_expect_${4:-success} "parse date ($1${3:+ TZ=$3})" "
test-date.c
+26
@@ -2,6 +2,7 @@
2
3 static const char *usage_msg = "\n"
4 " test-date relative [time_t]...\n"
5 +" test-date show:<format> [time_t]...\n"
6 " test-date parse [date]...\n"
7 " test-date approxidate [date]...\n";
8
@@ -17,6 +18,29 @@ static void show_relative_dates(char **argv, struct timeval *now)
18 strbuf_release(&buf);
19 }
20
21 +static void show_dates(char **argv, const char *format)
22 +{
23 + struct date_mode mode;
24 +
25 + parse_date_format(format, &mode);
26 + for (; *argv; argv++) {
27 + char *arg = *argv;
28 + time_t t;
29 + int tz;
30 +
31 + /*
32 + * Do not use our normal timestamp parsing here, as the point
33 + * is to test the formatting code in isolation.
34 + */
35 + t = strtol(arg, &arg, 10);
36 + while (*arg == ' ')
37 + arg++;
38 + tz = atoi(arg);
39 +
40 + printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
41 + }
42 +}
43 +
44 static void parse_dates(char **argv, struct timeval *now)
45 {
46 struct strbuf result = STRBUF_INIT;
@@ -63,6 +87,8 @@ int main(int argc, char **argv)
87 usage(usage_msg);
88 if (!strcmp(*argv, "relative"))
89 show_relative_dates(argv+1, &now);
90 + else if (skip_prefix(*argv, "show:", &x))
91 + show_dates(argv+1, x);
92 else if (!strcmp(*argv, "parse"))
93 parse_dates(argv+1, &now);
94 else if (!strcmp(*argv, "approxidate"))