From fdba2cdec47b1760422107c671feef1d2a57b8af Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 20 Jun 2016 17:10:29 -0400 Subject: [PATCH 1/3] t0006: rename test-date's "show" to "relative" The "show" tests are really only checking relative formats; we should make that more clear. This also frees up the "show" name to later check other formats. We could later fold "relative" into a more generic "show" command, but it's not worth it. Relative times are a special case already because we have to munge the concept of "now" in our tests. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- t/t0006-date.sh | 26 +++++++++++++------------- test-date.c | 8 ++++---- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/t/t0006-date.sh b/t/t0006-date.sh index fac0986134..fa05269e50 100755 --- a/t/t0006-date.sh +++ b/t/t0006-date.sh @@ -6,26 +6,26 @@ test_description='test date parsing and printing' # arbitrary reference time: 2009-08-30 19:20:00 TEST_DATE_NOW=1251660000; export TEST_DATE_NOW -check_show() { +check_relative() { t=$(($TEST_DATE_NOW - $1)) echo "$t -> $2" >expect test_expect_${3:-success} "relative date ($2)" " - test-date show $t >actual && + test-date relative $t >actual && test_i18ncmp expect actual " } -check_show 5 '5 seconds ago' -check_show 300 '5 minutes ago' -check_show 18000 '5 hours ago' -check_show 432000 '5 days ago' -check_show 1728000 '3 weeks ago' -check_show 13000000 '5 months ago' -check_show 37500000 '1 year, 2 months ago' -check_show 55188000 '1 year, 9 months ago' -check_show 630000000 '20 years ago' -check_show 31449600 '12 months ago' -check_show 62985600 '2 years ago' +check_relative 5 '5 seconds ago' +check_relative 300 '5 minutes ago' +check_relative 18000 '5 hours ago' +check_relative 432000 '5 days ago' +check_relative 1728000 '3 weeks ago' +check_relative 13000000 '5 months ago' +check_relative 37500000 '1 year, 2 months ago' +check_relative 55188000 '1 year, 9 months ago' +check_relative 630000000 '20 years ago' +check_relative 31449600 '12 months ago' +check_relative 62985600 '2 years ago' check_parse() { echo "$1 -> $2" >expect diff --git a/test-date.c b/test-date.c index 63f373557e..8ebcdedbf0 100644 --- a/test-date.c +++ b/test-date.c @@ -1,11 +1,11 @@ #include "cache.h" static const char *usage_msg = "\n" -" test-date show [time_t]...\n" +" test-date relative [time_t]...\n" " test-date parse [date]...\n" " test-date approxidate [date]...\n"; -static void show_dates(char **argv, struct timeval *now) +static void show_relative_dates(char **argv, struct timeval *now) { struct strbuf buf = STRBUF_INIT; @@ -61,8 +61,8 @@ int main(int argc, char **argv) argv++; if (!*argv) usage(usage_msg); - if (!strcmp(*argv, "show")) - show_dates(argv+1, &now); + if (!strcmp(*argv, "relative")) + show_relative_dates(argv+1, &now); else if (!strcmp(*argv, "parse")) parse_dates(argv+1, &now); else if (!strcmp(*argv, "approxidate")) From 36d6792157cc064607147b942c80bc3716dd339b Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 20 Jun 2016 17:11:59 -0400 Subject: [PATCH 2/3] 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 Signed-off-by: Junio C Hamano --- t/t0006-date.sh | 21 +++++++++++++++++++++ test-date.c | 26 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/t/t0006-date.sh b/t/t0006-date.sh index fa05269e50..57033ddcd1 100755 --- a/t/t0006-date.sh +++ b/t/t0006-date.sh @@ -27,6 +27,27 @@ check_relative 630000000 '20 years ago' check_relative 31449600 '12 months ago' check_relative 62985600 '2 years ago' +check_show () { + format=$1 + time=$2 + expect=$3 + test_expect_${4:-success} "show date ($format:$time)" ' + echo "$time -> $expect" >expect && + test-date show:$format "$time" >actual && + test_cmp expect actual + ' +} + +# arbitrary but sensible time for examples +TIME='1466000000 +0200' +check_show iso8601 "$TIME" '2016-06-15 16:13:20 +0200' +check_show iso8601-strict "$TIME" '2016-06-15T16:13:20+02:00' +check_show rfc2822 "$TIME" 'Wed, 15 Jun 2016 16:13:20 +0200' +check_show short "$TIME" '2016-06-15' +check_show default "$TIME" 'Wed Jun 15 16:13:20 2016 +0200' +check_show raw "$TIME" '1466000000 +0200' +check_show iso-local "$TIME" '2016-06-15 14:13:20 +0000' + check_parse() { echo "$1 -> $2" >expect test_expect_${4:-success} "parse date ($1${3:+ TZ=$3})" " diff --git a/test-date.c b/test-date.c index 8ebcdedbf0..d9ab360909 100644 --- a/test-date.c +++ b/test-date.c @@ -2,6 +2,7 @@ static const char *usage_msg = "\n" " test-date relative [time_t]...\n" +" test-date show: [time_t]...\n" " test-date parse [date]...\n" " test-date approxidate [date]...\n"; @@ -17,6 +18,29 @@ static void show_relative_dates(char **argv, struct timeval *now) strbuf_release(&buf); } +static void show_dates(char **argv, const char *format) +{ + struct date_mode mode; + + parse_date_format(format, &mode); + for (; *argv; argv++) { + char *arg = *argv; + time_t t; + int tz; + + /* + * Do not use our normal timestamp parsing here, as the point + * is to test the formatting code in isolation. + */ + t = strtol(arg, &arg, 10); + while (*arg == ' ') + arg++; + tz = atoi(arg); + + printf("%s -> %s\n", *argv, show_date(t, tz, &mode)); + } +} + static void parse_dates(char **argv, struct timeval *now) { struct strbuf result = STRBUF_INIT; @@ -63,6 +87,8 @@ int main(int argc, char **argv) usage(usage_msg); if (!strcmp(*argv, "relative")) show_relative_dates(argv+1, &now); + else if (skip_prefix(*argv, "show:", &x)) + show_dates(argv+1, x); else if (!strcmp(*argv, "parse")) parse_dates(argv+1, &now); else if (!strcmp(*argv, "approxidate")) From bab748371a104c58058c0eff9f4073b710ce0355 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 20 Jun 2016 17:14:14 -0400 Subject: [PATCH 3/3] local_tzoffset: detect errors from tm_to_time_t When we want to know the local timezone offset at a given timestamp, we compute it by asking for localtime() at the given time, and comparing the offset to GMT at that time. However, there's some juggling between time_t and "struct tm" which happens, which involves calling our own tm_to_time_t(). If that function returns an error (e.g., because it only handles dates up to the year 2099), it returns "-1", which we treat as a time_t, and is clearly bogus, leading to bizarre timestamps (that seem to always adjust the time back to (time_t)(uint32_t)-1, in the year 2106). It's not a good idea for local_tzoffset() to simply die here; it would make it hard to run "git log" on a repository with funny timestamps. Instead, let's just treat such cases as "zero offset". Reported-by: Norbert Kiesel Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- date.c | 2 ++ t/t0006-date.sh | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/date.c b/date.c index 7c9f76998a..4c7aa9ba85 100644 --- a/date.c +++ b/date.c @@ -74,6 +74,8 @@ static int local_tzoffset(unsigned long time) localtime_r(&t, &tm); t_local = tm_to_time_t(&tm); + if (t_local == -1) + return 0; /* error; just use +0000 */ if (t_local < t) { eastwest = -1; offset = t - t_local; diff --git a/t/t0006-date.sh b/t/t0006-date.sh index 57033ddcd1..04ce53509c 100755 --- a/t/t0006-date.sh +++ b/t/t0006-date.sh @@ -48,6 +48,11 @@ check_show default "$TIME" 'Wed Jun 15 16:13:20 2016 +0200' check_show raw "$TIME" '1466000000 +0200' check_show iso-local "$TIME" '2016-06-15 14:13:20 +0000' +# arbitrary time absurdly far in the future +FUTURE="5758122296 -0400" +check_show iso "$FUTURE" "2152-06-19 18:24:56 -0400" +check_show iso-local "$FUTURE" "2152-06-19 22:24:56 +0000" + check_parse() { echo "$1 -> $2" >expect test_expect_${4:-success} "parse date ($1${3:+ TZ=$3})" "