mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
js: time module: implement utc,now,local (#11138)
This commit is contained in:
@@ -119,3 +119,7 @@ fn convert_ctime(t C.tm, microsecond int) Time {
|
||||
unix: make_unix_time(t)
|
||||
}
|
||||
}
|
||||
|
||||
pub const (
|
||||
infinite = Duration(C.INT64_MAX)
|
||||
)
|
||||
|
38
vlib/time/time.js.v
Normal file
38
vlib/time/time.js.v
Normal file
@@ -0,0 +1,38 @@
|
||||
module time
|
||||
|
||||
pub fn now() Time {
|
||||
mut res := Time{}
|
||||
#let date = new Date()
|
||||
#res.year.val = date.getFullYear()
|
||||
#res.month.val = date.getMonth()
|
||||
#res.day.val = date.getDay()
|
||||
#res.hour.val = date.getHours()
|
||||
#res.minute.val = date.getMinutes()
|
||||
#res.second.val = date.getSeconds()
|
||||
#res.microsecond.val = date.getMilliseconds() * 1000
|
||||
#res.unix.val = (date.getTime() / 1000).toFixed(0)
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
pub fn utc() Time {
|
||||
mut res := Time{}
|
||||
#let date = new Date()
|
||||
#res.year.val = date.getUTCFullYear()
|
||||
#res.month.val = date.getUTCMonth()
|
||||
#res.day.val = date.getUTCDay()
|
||||
#res.hour.val = date.getUTCHours()
|
||||
#res.minute.val = date.getUTCMinutes()
|
||||
#res.second.val = date.getUTCSeconds()
|
||||
#res.microsecond.val = date.getUTCMilliseconds() * 1000
|
||||
#res.unix.val = (date.getTime() / 1000).toFixed(0)
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
/// Returns local time
|
||||
pub fn (t Time) local() Time {
|
||||
// TODO: Does this actually correct? JS clock is always set to timezone or no?
|
||||
// if it is not we should try to use Intl for getting local time.
|
||||
return t
|
||||
}
|
@@ -267,7 +267,6 @@ pub const (
|
||||
second = Duration(1000 * millisecond)
|
||||
minute = Duration(60 * second)
|
||||
hour = Duration(60 * minute)
|
||||
infinite = Duration(C.INT64_MAX)
|
||||
)
|
||||
|
||||
// nanoseconds returns the duration as an integer number of nanoseconds.
|
||||
|
31
vlib/time/time_js.js.v
Normal file
31
vlib/time/time_js.js.v
Normal file
@@ -0,0 +1,31 @@
|
||||
module time
|
||||
|
||||
#var $timeOff = 0;
|
||||
#var $seen = 0
|
||||
#function $sys_mono_new_Date() {
|
||||
#var t = Date.now()
|
||||
#if (t < seen)
|
||||
#timeOff += (seen - t)
|
||||
#
|
||||
#seen = t
|
||||
#return t + timeOff
|
||||
#}
|
||||
|
||||
pub fn sys_mono_now() u64 {
|
||||
$if js_browser {
|
||||
mut res := u64(0)
|
||||
#res = new u64(window.performance.now() * 1000000)
|
||||
|
||||
return res
|
||||
} $else $if js_node {
|
||||
mut res := u64(0)
|
||||
#res.val = Number($process.hrtime.bigint())
|
||||
|
||||
return res
|
||||
} $else {
|
||||
mut res := u64(0)
|
||||
#res = new u64($sys_mono_new_Date() * 1000000)
|
||||
|
||||
return res
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user