-
Notifications
You must be signed in to change notification settings - Fork 0
/
gt_interval.go
391 lines (323 loc) · 10.4 KB
/
gt_interval.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package gt
import (
"database/sql/driver"
"encoding/json"
"fmt"
"time"
)
/*
Shortcut: parses successfully or panics. Should be used only in root scope. When
error handling is relevant, use `.Parse`.
*/
func ParseInterval(src string) (val Interval) {
try(val.Parse(src))
return
}
// Simplified interval constructor without a time constituent.
func DateInterval(years, months, days int) Interval {
return Interval{Years: years, Months: months, Days: days}
}
// Simplified interval constructor without a date constituent.
func TimeInterval(hours, mins, secs int) Interval {
return Interval{Hours: hours, Minutes: mins, Seconds: secs}
}
// Simplified interval constructor.
func IntervalFrom(years, months, days, hours, mins, secs int) Interval {
return Interval{years, months, days, hours, mins, secs}
}
// Uses `.SetDuration` and returns the resulting interval.
func DurationInterval(src time.Duration) (val Interval) {
val.SetDuration(src)
return
}
/*
Represents an ISO 8601 time interval that has only duration (no timestamps, no
range). Supports all six components of ISO 8601 interval: years, months, days,
hours, minutes, seconds.
Features:
* Reversible encoding/decoding in text.
* Reversible encoding/decoding in JSON.
* Reversible encoding/decoding in SQL.
Text encoding and decoding uses the standard ISO 8601 format:
P0Y0M0DT0H0M0S
When interacting with a database, to make intervals parsable, configure your DB
to always output them in the standard ISO 8601 format.
Limitations:
* Supports only the standard machine-readable format.
* Doesn't support decimal fractions.
For a nullable variant, see `gt.NullInterval`.
*/
type Interval struct {
Years int `json:"years" db:"years"`
Months int `json:"months" db:"months"`
Days int `json:"days" db:"days"`
Hours int `json:"hours" db:"hours"`
Minutes int `json:"minutes" db:"minutes"`
Seconds int `json:"seconds" db:"seconds"`
}
var (
_ = Encodable(Interval{})
_ = Decodable((*Interval)(nil))
)
// Implement `gt.Zeroable`. Equivalent to `reflect.ValueOf(self).IsZero()`.
func (self Interval) IsZero() bool { return self == Interval{} }
// Implement `gt.Nullable`. Always `false`.
func (self Interval) IsNull() bool { return false }
// Implement `gt.Getter`, using `.String` to return a string representation.
func (self Interval) Get() any { return self.String() }
// Implement `gt.Setter`, using `.Scan`. Panics on error.
func (self *Interval) Set(src any) { try(self.Scan(src)) }
// Implement `gt.Zeroer`, zeroing the receiver.
func (self *Interval) Zero() {
if self != nil {
*self = Interval{}
}
}
/*
Implement `fmt.Stringer`, returning a text representation in the standard
machine-readable ISO 8601 format.
*/
func (self Interval) String() string {
if self.IsZero() {
return zeroInterval
}
return bytesString(self.AppendTo(nil))
}
// Implement `gt.Parser`, parsing a valid machine-readable ISO 8601 representation.
func (self *Interval) Parse(src string) error { return self.parse(src) }
// Implement `gt.AppenderTo`, using the same representation as `.String`.
func (self Interval) AppendTo(buf []byte) []byte {
if self.IsZero() {
return append(buf, zeroInterval...)
}
buf = Raw(buf).Grow(self.bufLen())
buf = append(buf, 'P')
buf = appendIntervalPart(buf, self.Years, 'Y')
buf = appendIntervalPart(buf, self.Months, 'M')
buf = appendIntervalPart(buf, self.Days, 'D')
if self.HasTime() {
buf = append(buf, 'T')
buf = appendIntervalPart(buf, self.Hours, 'H')
buf = appendIntervalPart(buf, self.Minutes, 'M')
buf = appendIntervalPart(buf, self.Seconds, 'S')
}
return buf
}
// Implement `encoding.TextMarhaler`, using the same representation as `.String`.
func (self Interval) MarshalText() ([]byte, error) {
return self.AppendTo(nil), nil
}
// Implement `encoding.TextUnmarshaler`, using the same algorithm as `.Parse`.
func (self *Interval) UnmarshalText(src []byte) error {
return self.Parse(bytesString(src))
}
// Implement `json.Marshaler`, using the same representation as `.String`.
func (self Interval) MarshalJSON() ([]byte, error) {
return json.Marshal(self.Get())
}
// Implement `json.Unmarshaler`, using the same algorithm as `.Parse`.
func (self *Interval) UnmarshalJSON(src []byte) error {
if isJsonStr(src) {
return self.UnmarshalText(cutJsonStr(src))
}
return errJsonString(src, self)
}
// Implement `driver.Valuer`, using `.Get`.
func (self Interval) Value() (driver.Value, error) {
return self.Get(), nil
}
/*
Implement `sql.Scanner`, converting an arbitrary input to `gt.Interval` and
modifying the receiver. Acceptable inputs:
* `string` -> use `.Parse`
* `[]byte` -> use `.UnmarshalText`
* `time.Duration` -> use `.SetDuration`
* `gt.Interval` -> assign
* `gt.NullInterval` -> assign
* `gt.Getter` -> scan underlying value
*/
func (self *Interval) Scan(src any) error {
switch src := src.(type) {
case string:
return self.Parse(src)
case []byte:
return self.UnmarshalText(src)
case time.Duration:
self.SetDuration(src)
return nil
case Interval:
*self = src
return nil
case NullInterval:
*self = Interval(src)
return nil
default:
val, ok := get(src)
if ok {
return self.Scan(val)
}
return errScanType(self, src)
}
}
/*
Sets the interval to an approximate value of the given duration, expressed in
hours, minutes, seconds, truncating any fractions that don't fit.
*/
func (self *Interval) SetDuration(val time.Duration) {
const minSecs = 60
const hourMins = 60
// TODO simpler math.
hours := int(val.Hours())
minutes := int(val.Minutes()) - (hours * hourMins)
seconds := int(val.Seconds()) - (minutes * minSecs) - (hours * hourMins * minSecs)
*self = Interval{Hours: hours, Minutes: minutes, Seconds: seconds}
}
// Returns the date portion of the interval, disregarding the time portion. The
// result can be passed to `time.Time.AddDate` and `gt.NullTime.AddDate`.
func (self Interval) Date() (years int, months int, days int) {
return self.Years, self.Months, self.Days
}
// Returns only the date portion of this interval, with other fields set to 0.
func (self Interval) OnlyDate() Interval {
return Interval{Years: self.Years, Months: self.Months, Days: self.Days}
}
// Returns only the time portion of this interval, with other fields set to 0.
func (self Interval) OnlyTime() Interval {
return Interval{Hours: self.Hours, Minutes: self.Minutes, Seconds: self.Seconds}
}
// True if the interval has years, months, or days.
func (self Interval) HasDate() bool {
return self.Years != 0 || self.Months != 0 || self.Days != 0
}
// True if the interval has hours, minutes, or seconds.
func (self Interval) HasTime() bool {
return self.Hours != 0 || self.Minutes != 0 || self.Seconds != 0
}
/*
Returns the duration of ONLY the time portion of this interval. Panics if the
interval has a date constituent. To make it clear that you're explicitly
disregarding the date part, call `.OnlyTime` first. Warning: there are no
overflow checks. Usage example:
someInterval.OnlyTime().Duration()
*/
func (self Interval) Duration() time.Duration {
if self.HasDate() {
panic(fmt.Errorf(`[gt] failed to convert interval %q to duration: days/months/years can't be converted to nanoseconds`, &self))
}
return time.Duration(self.Hours)*time.Hour +
time.Duration(self.Minutes)*time.Minute +
time.Duration(self.Seconds)*time.Second
}
// Returns a version of this interval with `.Years = val`.
func (self Interval) WithYears(val int) Interval {
self.Years = val
return self
}
// Returns a version of this interval with `.Months = val`.
func (self Interval) WithMonths(val int) Interval {
self.Months = val
return self
}
// Returns a version of this interval with `.Days = val`.
func (self Interval) WithDays(val int) Interval {
self.Days = val
return self
}
// Returns a version of this interval with `.Hours = val`.
func (self Interval) WithHours(val int) Interval {
self.Hours = val
return self
}
// Returns a version of this interval with `.Minutes = val`.
func (self Interval) WithMinutes(val int) Interval {
self.Minutes = val
return self
}
// Returns a version of this interval with `.Seconds = val`.
func (self Interval) WithSeconds(val int) Interval {
self.Seconds = val
return self
}
// Returns a version of this interval with `.Years += val`.
func (self Interval) AddYears(val int) Interval {
self.Years += val
return self
}
// Returns a version of this interval with `.Months += val`.
func (self Interval) AddMonths(val int) Interval {
self.Months += val
return self
}
// Returns a version of this interval with `.Days += val`.
func (self Interval) AddDays(val int) Interval {
self.Days += val
return self
}
// Returns a version of this interval with `.Hours += val`.
func (self Interval) AddHours(val int) Interval {
self.Hours += val
return self
}
// Returns a version of this interval with `.Minutes += val`.
func (self Interval) AddMinutes(val int) Interval {
self.Minutes += val
return self
}
// Returns a version of this interval with `.Seconds += val`.
func (self Interval) AddSeconds(val int) Interval {
self.Seconds += val
return self
}
/*
Adds every field of one interval to every field of another interval, returning
the sum. Does NOT convert different time units, such as seconds to minutes or
vice versa.
*/
func (self Interval) Add(val Interval) Interval {
return Interval{
Years: self.Years + val.Years,
Months: self.Months + val.Months,
Days: self.Days + val.Days,
Hours: self.Hours + val.Hours,
Minutes: self.Minutes + val.Minutes,
Seconds: self.Seconds + val.Seconds,
}
}
/*
Subtracts every field of one interval from every corresponding field of another
interval, returning the difference. Does NOT convert different time units, such
as seconds to minutes or vice versa.
*/
func (self Interval) Sub(val Interval) Interval {
return self.Add(val.Neg())
}
/*
Returns a version of this interval with every field inverted: positive fields
become negative, and negative fields become positive.
*/
func (self Interval) Neg() Interval {
return Interval{
Years: -self.Years,
Months: -self.Months,
Days: -self.Days,
Hours: -self.Hours,
Minutes: -self.Minutes,
Seconds: -self.Seconds,
}
}
func (self Interval) bufLen() (num int) {
if self.IsZero() {
return len(zeroInterval)
}
num += len(`P`)
addIntervalPartLen(&num, self.Years)
addIntervalPartLen(&num, self.Months)
addIntervalPartLen(&num, self.Days)
if self.HasTime() {
num += len(`T`)
}
addIntervalPartLen(&num, self.Hours)
addIntervalPartLen(&num, self.Minutes)
addIntervalPartLen(&num, self.Seconds)
return
}