1
0
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:
playX 2021-08-11 09:22:53 +03:00 committed by GitHub
parent 012b3f0f64
commit 70124d2d23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 103 additions and 2 deletions

View File

@ -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
View 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
}

View File

@ -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
View 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
}
}

View File

@ -327,6 +327,7 @@ pub fn (mut g JsGen) init() {
g.definitions.writeln('const \$os = require("os");')
g.definitions.writeln('const \$process = process;')
}
g.definitions.writeln('function alias(value) { return value; } ')
}
pub fn (g JsGen) hashes() string {
@ -406,13 +407,41 @@ fn (mut g JsGen) get_alias(name string) string {
fn (mut g JsGen) js_name(name_ string) string {
mut is_js := false
is_overload := ['+', '-', '*', '/', '==', '<', '>']
mut name := name_
if name.starts_with('JS.') {
name = name[3..]
is_js = true
}
ns := get_ns(name)
name = if g.ns == 0 {
name = if name in is_overload {
match name {
'+' {
'\$add'
}
'-' {
'\$sub'
}
'/' {
'\$div'
}
'*' {
'\$mul'
}
'==' {
'eq'
}
'>' {
'\$gt'
}
'<' {
'\$lt'
}
else {
''
}
}
} else if g.ns == 0 {
name
} else if ns == g.ns.name {
name.split('.').last()