time: fixed the recursive local time call by check local time status
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>pull/13861/head
parent
5e3a916afe
commit
e6951662b1
|
|
@ -117,5 +117,9 @@ fn convert_ctime(t C.tm, microsecond int) Time {
|
||||||
second: t.tm_sec
|
second: t.tm_sec
|
||||||
microsecond: microsecond
|
microsecond: microsecond
|
||||||
unix: make_unix_time(t)
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,7 @@ pub:
|
||||||
second int
|
second int
|
||||||
microsecond int
|
microsecond int
|
||||||
unix i64
|
unix i64
|
||||||
|
is_local bool // used to make time.now().local().local() == time.now().local()
|
||||||
}
|
}
|
||||||
|
|
||||||
// FormatDelimiter contains different time formats.
|
// FormatDelimiter contains different time formats.
|
||||||
|
|
@ -348,7 +349,7 @@ pub fn (d Duration) str() string {
|
||||||
|
|
||||||
// offset returns time zone UTC offset in seconds.
|
// offset returns time zone UTC offset in seconds.
|
||||||
pub fn offset() int {
|
pub fn offset() int {
|
||||||
t := now()
|
t := utc()
|
||||||
local := t.local()
|
local := t.local()
|
||||||
return int(local.unix - t.unix)
|
return int(local.unix - t.unix)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,9 @@ fn make_unix_time(t C.tm) i64 {
|
||||||
|
|
||||||
// local returns t with the location set to local time.
|
// local returns t with the location set to local time.
|
||||||
pub fn (t Time) local() Time {
|
pub fn (t Time) local() Time {
|
||||||
|
if t.is_local {
|
||||||
|
return t
|
||||||
|
}
|
||||||
loc_tm := C.tm{}
|
loc_tm := C.tm{}
|
||||||
C.localtime_r(voidptr(&t.unix), &loc_tm)
|
C.localtime_r(voidptr(&t.unix), &loc_tm)
|
||||||
return convert_ctime(loc_tm, t.microsecond)
|
return convert_ctime(loc_tm, t.microsecond)
|
||||||
|
|
|
||||||
|
|
@ -259,23 +259,7 @@ fn test_since() {
|
||||||
// problem: the local method add 2h on the time in a Linux machine
|
// problem: the local method add 2h on the time in a Linux machine
|
||||||
// the other machine are not tested in a local env
|
// the other machine are not tested in a local env
|
||||||
fn test_recursive_local_call() {
|
fn test_recursive_local_call() {
|
||||||
assert time.now().unix == time.now().local().unix
|
now_tm := time.now()
|
||||||
assert time.now().str() == time.now().local().str()
|
assert now_tm.str() == now_tm.local().str()
|
||||||
assert time.now().local().str() == time.now().local().local().str()
|
assert now_tm.local().str() == now_tm.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
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue