-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration.go
216 lines (175 loc) · 4.67 KB
/
duration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package tokiope
import (
"fmt"
"math"
)
type State int8
const (
// StateOK represents no error.
StateOK State = (1 << iota) >> 1
// StateOverflow bit is set if overflow occurred
StateOverflow
)
const NanosPerSecond = 1_000_000_000
const (
SecondsPerMinute = 60
MinutesPerHour = 60
HoursPerDay = 24
SecondsPerHour = SecondsPerMinute * MinutesPerHour
SecondsPerDay = SecondsPerHour * HoursPerDay
)
// Duration represents an amount between two instants.
type Duration struct {
state State
seconds int64
nano int
}
var MinDuration = Duration{seconds: math.MinInt64}
var MaxDuration = Duration{seconds: math.MaxInt64, nano: NanosPerSecond - 1}
// Seconds returns a Duration of seconds and nanoseconds.
func Seconds(seconds int64, nano int64) (d Duration) {
secs, nanos, state := divFloor(nano, NanosPerSecond)
d.state |= state
secs, state = add(secs, seconds)
d.state |= state
d.seconds, d.nano = secs, int(nanos)
return
}
// Minutes returns a Duration of minutes.
func Minutes(minutes int64) (d Duration) {
d.seconds, d.state = mul(minutes, SecondsPerMinute)
return
}
// Hours returns a Duration of hours.
func Hours(hours int64) (d Duration) {
d.seconds, d.state = mul(hours, SecondsPerHour)
return
}
// Days returns a Duration of days.
func Days(days int64) (d Duration) {
d.seconds, d.state = mul(days, SecondsPerDay)
return
}
// Nanoseconds returns a Duration of nanoseconds.
func Nanoseconds(nanoseconds int64) (d Duration) {
secs, nano, state := divFloor(nanoseconds, NanosPerSecond)
d.seconds, d.nano, d.state = secs, int(nano), state
return
}
var _ interface {
Seconds() (seconds int64, nano int64)
Add(o Duration) Duration
AddNano(nanoseconds int64) Duration
Sub(o Duration) Duration
SubNano(nanoseconds int64) Duration
Abs() Duration
Sign() int
Neg() Duration
Cmp(o Duration) int
State() State
OK() bool
String() string
} = Duration{}
// Seconds returns the number of seconds and nanoseconds.
func (d Duration) Seconds() (seconds int64, nano int64) {
return d.seconds, int64(d.nano)
}
// Add returns the Duration adding the other duration.
func (d Duration) Add(o Duration) (out Duration) {
out.state = d.State() | o.State()
nanos := int64(d.nano + o.nano)
secs, nanos := nanos/NanosPerSecond, nanos%NanosPerSecond
secs, state := add(secs, d.seconds)
out.state |= state
secs, state = add(secs, o.seconds)
out.state |= state
out.seconds, out.nano = secs, int(nanos)
return
}
// AddNano returns the Duration adding the nanoseconds.
func (d Duration) AddNano(nanoseconds int64) (out Duration) {
secs, nanos, _ := divFloor(nanoseconds, NanosPerSecond)
return d.Add(Duration{seconds: secs, nano: int(nanos)})
}
// Sub returns the Duration subtracting the other duration.
func (d Duration) Sub(o Duration) (out Duration) {
out.state = d.State() | o.State()
nanos := int64(d.nano - o.nano)
secs, nanos, state := divFloor(nanos, NanosPerSecond)
out.state |= state
secs, state = add(secs, d.seconds)
out.state |= state
secs, state = sub(secs, o.seconds)
out.state |= state
out.seconds, out.nano = secs, int(nanos)
return
}
// SubNano returns the Duration subtracting the nanoseconds.
func (d Duration) SubNano(nanoseconds int64) (out Duration) {
secs, nanos, _ := divFloor(nanoseconds, NanosPerSecond)
return d.Sub(Duration{seconds: secs, nano: int(nanos)})
}
// Abs returns the absolute value of the Duration.
func (d Duration) Abs() Duration {
if d.Sign() < 0 {
return d.Neg()
}
return d
}
// Sign returns the sign of the Duration.
func (d Duration) Sign() int {
if d.seconds == 0 && d.nano == 0 {
return 0
}
if d.seconds >= 0 {
return 1
}
return -1
}
// Neg returns the negated Duration.
func (d Duration) Neg() (out Duration) {
out = d
just, nanos, _ := divFloor(int64(NanosPerSecond-d.nano), NanosPerSecond)
secs, state := mul(out.seconds, -1)
out.state |= state
if just != 1 {
secs, state = mul(d.seconds, -1)
out.state |= state
secs, state = sub(secs, 1)
out.state |= state
}
out.seconds, out.nano = secs, int(nanos)
return
}
// Cmp compares the Duration with the other Duration.
func (d Duration) Cmp(o Duration) int {
if d.less(o) {
return -1
}
if d.greater(o) {
return 1
}
return 0
}
func (d Duration) less(o Duration) bool {
if d.seconds == o.seconds {
return d.nano < o.nano
}
return d.seconds < o.seconds
}
func (d Duration) greater(o Duration) bool {
if d.seconds == o.seconds {
return d.nano > o.nano
}
return d.seconds > o.seconds
}
// String returns the string representation of the Duration.
func (d Duration) String() string {
return fmt.Sprintf(`%d.%09d`, d.seconds, d.nano)
}
func (d Duration) State() State {
return d.state
}
func (d Duration) OK() bool {
return d.state == StateOK
}