v
Зеркало из https://github.com/vlang/v
1module time
2
3// A lot of these are taken from the Go library.
4pub type Duration = i64
5
6pub const nanosecond = Duration(1)
7pub const microsecond = Duration(1000 * nanosecond)
8pub const millisecond = Duration(1000 * microsecond)
9pub const second = Duration(1000 * millisecond)
10pub const minute = Duration(60 * second)
11pub const hour = Duration(60 * minute)
12// day = Duration(24 * hour)
13pub const infinite = Duration(i64(9223372036854775807))
14
15// nanoseconds returns the duration as an integer number of nanoseconds.
16pub fn (d Duration) nanoseconds() i64 {
17return i64(d)
18}
19
20// microseconds returns the duration as an integer number of microseconds.
21pub fn (d Duration) microseconds() i64 {
22return i64(d) / microsecond
23}
24
25// milliseconds returns the duration as an integer number of milliseconds.
26pub fn (d Duration) milliseconds() i64 {
27return i64(d) / millisecond
28}
29
30// The following functions return floating point numbers because it's common to
31// consider all of them in sub-one intervals
32// seconds returns the duration as a floating point number of seconds.
33pub fn (d Duration) seconds() f64 {
34return f64(d) / f64(second)
35}
36
37// minutes returns the duration as a floating point number of minutes.
38pub fn (d Duration) minutes() f64 {
39return f64(d) / f64(minute)
40}
41
42// hours returns the duration as a floating point number of hours.
43pub fn (d Duration) hours() f64 {
44return f64(d) / f64(hour)
45}
46
47// days returns the duration as a floating point number of days.
48pub fn (d Duration) days() f64 {
49return f64(d) / f64(hour * 24)
50}
51
52// str pretty prints the duration
53//
54// ```
55// h:m:s // 5:02:33
56// m:s.mi<s> // 2:33.015
57// s.mi<s> // 33.015s
58// mi.mc<ms> // 15.007ms
59// mc.ns<ns> // 7.234us
60// ns<ns> // 234ns
61// ```
62pub fn (d Duration) str() string {
63if d == infinite {
64return 'inf'
65}
66mut sign := ''
67mut t := i64(d)
68if t < 0 {
69sign = '-'
70t = -t
71}
72hr := t / hour
73t -= hr * hour
74min := t / minute
75t -= min * minute
76sec := t / second
77t -= sec * second
78ms := t / millisecond
79t -= ms * millisecond
80us := t / microsecond
81t -= us * microsecond
82ns := t
83
84return match true {
85hr > 0 { '${sign}${hr}:${min:02}:${sec:02}' }
86min > 0 { '${sign}${min}:${sec:02}.${ms:03}' }
87sec > 0 { '${sign}${sec}.${ms:03}s' }
88ms > 0 { '${sign}${ms}.${us:03}ms' }
89us > 0 { '${sign}${us}.${ns:03}us' }
90else { '${sign}${ns}ns' }
91}
92}
93
94// debug returns a detailed breakdown of the Duration, as: 'Duration: - 50days, 4h, 3m, 7s, 541ms, 78us, 9ns'
95pub fn (d Duration) debug() string {
96mut res := []string{}
97mut x := i64(d)
98mut sign := ''
99if x < 0 {
100sign = '- '
101x = -x
102}
103for label, v in {
104'days': 24 * hour
105'h': hour
106'm': minute
107's': second
108'ms': millisecond
109'us': microsecond
110} {
111if x > v {
112xx := x / v
113x = x % v
114res << xx.str() + label
115}
116}
117if x > 0 {
118res << '${x}ns'
119}
120return 'Duration: ${sign}${res.join(', ')}'
121}
122