From 3155f9903ec263d33f375c762a6a346bc8dc92c5 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 19 Nov 2023 23:30:02 -0800 Subject: time: bug: must subtract gmtoff, not add. This works fine: 1> (time-struct-utc 0) #S(time year 1970 month 1 day 1 hour 0 min 0 sec 0 wday 4 yday 0 dst nil gmtoff 0 zone "GMT") 2> *1.(time-utc) 0 3> *1.(time-local) 28800 But we want the following to return the same results: 1> (time-struct-local 0) #S(time year 1969 month 12 day 31 hour 16 min 0 sec 0 wday 3 yday 364 dst nil gmtoff -28800 zone "PST") 2> *1.(time-utc) -57600 3> *1.(time-local) -28800 With the patch, we do: 1> (time-struct-local 0) #S(time year 1969 month 12 day 31 hour 16 min 0 sec 0 wday 3 yday 364 dst nil gmtoff -28800 zone "PST") 2> *1.(time-utc) 0 3> *1.(time-local) 28800 This is also broken: 1> (time-parse-utc "%H:%M:%z" "00:00:-0800") -28800 It must return 28800. * time.c (time_meth): This function, which is the imlpementation of the time-utc and time-local methods, must subtract the gmtoff field, not add it. This is so that a UTC time expressed in a local time zone will convert back to the correct UTC epoch. (time_parse_local, time_parse_utc): Here we likewise must subtract the gmtoff. --- time.c | 8 ++++---- txr.1 | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/time.c b/time.c index b53fa37f..fbc1d42c 100644 --- a/time.c +++ b/time.c @@ -438,7 +438,7 @@ static val time_meth(val utc_p, val time_struct) hour, min, sec, dst); if (gmtoff) - out = plus(out, gmtoff); + out = minus(out, gmtoff); return out; } @@ -501,7 +501,7 @@ val time_parse_local(val format, val string) #if HAVE_TM_GMTOFF { long gmtoff = tms.TM_GMTOFF; - return num(mktime(&tms) + gmtoff); + return num(mktime(&tms) - gmtoff); } #else return num(mktime(&tms)); @@ -516,12 +516,12 @@ val time_parse_utc(val format, val string) #if HAVE_TIMEGM && HAVE_TM_GMTOFF { long gmtoff = tms.TM_GMTOFF; - return num_time(timegm(&tms) + gmtoff); + return num_time(timegm(&tms) - gmtoff); } #elif HAVE_TM_GMTOFF { long gmtoff = tms.TM_GMTOFF; - return num_time(timegm_hack(&tms) + tms.gmtoff); + return num_time(timegm_hack(&tms) - tms.gmtoff); } #elif HAVE_TIMEGM return num_time(timegm(&tms)); diff --git a/txr.1 b/txr.1 index 38172b93..f5a409b5 100644 --- a/txr.1 +++ b/txr.1 @@ -69054,7 +69054,7 @@ Note: if the .code gmtoff slot is not .codn nil , -its value is added to the returned result. +its value is subtracted from the returned result. .coNP Method @ time-string .synb -- cgit v1.2.3