From 1b7fd2cf00d0fc6b4b1b8d61668c9466ee695b33 Mon Sep 17 00:00:00 2001 From: Ekopalypse <47723516+Ekopalypse@users.noreply.github.com> Date: Sat, 27 Mar 2021 16:34:34 +0100 Subject: [PATCH] time: fix time offset (#9449) --- vlib/time/time_test.v | 22 ++++++++++++++++++++++ vlib/time/time_windows.c.v | 4 ++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/vlib/time/time_test.v b/vlib/time/time_test.v index a498dab674..0511916436 100644 --- a/vlib/time/time_test.v +++ b/vlib/time/time_test.v @@ -1,4 +1,5 @@ import time +import math const ( time_to_test = time.Time{ @@ -223,3 +224,24 @@ fn test_unix_time() { assert utm2 - utm1 > 2 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() +} diff --git a/vlib/time/time_windows.c.v b/vlib/time/time_windows.c.v index 974416b161..ee33a40581 100644 --- a/vlib/time/time_windows.c.v +++ b/vlib/time/time_windows.c.v @@ -114,8 +114,8 @@ pub fn (t Time) local() Time { hour: st_local.hour minute: st_local.minute second: st_local.second // These are the same - microsecond: t.microsecond - unix: t.unix + microsecond: st_local.millisecond * 1000 + unix: u64(st_local.unix_time()) } return t_local }