mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
time: update doc comments (#18947)
This commit is contained in:
@ -5,7 +5,7 @@ module time
|
|||||||
|
|
||||||
import strconv
|
import strconv
|
||||||
|
|
||||||
// parse_rfc3339 returns time from a date string in RFC 3339 datetime format.
|
// parse_rfc3339 returns the time from a date string in RFC 3339 datetime format.
|
||||||
// See also https://ijmacd.github.io/rfc3339-iso8601/ for a visual reference of
|
// See also https://ijmacd.github.io/rfc3339-iso8601/ for a visual reference of
|
||||||
// the differences between ISO-8601 and RFC 3339.
|
// the differences between ISO-8601 and RFC 3339.
|
||||||
pub fn parse_rfc3339(s string) !Time {
|
pub fn parse_rfc3339(s string) !Time {
|
||||||
@ -59,7 +59,7 @@ pub fn parse_rfc3339(s string) !Time {
|
|||||||
return error_invalid_time(9, 'malformed date')
|
return error_invalid_time(9, 'malformed date')
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse returns time from a date string in "YYYY-MM-DD HH:mm:ss" format.
|
// parse returns the time from a date string in "YYYY-MM-DD HH:mm:ss" format.
|
||||||
pub fn parse(s string) !Time {
|
pub fn parse(s string) !Time {
|
||||||
if s == '' {
|
if s == '' {
|
||||||
return error_invalid_time(0, 'datetime string is empty')
|
return error_invalid_time(0, 'datetime string is empty')
|
||||||
@ -197,7 +197,7 @@ pub fn parse_iso8601(s string) !Time {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse_rfc2822 returns time from a date string in RFC 2822 datetime format.
|
// parse_rfc2822 returns the time from a date string in RFC 2822 datetime format.
|
||||||
pub fn parse_rfc2822(s string) !Time {
|
pub fn parse_rfc2822(s string) !Time {
|
||||||
if s == '' {
|
if s == '' {
|
||||||
return error_invalid_time(0, 'datetime string is empty')
|
return error_invalid_time(0, 'datetime string is empty')
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
module time
|
module time
|
||||||
|
|
||||||
// parse returns time from a date string.
|
// parse returns the time from a date string.
|
||||||
//
|
//
|
||||||
// TODO(playX): JS Date expects iso8061 format of strings and other formats
|
// TODO(playX): JS Date expects iso8061 format of strings and other formats
|
||||||
// are implementation dependent, we probably want to implement parsing in JS.
|
// are implementation dependent, we probably want to implement parsing in JS.
|
||||||
|
@ -30,13 +30,13 @@ pub fn new_stopwatch(opts StopWatchOptions) StopWatch {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// start starts the stopwatch. If the timer was paused, restarts counting.
|
// start starts the stopwatch. If the timer was paused, it continues counting.
|
||||||
pub fn (mut t StopWatch) start() {
|
pub fn (mut t StopWatch) start() {
|
||||||
t.start = sys_mono_now()
|
t.start = sys_mono_now()
|
||||||
t.end = 0
|
t.end = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// restart restarts the stopwatch. If the timer was paused, restarts counting.
|
// restart restarts the stopwatch. If the timer was paused, it restarts counting.
|
||||||
pub fn (mut t StopWatch) restart() {
|
pub fn (mut t StopWatch) restart() {
|
||||||
t.start = sys_mono_now()
|
t.start = sys_mono_now()
|
||||||
t.end = 0
|
t.end = 0
|
||||||
|
@ -20,7 +20,7 @@ fn C.gmtime(t &C.time_t) &C.tm
|
|||||||
fn C.gmtime_r(t &C.time_t, res &C.tm) &C.tm
|
fn C.gmtime_r(t &C.time_t, res &C.tm) &C.tm
|
||||||
fn C.strftime(buf &char, maxsize usize, const_format &char, const_tm &C.tm) usize
|
fn C.strftime(buf &char, maxsize usize, const_format &char, const_tm &C.tm) usize
|
||||||
|
|
||||||
// now returns current local time.
|
// now returns the current local time.
|
||||||
pub fn now() Time {
|
pub fn now() Time {
|
||||||
$if macos {
|
$if macos {
|
||||||
return darwin_now()
|
return darwin_now()
|
||||||
@ -62,7 +62,7 @@ pub fn utc() Time {
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// new_time returns a time struct with calculated Unix time.
|
// new_time returns a time struct with the calculated Unix time.
|
||||||
pub fn new_time(t Time) Time {
|
pub fn new_time(t Time) Time {
|
||||||
if t.unix != 0 {
|
if t.unix != 0 {
|
||||||
return t
|
return t
|
||||||
@ -82,7 +82,8 @@ pub fn new_time(t Time) Time {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ticks returns a number of milliseconds elapsed since system start.
|
// ticks returns the number of milliseconds since the UNIX epoch.
|
||||||
|
// On Windows ticks returns the number of milliseconds elapsed since system start.
|
||||||
pub fn ticks() i64 {
|
pub fn ticks() i64 {
|
||||||
$if windows {
|
$if windows {
|
||||||
return C.GetTickCount()
|
return C.GetTickCount()
|
||||||
@ -96,7 +97,7 @@ pub fn ticks() i64 {
|
|||||||
// # return (double)(* (uint64_t *) &elapsedNano) / 1000000;
|
// # return (double)(* (uint64_t *) &elapsedNano) / 1000000;
|
||||||
}
|
}
|
||||||
|
|
||||||
// str returns time in the same format as `parse` expects ("YYYY-MM-DD HH:mm:ss").
|
// str returns the time in the same format as `parse` expects ("YYYY-MM-DD HH:mm:ss").
|
||||||
pub fn (t Time) str() string {
|
pub fn (t Time) str() string {
|
||||||
// TODO Define common default format for
|
// TODO Define common default format for
|
||||||
// `str` and `parse` and use it in both ways
|
// `str` and `parse` and use it in both ways
|
||||||
|
@ -30,20 +30,21 @@ pub fn utc() Time {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns local time
|
// local returns the local time.
|
||||||
pub fn (t Time) local() Time {
|
pub fn (t Time) local() Time {
|
||||||
// TODO: Does this actually correct? JS clock is always set to timezone or no?
|
// 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.
|
// if it is not we should try to use Intl for getting local time.
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sleep suspends the execution for a given duration (in nanoseconds).
|
||||||
pub fn sleep(dur Duration) {
|
pub fn sleep(dur Duration) {
|
||||||
#let now = new Date().getTime()
|
#let now = new Date().getTime()
|
||||||
#let toWait = BigInt(dur.val) / BigInt(time__millisecond)
|
#let toWait = BigInt(dur.val) / BigInt(time__millisecond)
|
||||||
#while (new Date().getTime() < now + Number(toWait)) {}
|
#while (new Date().getTime() < now + Number(toWait)) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// new_time returns a time struct with calculated Unix time.
|
// new_time returns a time struct with the calculated Unix time.
|
||||||
pub fn new_time(t Time) Time {
|
pub fn new_time(t Time) Time {
|
||||||
if t.unix != 0 {
|
if t.unix != 0 {
|
||||||
return t
|
return t
|
||||||
|
@ -90,7 +90,7 @@ pub fn Time.new(t Time) Time {
|
|||||||
return new_time(t)
|
return new_time(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// smonth returns month name abbreviation.
|
// smonth returns the month name abbreviation.
|
||||||
pub fn (t Time) smonth() string {
|
pub fn (t Time) smonth() string {
|
||||||
if t.month <= 0 || t.month > 12 {
|
if t.month <= 0 || t.month > 12 {
|
||||||
return '---'
|
return '---'
|
||||||
@ -99,19 +99,19 @@ pub fn (t Time) smonth() string {
|
|||||||
return time.months_string[i * 3..(i + 1) * 3]
|
return time.months_string[i * 3..(i + 1) * 3]
|
||||||
}
|
}
|
||||||
|
|
||||||
// unix_time returns Unix time.
|
// unix_time returns the UNIX time.
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (t Time) unix_time() i64 {
|
pub fn (t Time) unix_time() i64 {
|
||||||
return t.unix
|
return t.unix
|
||||||
}
|
}
|
||||||
|
|
||||||
// unix_time_milli returns Unix time with millisecond resolution.
|
// unix_time_milli returns the UNIX time with millisecond resolution.
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (t Time) unix_time_milli() i64 {
|
pub fn (t Time) unix_time_milli() i64 {
|
||||||
return t.unix * 1000 + (t.microsecond / 1000)
|
return t.unix * 1000 + (t.microsecond / 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
// add returns a new time that duration is added
|
// add returns a new time with the given duration added.
|
||||||
pub fn (t Time) add(d Duration) Time {
|
pub fn (t Time) add(d Duration) Time {
|
||||||
microseconds := i64(t.unix) * 1_000_000 + t.microsecond + d.microseconds()
|
microseconds := i64(t.unix) * 1_000_000 + t.microsecond + d.microseconds()
|
||||||
unix := microseconds / 1_000_000
|
unix := microseconds / 1_000_000
|
||||||
|
@ -11,6 +11,7 @@ module time
|
|||||||
#return t + $timeOff
|
#return t + $timeOff
|
||||||
#}
|
#}
|
||||||
|
|
||||||
|
// sys_mono_now returns a *monotonically increasing time*, NOT a time adjusted for daylight savings, location etc.
|
||||||
pub fn sys_mono_now() u64 {
|
pub fn sys_mono_now() u64 {
|
||||||
$if js_browser {
|
$if js_browser {
|
||||||
mut res := u64(0)
|
mut res := u64(0)
|
||||||
|
@ -125,7 +125,7 @@ pub fn (d Duration) timespec() C.timespec {
|
|||||||
return ts
|
return ts
|
||||||
}
|
}
|
||||||
|
|
||||||
// return timespec of 1970/1/1
|
// zero_timespec returns the calendar time in seconds and nanoseconds of the beginning of the Unix epoch.
|
||||||
pub fn zero_timespec() C.timespec {
|
pub fn zero_timespec() C.timespec {
|
||||||
ts := C.timespec{
|
ts := C.timespec{
|
||||||
tv_sec: 0
|
tv_sec: 0
|
||||||
@ -134,7 +134,7 @@ pub fn zero_timespec() C.timespec {
|
|||||||
return ts
|
return ts
|
||||||
}
|
}
|
||||||
|
|
||||||
// sleep makes the calling thread sleep for a given duration (in nanoseconds).
|
// sleep suspends the execution of the calling thread for a given duration (in nanoseconds).
|
||||||
pub fn sleep(duration Duration) {
|
pub fn sleep(duration Duration) {
|
||||||
mut req := C.timespec{duration / second, duration % second}
|
mut req := C.timespec{duration / second, duration % second}
|
||||||
rem := C.timespec{}
|
rem := C.timespec{}
|
||||||
|
Reference in New Issue
Block a user