Skip to content

Commit 0c72d6d

Browse files
committed
Merge branch 'jk/tzoffset-fix' into maint
The internal code used to show local timezone offset is not prepared to handle timestamps beyond year 2100, and gave a bogus offset value to the caller. Use a more benign looking +0000 instead and let "git log" going in such a case, instead of aborting. * jk/tzoffset-fix: local_tzoffset: detect errors from tm_to_time_t t0006: test various date formats t0006: rename test-date's "show" to "relative"
2 parents 76180a2 + bab7483 commit 0c72d6d

File tree

3 files changed

+71
-17
lines changed

3 files changed

+71
-17
lines changed

date.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ static int local_tzoffset(unsigned long time)
7474
localtime_r(&t, &tm);
7575
t_local = tm_to_time_t(&tm);
7676

77+
if (t_local == -1)
78+
return 0; /* error; just use +0000 */
7779
if (t_local < t) {
7880
eastwest = -1;
7981
offset = t - t_local;

t/helper/test-date.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "cache.h"
22

33
static const char *usage_msg = "\n"
4-
" test-date show [time_t]...\n"
4+
" 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

8-
static void show_dates(char **argv, struct timeval *now)
9+
static void show_relative_dates(char **argv, struct timeval *now)
910
{
1011
struct strbuf buf = STRBUF_INIT;
1112

@@ -17,6 +18,29 @@ static void show_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;
@@ -61,8 +85,10 @@ int main(int argc, char **argv)
6185
argv++;
6286
if (!*argv)
6387
usage(usage_msg);
64-
if (!strcmp(*argv, "show"))
65-
show_dates(argv+1, &now);
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);
6692
else if (!strcmp(*argv, "parse"))
6793
parse_dates(argv+1, &now);
6894
else if (!strcmp(*argv, "approxidate"))

t/t0006-date.sh

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,52 @@ test_description='test date parsing and printing'
66
# arbitrary reference time: 2009-08-30 19:20:00
77
TEST_DATE_NOW=1251660000; export TEST_DATE_NOW
88

9-
check_show() {
9+
check_relative() {
1010
t=$(($TEST_DATE_NOW - $1))
1111
echo "$t -> $2" >expect
1212
test_expect_${3:-success} "relative date ($2)" "
13-
test-date show $t >actual &&
13+
test-date relative $t >actual &&
1414
test_i18ncmp expect actual
1515
"
1616
}
1717

18-
check_show 5 '5 seconds ago'
19-
check_show 300 '5 minutes ago'
20-
check_show 18000 '5 hours ago'
21-
check_show 432000 '5 days ago'
22-
check_show 1728000 '3 weeks ago'
23-
check_show 13000000 '5 months ago'
24-
check_show 37500000 '1 year, 2 months ago'
25-
check_show 55188000 '1 year, 9 months ago'
26-
check_show 630000000 '20 years ago'
27-
check_show 31449600 '12 months ago'
28-
check_show 62985600 '2 years ago'
18+
check_relative 5 '5 seconds ago'
19+
check_relative 300 '5 minutes ago'
20+
check_relative 18000 '5 hours ago'
21+
check_relative 432000 '5 days ago'
22+
check_relative 1728000 '3 weeks ago'
23+
check_relative 13000000 '5 months ago'
24+
check_relative 37500000 '1 year, 2 months ago'
25+
check_relative 55188000 '1 year, 9 months ago'
26+
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+
# arbitrary time absurdly far in the future
52+
FUTURE="5758122296 -0400"
53+
check_show iso "$FUTURE" "2152-06-19 18:24:56 -0400"
54+
check_show iso-local "$FUTURE" "2152-06-19 22:24:56 +0000"
2955

3056
check_parse() {
3157
echo "$1 -> $2" >expect

0 commit comments

Comments
 (0)