-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
time: extract Duration related code into duration.v (#21229)
- Loading branch information
Showing
2 changed files
with
123 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
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 t := i64(d) | ||
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 { '${hr}:${min:02}:${sec:02}' } | ||
min > 0 { '${min}:${sec:02}.${ms:03}' } | ||
sec > 0 { '${sec}.${ms:03}s' } | ||
ms > 0 { '${ms}.${us:03}ms' } | ||
us > 0 { '${us}.${ns:03}us' } | ||
else { '${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(', ')}' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters