-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.go
287 lines (246 loc) · 7.9 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
package calendar
import (
"cmp"
"fmt"
"github.com/Jumpaku/go-assert"
"github.com/Jumpaku/tokiope/internal/exact"
)
// Date . The zero value represents 0001-01-01
type Date struct {
days int64
}
var _ interface {
UnixDay() int64
YMD() (year int, month Month, dayOfMonth int)
YWD() (year int, week int, dayOfWeek DayOfWeek)
YD() (year int, dayOfYear int)
Add(days int) Date
Sub(days int) Date
Year() Year
YearMonth() YearMonth
YearWeek() YearWeek
DaysUntil(endExclusive Date) int64
Cmp(other Date) int
Equal(other Date) bool
Before(other Date) bool
After(other Date) bool
String() string
} = Date{}
func DateOfYMD(year int, month Month, dayOfMonth int) Date {
assert.Params(MonthJanuary <= month && month <= MonthDecember, "month must be in [%d, %d]: %d", MonthJanuary, MonthDecember, month)
lastDayOfMonth := YearMonthOf(year, month).Days()
assert.Params(1 <= dayOfMonth && dayOfMonth <= lastDayOfMonth, "dayOfMonth must be in [%d, %d]: %d", 1, lastDayOfMonth, dayOfMonth)
return Date{days: daysFromYMD(year, month, dayOfMonth)}
}
func DateOfYWD(year int, week int, dayOfWeek DayOfWeek) Date {
assert.Params(DayOfWeekMonday <= dayOfWeek && dayOfWeek <= DayOfWeekSunday, "dayOfWeek must be in [%d, %d]: %d", DayOfWeekMonday, DayOfWeekSunday, dayOfWeek)
assert.Params(1 <= week && week <= 53, "week must be in [%d, %d]: %d", 1, 53, week)
days := daysFromYWD(year, week, dayOfWeek)
if week == 53 {
assert.Params(days < daysFromYWD(year+1, 1, dayOfWeek),
"week must be in [%d, %d]: %d", 1, 52, week)
}
return Date{days: days}
}
func DateOfYD(year int, dayOfYear int) Date {
lastDayOfYear := Year(year).Days()
assert.Params(1 <= dayOfYear && dayOfYear <= lastDayOfYear, "day must be in [%d, %d]: %d", 1, lastDayOfYear, dayOfYear)
return Date{days: daysFromYD(year, dayOfYear)}
}
func UnixDay(epochDays int64) Date {
return Date{days: epochDays + days0000To1970 - 366}
}
func (d Date) UnixDay() int64 {
return toEpochDays(d.YMD())
}
func (d Date) YMD() (year int, month Month, day int) {
return daysToYMD(d.days)
}
func (d Date) YWD() (year int, week int, dayOfWeek DayOfWeek) {
return daysToYWD(d.days)
}
func (d Date) YD() (year int, dayOfYear int) {
return daysToYD(d.days)
}
func (d Date) Year() Year {
y, _ := d.YD()
return Year(y)
}
func (d Date) YearMonth() YearMonth {
y, m, _ := d.YMD()
return YearMonthOf(y, m)
}
func (d Date) YearWeek() YearWeek {
y, w, _ := d.YWD()
return YearWeekOf(y, w)
}
func (d Date) Cmp(other Date) int {
return cmp.Compare(d.days, other.days)
}
func (d Date) Equal(other Date) bool {
return d.days == other.days
}
func (d Date) Before(other Date) bool {
return d.days < other.days
}
func (d Date) After(other Date) bool {
return d.days > other.days
}
func (d Date) Add(days int) Date {
return Date{d.days + int64(days)}
}
func (d Date) Sub(days int) Date {
return Date{d.days - int64(days)}
}
func (d Date) DaysUntil(endExclusive Date) int64 {
return endExclusive.days - d.days
}
func (d Date) String() string {
return FormatDate(d, DateFormatYMD)
}
func toEpochDays(year int, month Month, day int) int64 {
y := int64(year)
m := int64(month)
d := int64(day)
var total int64 = 0
total += 365 * y
if y >= 0 {
total += (y+3)/4 - (y+99)/100 + (y+399)/400
} else {
total -= y/-4 - y/-100 + y/-400
}
total += (367*m - 362) / 12
total += d - 1
if m > 2 {
total--
if !Year(year).IsLeap() {
total--
}
}
return total - days0000To1970
}
func fromEpochDays(epochDays int64) (int, Month, int) {
var zeroDay = epochDays + days0000To1970
// find the march-based year
zeroDay -= 60 // adjust to 0000-03-01 so leap day is at end of four years cycle
var adjust int64 = 0
if zeroDay < 0 { // adjust negative years to positive for calculation
adjustCycles := (zeroDay+1)/daysPerCycle - 1
adjust = adjustCycles * 400
zeroDay += -adjustCycles * daysPerCycle
}
var yearEst = (400*zeroDay + 591) / daysPerCycle
var doyEst = zeroDay - (365*yearEst + yearEst/4 - yearEst/100 + yearEst/400)
if doyEst < 0 { // fix estimate
yearEst--
doyEst = zeroDay - (365*yearEst + yearEst/4 - yearEst/100 + yearEst/400)
}
yearEst += adjust // reset any negative year
marchDoy0 := doyEst
// convert march-based values back to january-based
marchMonth0 := (marchDoy0*5 + 2) / 153
month := (marchMonth0+2)%12 + 1
dom := marchDoy0 - (marchMonth0*306+5)/10 + 1
yearEst += marchMonth0 / 10
return int(yearEst), Month(month), int(dom)
}
func daysToYMD(days int64) (year int, month Month, day int) {
return fromEpochDays(days - days0000To1970 + 366)
}
func firstMondayIn(year int) int64 {
jan4 := daysFromYMD(year, MonthJanuary, 4)
_, dow, _ := exact.DivFloor(jan4-firstMondayIn2000, 7)
return jan4 - dow
}
func daysToYWD(days int64) (year int, week int, dayOfWeek DayOfWeek) {
year, _, _ = daysToYMD(days)
firstMonday := firstMondayIn(year)
if days < firstMonday {
year--
firstMonday = firstMondayIn(year)
} else if nextMonday := firstMondayIn(year + 1); days >= nextMonday {
year++
firstMonday = nextMonday
}
week = int((days-firstMonday)/7) + 1
dayOfWeek = DayOfWeek((days-firstMonday)%7) + 1
return year, week, dayOfWeek
}
var yearDays = []int{
0,
0 + 31,
0 + 31 + 28,
0 + 31 + 28 + 31,
0 + 31 + 28 + 31 + 30,
0 + 31 + 28 + 31 + 30 + 31,
0 + 31 + 28 + 31 + 30 + 31 + 30,
0 + 31 + 28 + 31 + 30 + 31 + 30 + 31,
0 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
0 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
0 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
0 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
0 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31,
}
func daysToYD(days int64) (year int, day int) {
y, m, d := daysToYMD(days)
leap := 0
if Year(y).IsLeap() && m > 2 {
leap = 1
}
return y, yearDays[m-1] + d + leap
}
func daysFromYMD(year int, month Month, day int) (days int64) {
return toEpochDays(year, month, day) + days0000To1970 - 366
}
func daysFromYWD(year int, week int, dayOfWeek DayOfWeek) (days int64) {
firstMonday := firstMondayIn(year)
return firstMonday + 7*int64(week-1) + int64(dayOfWeek-1)
}
func daysFromYD(year int, dayOfYear int) (days int64) {
return daysFromYMD(year, MonthJanuary, 1) - 1 + int64(dayOfYear)
}
func validateYMD(year, month, dayOfMonth int) error {
//if YearMin <= Year(year) && Year(year) <= YearMax {
// return fmt.Errorf("year must be in [%d, %d]: %d", YearMin, YearMax, year)
//}
if !(MonthJanuary <= Month(month) && Month(month) <= MonthDecember) {
return fmt.Errorf("month must be in [%d, %d]: %d", MonthJanuary, MonthDecember, month)
}
lastDayOfMonth := YearMonthOf(year, Month(month)).Days()
if !(1 <= dayOfMonth && dayOfMonth <= lastDayOfMonth) {
return fmt.Errorf("day of month must be in [%d, %d]: %d", 1, lastDayOfMonth, dayOfMonth)
}
return nil
}
func validateYWD(year, week, dayOfWeek int) error {
//if YearMin <= Year(year) && Year(year) <= YearMax {
// return fmt.Errorf("year must be in [%d, %d]: %d", YearMin, YearMax, year)
//}
if !Year(year).ContainsWeek(week) {
return fmt.Errorf("month must be in [%d, %d]: %d", 1, Year(year).Weeks(), week)
}
if !(DayOfWeekMonday <= DayOfWeek(dayOfWeek) && DayOfWeek(dayOfWeek) <= DayOfWeekSunday) {
return fmt.Errorf("day of month must be in [%d, %d]: %d", DayOfWeekMonday, DayOfWeekSunday, dayOfWeek)
}
return nil
}
func validateYD(year, dayOfYear int) error {
//if YearMin <= Year(year) && Year(year) <= YearMax {
// return fmt.Errorf("year must be in [%d, %d]: %d", YearMin, YearMax, year)
//}
lastDayOfYear := Year(year).Days()
if !(1 <= dayOfYear && dayOfYear <= lastDayOfYear) {
return fmt.Errorf("month must be in [%d, %d]: %d", 1, lastDayOfYear, dayOfYear)
}
return nil
}
const (
// The number of days in a 400 years cycle.
daysPerCycle = 146097
// The number of days from year zero to year 1970.
// There are five 400 year cycles from year zero to 2000.
// There are 7 leap years from 1970 to 2000.
days0000To1970 = daysPerCycle*5 - (30*365 + 7)
// The number of days from year 0001-01-01 to year 2000-01-03.
firstMondayIn2000 = 730121
)