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