-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.go
331 lines (279 loc) · 8.34 KB
/
date.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
package timex
import (
"errors"
"fmt"
"math"
"time"
)
// Date represents a specific day in Gregorian calendar.
//
// The zero value of type Date is January 1 of year 1.
type Date struct {
ordinal int // ordinal represents days since January 1 of year 1.
}
// NewDate returns the date corresponding to year, month, and day.
func NewDate(year, month, day int) (Date, error) {
if month < 1 || month > 12 {
return Date{}, errors.New("month is out of range [1,12]")
}
if days := daysInMonth(year, month); day < 1 || day > days {
return Date{}, fmt.Errorf("day is out of range [1,%d]", days)
}
n := calendarToOrdinal(year, month, day)
return Date{ordinal: n}, nil
}
// MustNewDate is like NewDate but panics if the date cannot be created.
func MustNewDate(year, month, day int) Date {
date, err := NewDate(year, month, day)
if err != nil {
panic(`timex: NewDate: ` + err.Error())
}
return date
}
// DateFromOrdinalDate returns the date corresponding to year, day of year.
func DateFromOrdinalDate(year, dayOfYear int) (Date, error) {
days := 365
if isLeap(year) {
days++
}
if dayOfYear < 1 || dayOfYear > days {
return Date{}, fmt.Errorf("day of year is out of range [1,%d]", days)
}
n := ordinalDateToOrdinal(year, dayOfYear)
return Date{ordinal: n}, nil
}
// MustDateFromOrdinalDate is like DateFromOrdinalDate but panics if the date cannot be created.
func MustDateFromOrdinalDate(year, dayOfYear int) Date {
date, err := DateFromOrdinalDate(year, dayOfYear)
if err != nil {
panic(`timex: DateFromOrdinalDate: ` + err.Error())
}
return date
}
// DateFromTime returns the date specified by t.
func DateFromTime(t time.Time) Date {
year, month, day := t.Date()
n := calendarToOrdinal(year, int(month), day)
return Date{ordinal: n}
}
// Today returns the current date in the given location.
func Today(location *time.Location) Date {
t := time.Now().In(location)
return DateFromTime(t)
}
// Time returns the time.Time specified by d in the given location.
func (d Date) Time(location *time.Location) time.Time {
year, month, day := ordinalToCalendar(d.ordinal)
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, location)
}
// OrdinalDate returns the ordinal date specified by d.
func (d Date) OrdinalDate() (year, dayOfYear int) {
return ordinalToOrdinalDate(d.ordinal)
}
// Date returns the year, month and day specified by d.
func (d Date) Date() (year, month, day int) {
year, month, day = ordinalToCalendar(d.ordinal)
return
}
// Year returns the year specified by d.
func (d Date) Year() int {
year, _ := ordinalToOrdinalDate(d.ordinal)
return year
}
// Quarter returns the quarter specified by d.
func (d Date) Quarter() int {
_, month, _ := ordinalToCalendar(d.ordinal)
return (month-1)/3 + 1
}
// Month returns the month specified by d.
func (d Date) Month() int {
_, month, _ := ordinalToCalendar(d.ordinal)
return month
}
// Day returns the day of month specified by d.
func (d Date) Day() int {
_, _, day := ordinalToCalendar(d.ordinal)
return day
}
// DayOfYear returns the day of year specified by d.
func (d Date) DayOfYear() int {
_, dayOfYear := ordinalToOrdinalDate(d.ordinal)
return dayOfYear
}
// Weekday returns the day of week specified by d.
func (d Date) Weekday() time.Weekday {
weekday := (d.ordinal + 1) % 7 // Day 0 is monday.
if weekday < 0 {
weekday += 7
}
return time.Weekday(weekday)
}
// ISOWeek returns the ISO 8601 year and week number specified by d.
func (d Date) ISOWeek() (year, week int) {
delta := int(time.Thursday - d.Weekday())
if delta == 4 { // Sunday
delta = -3
}
thursday := d.ordinal + delta
year, dayOfYear := ordinalToOrdinalDate(thursday)
return year, (dayOfYear-1)/7 + 1
}
// norm1 normalize the hi and lo into [1, base].
func norm1(hi, lo, base int) (int, int) {
if lo < 1 {
n := -(lo/base - 1)
lo += n * base
hi -= n
}
if lo > base {
n := (lo - 1) / base
lo -= n * base
hi += n
}
return hi, lo
}
// Add returns the date corresponding to adding the given number of years, months, and days to d.
func (d Date) Add(years, months, days int) Date {
year, month, day := ordinalToCalendar(d.ordinal)
year += years
month += months
day += days
year, month = norm1(year, month, 12)
n := ordinalBeforeYear(year)
n += daysBeforeMonth(year, month)
n += day
return Date{ordinal: n}
}
// AddDays returns the date corresponding to adding the given number of days to d.
func (d Date) AddDays(days int) Date {
return Date{ordinal: d.ordinal + days}
}
// Sub returns the days d-dd.
// If the result exceeds the integer scope, the maximum (or minimum) integer will be returned.
func (d Date) Sub(dd Date) int {
days := d.ordinal - dd.ordinal
switch {
case d.ordinal >= 0 && dd.ordinal <= 0 && days < 0:
return math.MaxInt
case d.ordinal <= 0 && dd.ordinal >= 0 && days > 0:
return math.MinInt
default:
return days
}
}
// IsZero reports whether the date d is the zero value, January 1 of year 1.
func (d Date) IsZero() bool {
return d.ordinal == 0
}
// Before reports whether the date d is before dd.
func (d Date) Before(dd Date) bool {
return d.ordinal < dd.ordinal
}
// After reports whether the date d is after dd.
func (d Date) After(dd Date) bool {
return d.ordinal > dd.ordinal
}
// Equal reports whether the date d and dd is the same date.
func (d Date) Equal(dd Date) bool {
return d.ordinal == dd.ordinal
}
// daysInYear[month] counts the number of days in a non-leap year when the month ends.
var daysInYear = [...]int32{
0,
31,
31 + 28,
31 + 28 + 31,
31 + 28 + 31 + 30,
31 + 28 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31,
}
func isLeap(year int) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}
// daysInMonth returns the number of days in the specified month.
func daysInMonth(year, month int) int {
if month == 2 && isLeap(year) {
return 29
}
return int(daysInYear[month] - daysInYear[month-1])
}
// daysBeforeMonth returns the number of days in the year before specified month.
func daysBeforeMonth(year, month int) int {
days := int(daysInYear[month-1])
if month > 2 && isLeap(year) {
days++
}
return days
}
var (
daysEvery400Years = ordinalBeforeYear(401) + 1
daysEvery100Years = ordinalBeforeYear(101) + 1
daysEvery4Years = ordinalBeforeYear(5) + 1
)
// ordinalBeforeYear returns the ordinal of last year's last day.
// Ordinal day 0 is January 1 of year 1.
func ordinalBeforeYear(year int) int {
var delta int
if year > 0 {
y := year - 1 // If year is 5, delta reach 1.
delta = y/4 - y/100 + y/400
} else {
y := year // If year is -4, delta reach -1.
delta = y/4 - y/100 + y/400 - 1 // Handle year 0, it is a leap year.
}
return (year-1)*365 + delta - 1 // Shift for January 1 of year 1.
}
func ordinalDateToOrdinal(year, dayOfYear int) int {
return ordinalBeforeYear(year) + dayOfYear
}
func calendarToOrdinal(year, month, day int) int {
dayOfYear := daysBeforeMonth(year, month) + day
return ordinalDateToOrdinal(year, dayOfYear)
}
func ordinalToOrdinalDate(n int) (year, dayOfYear int) {
n400, n := norm1(0, n, daysEvery400Years)
n400, n = norm1(n400, n+1, daysEvery400Years) // Shift for January 1 of year 1, prevent integer overflow.
year = n400*400 + 1 // Start from year 1.
n100 := (n - 1) / daysEvery100Years
n100 -= n100 >> 2 // Handle the leap day every 400 years. If n100 is 4, set it to 3.
year += n100 * 100
n -= daysEvery100Years * n100
n4 := (n - 1) / daysEvery4Years
year += n4 * 4
n -= daysEvery4Years * n4
n1 := (n - 1) / 365
n1 -= n1 >> 2 // Handle the leap day every 4 years. If n1 is 4, set it to 3.
year += n1
n -= 365 * n1
return year, n
}
func ordinalDateToCalendar(year, dayOfYear int) (int, int, int) {
day := dayOfYear
if isLeap(year) {
switch {
case day == 31+29:
return year, 2, 29 // Leap day.
case day > 31+29:
day-- // Remove leap day.
}
}
month := (dayOfYear-1)/31 + 1
days := int(daysInYear[month])
if day > days {
month++
day -= days
} else {
day -= int(daysInYear[month-1])
}
return year, month, day
}
func ordinalToCalendar(n int) (year, month, day int) {
year, dayOfYear := ordinalToOrdinalDate(n)
return ordinalDateToCalendar(year, dayOfYear)
}