Skip to content

Commit 36d6792

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fdba2cd commit 36d6792

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

t/t0006-date.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ check_relative 630000000 '20 years ago'
2727
check_relative 31449600 '12 months ago'
2828
check_relative 62985600 '2 years ago'
2929

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+
3051
check_parse() {
3152
echo "$1 -> $2" >expect
3253
test_expect_${4:-success} "parse date ($1${3:+ TZ=$3})" "

test-date.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
static const char *usage_msg = "\n"
44
" test-date relative [time_t]...\n"
5+
" test-date show:<format> [time_t]...\n"
56
" test-date parse [date]...\n"
67
" test-date approxidate [date]...\n";
78

@@ -17,6 +18,29 @@ static void show_relative_dates(char **argv, struct timeval *now)
1718
strbuf_release(&buf);
1819
}
1920

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+
2044
static void parse_dates(char **argv, struct timeval *now)
2145
{
2246
struct strbuf result = STRBUF_INIT;
@@ -63,6 +87,8 @@ int main(int argc, char **argv)
6387
usage(usage_msg);
6488
if (!strcmp(*argv, "relative"))
6589
show_relative_dates(argv+1, &now);
90+
else if (skip_prefix(*argv, "show:", &x))
91+
show_dates(argv+1, x);
6692
else if (!strcmp(*argv, "parse"))
6793
parse_dates(argv+1, &now);
6894
else if (!strcmp(*argv, "approxidate"))

0 commit comments

Comments
 (0)