Skip to content

Commit 1b7fd2c

Browse files
authored
time: fix time offset (#9449)
1 parent d9240bd commit 1b7fd2c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

vlib/time/time_test.v

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import math
23

34
const (
45
time_to_test = time.Time{
@@ -223,3 +224,24 @@ fn test_unix_time() {
223224
assert utm2 - utm1 > 2
224225
assert utm2 - utm1 < 999
225226
}
227+
228+
fn test_offset() {
229+
u := time.utc()
230+
n := time.now()
231+
//
232+
mut diff_seconds := 0
233+
if u.day != n.day {
234+
if u.day > n.day {
235+
diff_seconds = int(math.abs(((u.hour * 60 + u.minute) - (n.hour * 60 + n.minute)) * 60)) - 86400
236+
} else {
237+
diff_seconds = 86400 - int(math.abs(((u.hour * 60 + u.minute) - (n.hour * 60 + n.minute)) * 60))
238+
}
239+
if math.abs(u.day - n.day) > 1 { // different month
240+
diff_seconds = diff_seconds * -1
241+
}
242+
} else { // same day
243+
diff_seconds = ((n.hour * 60 + n.minute) - (u.hour * 60 + u.minute)) * 60
244+
}
245+
246+
assert diff_seconds == time.offset()
247+
}

vlib/time/time_windows.c.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ pub fn (t Time) local() Time {
114114
hour: st_local.hour
115115
minute: st_local.minute
116116
second: st_local.second // These are the same
117-
microsecond: t.microsecond
118-
unix: t.unix
117+
microsecond: st_local.millisecond * 1000
118+
unix: u64(st_local.unix_time())
119119
}
120120
return t_local
121121
}

0 commit comments

Comments
 (0)