1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

time: fix repetitive time.now().local().local().local() offsetting the time further and further (#13861)

This commit is contained in:
Vincenzo Palazzo
2022-03-31 23:11:17 +02:00
committed by GitHub
parent 9c1981a309
commit 02c80bd445
5 changed files with 21 additions and 2 deletions

View File

@ -20,7 +20,8 @@ struct C.tm {
fn C.timegm(&C.tm) C.time_t
// fn C.gmtime_r(&tm, &gbuf)
// prefering localtime_r over the localtime because
// from docs localtime_r is thread safe,
fn C.localtime_r(t &C.time_t, tm &C.tm)
fn make_unix_time(t C.tm) i64 {
@ -29,6 +30,9 @@ fn make_unix_time(t C.tm) i64 {
// local returns t with the location set to local time.
pub fn (t Time) local() Time {
if t.is_local {
return t
}
loc_tm := C.tm{}
C.localtime_r(voidptr(&t.unix), &loc_tm)
return convert_ctime(loc_tm, t.microsecond)