time: fixed the recursive local time call by check local time status

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
pull/13861/head
Vincenzo Palazzo 2022-03-31 19:07:01 +02:00
parent 5e3a916afe
commit e6951662b1
No known key found for this signature in database
GPG Key ID: 8B6DC2B870B80D5F
4 changed files with 12 additions and 20 deletions

View File

@ -117,5 +117,9 @@ fn convert_ctime(t C.tm, microsecond int) Time {
second: t.tm_sec
microsecond: microsecond
unix: make_unix_time(t)
// for the actual code base when we
// call convert_ctime, it is always
// when we manage the local time.
is_local: true
}
}

View File

@ -45,6 +45,7 @@ pub:
second int
microsecond int
unix i64
is_local bool // used to make time.now().local().local() == time.now().local()
}
// FormatDelimiter contains different time formats.
@ -348,7 +349,7 @@ pub fn (d Duration) str() string {
// offset returns time zone UTC offset in seconds.
pub fn offset() int {
t := now()
t := utc()
local := t.local()
return int(local.unix - t.unix)
}

View File

@ -30,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)

View File

@ -259,23 +259,7 @@ fn test_since() {
// problem: the local method add 2h on the time in a Linux machine
// the other machine are not tested in a local env
fn test_recursive_local_call() {
assert time.now().unix == time.now().local().unix
assert time.now().str() == time.now().local().str()
assert time.now().local().str() == time.now().local().local().str()
}
fn test_local_method_with_time_operation() {
//FIXME: add a fixed TIMEZONE here
assert time.now().str() != time.utc().str()
assert time.now().str() == time.utc().local().str()
utc := time.utc()
utc_plus_1h := time.utc().add(time.hour).local()
// check if the utc local is increased by 1h
assert utc_plus_1h.hour == time.now().local().hour - 1
// trivial check
assert utc.hour < utc_plus_1h.hour
// normal utc vs an 1h alterate utc + local convertion
assert utc.hour == utc_plus_1h.hour - 3
now_tm := time.now()
assert now_tm.str() == now_tm.local().str()
assert now_tm.local().str() == now_tm.local().local().str()
}