1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

time: fix iso8601 parser and utc time

This commit is contained in:
Tomas Hellström
2020-06-10 11:14:55 +02:00
committed by GitHub
parent 8f9f426479
commit 2dc547a45c
10 changed files with 255 additions and 88 deletions

View File

@ -31,6 +31,7 @@ struct SystemTime {
fn C.GetSystemTimeAsFileTime(lpSystemTimeAsFileTime C._FILETIME)
fn C.FileTimeToSystemTime()
fn C.SystemTimeToTzSpecificLocalTime()
fn C.localtime_s(t &C.time_t, tm &C.tm )
const (
// start_time is needed on Darwin and Windows because of potential overflows
@ -91,9 +92,36 @@ fn local_as_unix_time() int {
return make_unix_time(tm)
}
fn to_local_time(t Time) Time {
st_utc := SystemTime{
year: u16(t.year)
month: u16(t.month)
day: u16(t.day)
hour: u16(t.hour)
minute: u16(t.minute)
second: u16(t.second)
}
st_local := SystemTime{}
C.SystemTimeToTzSpecificLocalTime(voidptr(0), &st_utc, &st_local)
t_local := Time {
year: st_local.year
month: st_local.month
day: st_local.day
hour: st_local.hour
minute: st_local.minute
second: st_local.second
// These are the same
microsecond: t.microsecond
unix: t.unix
}
return t_local
}
// win_now calculates current time using winapi to get higher resolution on windows
// GetSystemTimeAsFileTime is used. It can resolve time down to millisecond
// other more precice methods can be implemented in the future
// GetSystemTimeAsFileTime is used and converted to local time. It can resolve time
// down to millisecond. Other more precice methods can be implemented in the future
[inline]
fn win_now() Time {
ft_utc := C._FILETIME{}
@ -119,6 +147,32 @@ fn win_now() Time {
return t
}
// win_utc calculates current time using winapi to get higher resolution on windows
// GetSystemTimeAsFileTime is used. It can resolve time down to millisecond
// other more precice methods can be implemented in the future
[inline]
fn win_utc() Time {
ft_utc := C._FILETIME{}
C.GetSystemTimeAsFileTime(&ft_utc)
st_utc := SystemTime{}
C.FileTimeToSystemTime(&ft_utc, &st_utc)
t := Time {
year: st_utc.year
month: st_utc.month
day: st_utc.day
hour: st_utc.hour
minute: st_utc.minute
second: st_utc.second
microsecond: st_utc.millisecond*1000
unix: u64(st_utc.unix_time())
}
return t
}
// unix_time returns Unix time.
pub fn (st SystemTime) unix_time() int {
tt := C.tm{
@ -147,6 +201,21 @@ pub fn solaris_now() Time {
return Time{}
}
pub fn darwin_utc() Time {
return Time{}
}
// dummy to compile with all compilers
pub fn linux_utc() Time {
return Time{}
}
// dummy to compile with all compilers
pub fn solaris_utc() Time {
return Time{}
}
// dummy to compile with all compilers
pub struct C.timeval {
tv_sec u64