time: fix time offset (#9449)

pull/9455/head
Ekopalypse 2021-03-27 16:34:34 +01:00 committed by GitHub
parent d9240bd983
commit 1b7fd2cf00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import time import time
import math
const ( const (
time_to_test = time.Time{ time_to_test = time.Time{
@ -223,3 +224,24 @@ fn test_unix_time() {
assert utm2 - utm1 > 2 assert utm2 - utm1 > 2
assert utm2 - utm1 < 999 assert utm2 - utm1 < 999
} }
fn test_offset() {
u := time.utc()
n := time.now()
//
mut diff_seconds := 0
if u.day != n.day {
if u.day > n.day {
diff_seconds = int(math.abs(((u.hour * 60 + u.minute) - (n.hour * 60 + n.minute)) * 60)) - 86400
} else {
diff_seconds = 86400 - int(math.abs(((u.hour * 60 + u.minute) - (n.hour * 60 + n.minute)) * 60))
}
if math.abs(u.day - n.day) > 1 { // different month
diff_seconds = diff_seconds * -1
}
} else { // same day
diff_seconds = ((n.hour * 60 + n.minute) - (u.hour * 60 + u.minute)) * 60
}
assert diff_seconds == time.offset()
}

View File

@ -114,8 +114,8 @@ pub fn (t Time) local() Time {
hour: st_local.hour hour: st_local.hour
minute: st_local.minute minute: st_local.minute
second: st_local.second // These are the same second: st_local.second // These are the same
microsecond: t.microsecond microsecond: st_local.millisecond * 1000
unix: t.unix unix: u64(st_local.unix_time())
} }
return t_local return t_local
} }