1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

time: add Duration.str() (#12897)

* time: add str() method to Duration

* add Duration.str tests, move time.infinite to time.v, to be visible to the JS backend

Co-authored-by: Delyan Angelov <delian66@gmail.com>
This commit is contained in:
Miccah
2021-12-19 11:32:42 -06:00
committed by GitHub
parent 840a92c14e
commit d07975335d
3 changed files with 83 additions and 13 deletions

33
vlib/time/duration_test.v Normal file
View File

@ -0,0 +1,33 @@
import time
fn test_duration_str() {
assert time.Duration(1 * time.nanosecond).str() == '1ns'
assert time.Duration(999 * time.nanosecond).str() == '999ns'
assert time.Duration(1000 * time.nanosecond).str() == '1.000us'
//
assert time.Duration(1 * time.microsecond).str() == '1.000us'
assert time.Duration(999 * time.microsecond).str() == '999.000us'
assert time.Duration(1000 * time.microsecond).str() == '1.000ms'
//
assert time.Duration(1 * time.second).str() == '1.000s'
assert time.Duration(999 * time.second).str() == '16:39.000'
assert time.Duration(1000 * time.second).str() == '16:40.000'
//
assert time.Duration(1 * time.minute).str() == '1:00.000'
assert time.Duration(999 * time.minute).str() == '16:39:00'
assert time.Duration(1000 * time.minute).str() == '16:40:00'
//
assert time.Duration(1 * time.hour).str() == '1:00:00'
assert time.Duration(999 * time.hour).str() == '999:00:00'
assert time.Duration(1000 * time.hour).str() == '1000:00:00'
//
assert time.Duration(1 * time.microsecond + 7 * time.nanosecond).str() == '1.007us'
//
assert time.Duration(1 * time.second + 5 * time.nanosecond).str() == '1.000s'
assert time.Duration(1 * time.second + 5 * time.microsecond).str() == '1.000s'
assert time.Duration(1 * time.second + 5 * time.millisecond).str() == '1.005s'
//
assert time.Duration(1 * time.hour + 5 * time.millisecond).str() == '1:00:00'
assert time.Duration(1 * time.hour + 5 * time.second).str() == '1:00:05'
assert time.Duration(168 * time.hour + 5 * time.minute + 7 * time.second).str() == '168:05:07'
}