diff --git a/vlib/time/time.c.v b/vlib/time/time.c.v index 503c4815f5..9816706dfd 100644 --- a/vlib/time/time.c.v +++ b/vlib/time/time.c.v @@ -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 } } diff --git a/vlib/time/time.v b/vlib/time/time.v index 3a7eff3a59..986d729cfe 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -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) } diff --git a/vlib/time/time_nix.c.v b/vlib/time/time_nix.c.v index bb3a364f98..7bf12f3222 100644 --- a/vlib/time/time_nix.c.v +++ b/vlib/time/time_nix.c.v @@ -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) diff --git a/vlib/time/time_test.v b/vlib/time/time_test.v index 014b464528..c657b2c8ce 100644 --- a/vlib/time/time_test.v +++ b/vlib/time/time_test.v @@ -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() }