Skip to content

Commit 9eb386f

Browse files
committed
time: add Duration.times/1
1 parent 6e6b950 commit 9eb386f

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

vlib/time/duration.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,9 @@ pub fn (d Duration) debug() string {
120120
}
121121
return 'Duration: ${sign}${res.join(', ')}'
122122
}
123+
124+
// times allows you to return fractional unit durations, based on an existing duration.
125+
// For example, you can: `half_an_hour := time.hour.times(0.5)` .
126+
pub fn (d Duration) times(x f64) Duration {
127+
return f64(d) * x
128+
}

vlib/time/duration_test.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,9 @@ fn test_duration_debug() {
4545
assert time.Duration(169 * time.hour + 5 * time.minute + 7 * time.second).debug() == 'Duration: 7days, 1h, 5m, 7s'
4646
assert (-time.Duration(169 * time.hour + 5 * time.minute + 7 * time.second)).debug() == 'Duration: - 7days, 1h, 5m, 7s'
4747
}
48+
49+
fn test_duration_times() {
50+
assert time.second.times(2) == 2 * time.second
51+
assert time.minute.times(0.5) == 30 * time.second
52+
assert time.minute.times(-1.5) == -90 * time.second
53+
}

0 commit comments

Comments
 (0)