mirror of
https://github.com/vlang/v.git
synced 2025-09-14 15:02:33 +03:00
121 lines
2.9 KiB
V
121 lines
2.9 KiB
V
module time
|
|
|
|
// A lot of these are taken from the Go library.
|
|
pub type Duration = i64
|
|
|
|
pub const nanosecond = Duration(1)
|
|
pub const microsecond = Duration(1000 * nanosecond)
|
|
pub const millisecond = Duration(1000 * microsecond)
|
|
pub const second = Duration(1000 * millisecond)
|
|
pub const minute = Duration(60 * second)
|
|
pub const hour = Duration(60 * minute)
|
|
// day = Duration(24 * hour)
|
|
pub const infinite = Duration(i64(9223372036854775807))
|
|
|
|
// nanoseconds returns the duration as an integer number of nanoseconds.
|
|
pub fn (d Duration) nanoseconds() i64 {
|
|
return i64(d)
|
|
}
|
|
|
|
// microseconds returns the duration as an integer number of microseconds.
|
|
pub fn (d Duration) microseconds() i64 {
|
|
return i64(d) / time.microsecond
|
|
}
|
|
|
|
// milliseconds returns the duration as an integer number of milliseconds.
|
|
pub fn (d Duration) milliseconds() i64 {
|
|
return i64(d) / time.millisecond
|
|
}
|
|
|
|
// The following functions return floating point numbers because it's common to
|
|
// consider all of them in sub-one intervals
|
|
// seconds returns the duration as a floating point number of seconds.
|
|
pub fn (d Duration) seconds() f64 {
|
|
return f64(d) / f64(time.second)
|
|
}
|
|
|
|
// minutes returns the duration as a floating point number of minutes.
|
|
pub fn (d Duration) minutes() f64 {
|
|
return f64(d) / f64(time.minute)
|
|
}
|
|
|
|
// hours returns the duration as a floating point number of hours.
|
|
pub fn (d Duration) hours() f64 {
|
|
return f64(d) / f64(time.hour)
|
|
}
|
|
|
|
// days returns the duration as a floating point number of days.
|
|
pub fn (d Duration) days() f64 {
|
|
return f64(d) / f64(time.hour * 24)
|
|
}
|
|
|
|
// str pretty prints the duration
|
|
//
|
|
// ```
|
|
// h:m:s // 5:02:33
|
|
// m:s.mi<s> // 2:33.015
|
|
// s.mi<s> // 33.015s
|
|
// mi.mc<ms> // 15.007ms
|
|
// mc.ns<ns> // 7.234us
|
|
// ns<ns> // 234ns
|
|
// ```
|
|
pub fn (d Duration) str() string {
|
|
if d == time.infinite {
|
|
return 'inf'
|
|
}
|
|
mut sign := ''
|
|
mut t := i64(d)
|
|
if t < 0 {
|
|
sign = '-'
|
|
t = -t
|
|
}
|
|
hr := t / time.hour
|
|
t -= hr * time.hour
|
|
min := t / time.minute
|
|
t -= min * time.minute
|
|
sec := t / time.second
|
|
t -= sec * time.second
|
|
ms := t / time.millisecond
|
|
t -= ms * time.millisecond
|
|
us := t / time.microsecond
|
|
t -= us * time.microsecond
|
|
ns := t
|
|
|
|
return match true {
|
|
hr > 0 { '${sign}${hr}:${min:02}:${sec:02}' }
|
|
min > 0 { '${sign}${min}:${sec:02}.${ms:03}' }
|
|
sec > 0 { '${sign}${sec}.${ms:03}s' }
|
|
ms > 0 { '${sign}${ms}.${us:03}ms' }
|
|
us > 0 { '${sign}${us}.${ns:03}us' }
|
|
else { '${sign}${ns}ns' }
|
|
}
|
|
}
|
|
|
|
// debug returns a detailed breakdown of the Duration, as: 'Duration: - 50days, 4h, 3m, 7s, 541ms, 78us, 9ns'
|
|
pub fn (d Duration) debug() string {
|
|
mut res := []string{}
|
|
mut x := i64(d)
|
|
mut sign := ''
|
|
if x < 0 {
|
|
sign = '- '
|
|
x = -x
|
|
}
|
|
for label, v in {
|
|
'days': 24 * time.hour
|
|
'h': time.hour
|
|
'm': time.minute
|
|
's': time.second
|
|
'ms': time.millisecond
|
|
'us': time.microsecond
|
|
} {
|
|
if x > v {
|
|
xx := x / v
|
|
x = x % v
|
|
res << xx.str() + label
|
|
}
|
|
}
|
|
if x > 0 {
|
|
res << '${x}ns'
|
|
}
|
|
return 'Duration: ${sign}${res.join(', ')}'
|
|
}
|