-
Notifications
You must be signed in to change notification settings - Fork 40
/
time.go
311 lines (276 loc) · 7.74 KB
/
time.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
// Copyright 2023 Sneller, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package date
import (
"time"
)
// A Time represents a date and time with a nanosecond
// component. This representation allows for faster
// extraction of date components than time.Time, at the
// cost of slower conversion to Unix times.
//
// This representation cannot store years below 0 or
// above 16,383. Years falling outside that range will
// be truncated to fit within that range.
type Time struct {
ts uint64
ns uint32
}
// Parse parses a date string from data
// and returns the associated time and true,
// or the zero time value and false if the buffer
// did not contain a recognzied date format.
//
// Parse attempts to recognize strings
// that (approximately) match RFC3339 timestamps
// with optional nanosecond precision and timezone/offset
// components. Parse will automatically ignore leading
// and trailing whitespace as long as the middle characters
// of data are unambiguously a timestamp.
func Parse(data []byte) (Time, bool) {
year, month, day, hour, min, sec, ns, ok := parseDate(data)
if !ok {
return Time{}, false
}
return Date(year, month, day, hour, min, sec, ns), true
}
// Date constructs a Time from components. Values of
// month, day, hour, min, sec, and ns outside their
// usual ranges will be normalized. Values for year
// outside of the range [0, 16383] will be truncated to
// fit within that range.
func Date(year, month, day, hour, min, sec, ns int) Time {
sec, ns = norm(sec, ns, 1e9)
min, sec = norm(min, sec, 60)
hour, min = norm(hour, min, 60)
day, hour = norm(day, hour, 24)
year, month, day = normdate(year, month, day)
return date(year, month, day, hour, min, sec, ns)
}
func date(year, month, day, hour, min, sec, ns int) Time {
if year < 0 {
year = 0
} else if year > (1<<14)-1 {
year = (1 << 14) - 1
}
ts := (uint64(year) & 0xffff << 40) |
(uint64(month-1) & 0xff << 32) |
(uint64(day-1) & 0xff << 24) |
(uint64(hour) & 0xff << 16) |
(uint64(min) & 0xff << 8) |
(uint64(sec) & 0xff)
return Time{ts: ts, ns: uint32(ns)}
}
// FromTime returns a Time equivalent to t.
func FromTime(t time.Time) Time {
t = t.UTC()
year, month, day := t.Year(), int(t.Month()), t.Day()
hour, min, sec := t.Hour(), t.Minute(), t.Second()
ns := t.Nanosecond()
return date(year, month, day, hour, min, sec, ns)
}
// Now returns the current time.
func Now() Time {
return FromTime(time.Now())
}
// Unix returns a Time from the given Unix time in
// seconds and nanoseconds.
func Unix(sec, ns int64) Time {
return FromTime(time.Unix(sec, ns))
}
// UnixMicro returns a Time from the given Unix time in
// microseconds.
func UnixMicro(us int64) Time {
return FromTime(time.UnixMicro(us))
}
// Time returns t as a time.Time.
func (t Time) Time() time.Time {
year, month, day := t.Year(), time.Month(t.Month()), t.Day()
hour, min, sec := t.Hour(), t.Minute(), t.Second()
return time.Date(year, month, day, hour, min, sec, int(t.ns), time.UTC)
}
// Year returns the year component of t.
func (t Time) Year() int {
return int(t.ts & 0xffff0000000000 >> 40)
}
func (t Time) Quarter() int {
return (t.Month()-1)/3 + 1
}
// Month returns the month component of t.
func (t Time) Month() int {
return int(t.ts&0xff00000000>>32) + 1
}
// Day returns the day component of t.
func (t Time) Day() int {
return int(t.ts&0xff000000>>24) + 1
}
// Hour returns the hour component of t.
func (t Time) Hour() int {
return int(t.ts & 0xff0000 >> 16)
}
// Minute returns the minute component of t.
func (t Time) Minute() int {
return int(t.ts & 0xff00 >> 8)
}
// Second returns the second component of t.
func (t Time) Second() int {
return int(t.ts & 0xff)
}
// Nanosecond returns the nanosecond component of t.
func (t Time) Nanosecond() int {
return int(t.ns)
}
// Unix returns t as the number of seconds since the
// Unix epoch.
func (t Time) Unix() int64 {
return t.Time().Unix()
}
// UnixMicro returns t as the number of microseconds since
// the Unix epoch.
func (t Time) UnixMicro() int64 {
return t.Time().UnixMicro()
}
// UnixNano returns t as the number of nanoseconds since
// the Unix epoch.
func (t Time) UnixNano() int64 {
return t.Time().UnixNano()
}
// Equal returns whether t == t2.
func (t Time) Equal(t2 Time) bool {
return t == t2
}
// Before returns whether t is before t2.
func (t Time) Before(t2 Time) bool {
return t.ts < t2.ts || (t.ts == t2.ts && t.ns < t2.ns)
}
// After returns whether t is after t2.
func (t Time) After(t2 Time) bool {
return t.ts > t2.ts || (t.ts == t2.ts && t.ns > t2.ns)
}
// IsZero returns whether t is the zero value,
// corresponding to January 1st of year zero.
func (t Time) IsZero() bool {
return t == Time{}
}
// AppendRFC3339Nano appends t formatted as an RFC3339
// compliant string (including nanoseconds) to b. This
// method is functionally equivalent to:
//
// t.Time().AppendFormat(b, time.RFC3339Nano)
func (t Time) AppendRFC3339Nano(b []byte) []byte {
year, month, day := t.Year(), t.Month(), t.Day()
hour, min, sec := t.Hour(), t.Minute(), t.Second()
ns := int(t.ns)
lenmin := len("0000-00-00T00:00:00Z")
lenmax := lenmin
if ns != 0 {
lenmin += len(".1")
lenmax += len(".999999999")
}
if len(b)+lenmin > cap(b) {
b2 := make([]byte, len(b), len(b)+lenmax)
copy(b2, b)
b = b2
}
b = appendInt(b, year, 4, false)
b = append(b, '-')
b = appendInt(b, month, 2, false)
b = append(b, '-')
b = appendInt(b, day, 2, false)
b = append(b, 'T')
b = appendInt(b, hour, 2, false)
b = append(b, ':')
b = appendInt(b, min, 2, false)
b = append(b, ':')
b = appendInt(b, sec, 2, false)
if ns != 0 {
b = append(b, '.')
b = appendInt(b, ns, 9, true)
}
b = append(b, 'Z')
return b
}
// Add adds d to t.
func (t Time) Add(d time.Duration) Time {
return FromTime(t.Time().Add(d))
}
// Round rounds t to the nearest multiple of d.
func (t Time) Round(d time.Duration) Time {
return FromTime(t.Time().Round(d))
}
// Truncate rounds t down to a multiple of d.
func (t Time) Truncate(d time.Duration) Time {
return FromTime(t.Time().Truncate(d))
}
// String implements io.Stringer. The returned string
// is meant to be used for debugging purposes.
func (t Time) String() string {
year, month, day := t.Year(), t.Month(), t.Day()
hour, min, sec := t.Hour(), t.Minute(), t.Second()
ns := int(t.ns)
n := len("0000-00-00 00:00:00 +0000 UTC")
if ns != 0 {
n += len(".999999999")
}
b := make([]byte, 0, n)
b = appendInt(b, year, 4, false)
b = append(b, '-')
b = appendInt(b, month, 2, false)
b = append(b, '-')
b = appendInt(b, day, 2, false)
b = append(b, ' ')
b = appendInt(b, hour, 2, false)
b = append(b, ':')
b = appendInt(b, min, 2, false)
b = append(b, ':')
b = appendInt(b, sec, 2, false)
if ns != 0 {
b = append(b, '.')
b = appendInt(b, ns, 9, true)
}
b = append(b, " +0000 UTC"...)
return string(b)
}
var monthdays = [12]int{
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
}
func daysin(y, m int) int {
d := monthdays[m-1]
if m == 2 && isleap(y) {
d++
}
return d
}
func normdate(y, m, d int) (year, month, day int) {
y, m = norm(y, m-1, 12)
m++
md := daysin(y, m)
if d >= 1 && d <= md {
return y, m, d
}
for d < 1 {
if m--; m < 1 {
y, m = y-1, 12
}
md = daysin(y, m)
d += md
}
for ; d > md; md = daysin(y, m) {
d -= md
if m++; m > 12 {
y, m = y+1, 1
}
}
return y, m, d
}