Skip to content

Commit bab7483

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

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
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/t0006-date.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ check_show default "$TIME" 'Wed Jun 15 16:13:20 2016 +0200'
4848
check_show raw "$TIME" '1466000000 +0200'
4949
check_show iso-local "$TIME" '2016-06-15 14:13:20 +0000'
5050

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

0 commit comments

Comments
 (0)