-
Notifications
You must be signed in to change notification settings - Fork 1
/
gregorian.go
executable file
·316 lines (293 loc) · 5.98 KB
/
gregorian.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
package tai
// maxint64 / seconds per year = 292277024626
// 292,277,024,626
// year 292 billion is when this becomes invalid
// (perfectly fine)
const (
January = iota + 1
February
March
April
May
June
July
August
September
October
November
December
)
const (
Monday = iota
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
)
var (
daysPerNonLeapMonth = [...]int{
0,
31,
28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
}
daysPerLeapMonth = [...]int{
0,
31,
29,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
}
daysBeforeNonLeapMonth = [...]int{
0, // not a month
0, // January
31, // February
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,
}
monthNamesFull = [...]string{
"not a month",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}
monthNamesAbbrev = [...]string{
"NaM",
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sept",
"Oct",
"Nov",
"Dec",
}
weekdayNames = [...]string{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
}
weekdayNamesAbbrev = [...]string{
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
}
)
const (
eraYears = 400
eraYearsm1 = eraYears - 1
epochDays = 719468 - 4383 // 719468 == Jan 1 1970 from 0000 Mar 1
yearDays = 365
eraDays = 146097
eraDaysm1 = eraDays - 1
)
// IsLeapYear returns true if year is a leap year, false if
// year is not a leap year.
func IsLeapYear(year int) bool {
/* per USNO,
Every year that is exactly divisible by four is a leap year,
except for years that are exactly divisible by 100,
but these centurial years are leap years if they are exactly divisible by 400.
For example, the years 1700, 1800, and 1900 are not leap years,
but the years 1600 and 2000 are.
*/
if year%4 == 0 { // every year that is exactly divisible by four
if year%100 == 0 { // except for years that are exactly divisible by 100
return year%400 == 0 // if they are exactly divisible by 400
}
return true
}
return false
}
/*
The functions:
DaysFromCivil
CivilFromDays
WeekdayFromDays
WeekdayDifference
NextWeekday
PrevWeekday
DaysFromSecsEpoch
SecsEpochFromDays
are adapted from Howard Hinnant's public domain algorithms
https://howardhinnant.github.io/date_algorithms.html
Thank you, Howard!
*/
// DaysFromCivil returns the number of days in the Gregorian calendar since
// Jan 1, 1958 from a year, month, and day
func DaysFromCivil(y, m, d int) int {
if m <= 2 {
y--
}
var era, doy int
if y >= 0 {
era = y / eraYears
} else {
era = (y - eraYearsm1) / eraYears
}
yoe := y - era*eraYears
if m > 2 {
m -= 3
} else {
m += 9
}
doy = (153*m+2)/5 + d - 1
doe := yoe*yearDays + yoe/4 - yoe/100 + doy
return era*eraDays + doe - epochDays
}
// CivilFromDays converts the number of days in the internal representation
// to a day in the civil (Gregorian) calendar
func CivilFromDays(days int) (y, m, d int) {
days += epochDays
var era, doe, yoe int
if days >= 0 {
era = days
} else {
era = days - eraDaysm1
}
era /= eraDays
doe = days - era*eraDays
yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365
y = yoe + era*eraYears
doy := doe - (365*yoe + yoe/4 - yoe/100)
mp := (5*doy + 2) / 153
d = doy - (153*mp+2)/5 + 1
if mp < 10 {
m = mp + 3
} else {
m = mp - 9
}
if m <= 2 {
y++
}
return
}
// WeekFromDays returns the weekday number in the common programming,
// ISO-incompatible notation where 0 == sunday, 6 == sat; not ISO (0 == monday)
func WeekdayFromDays(days int) int {
if days >= -4 {
return (days + 4) % 7
}
return (days+5)%7 + 6
}
// WeekdayDifference computes the number of days between weekday d1, d2.
//
// d1,d2 are in the range [0,6]
func WeekdayDifference(d1, d2 int) int {
d1 -= d2
if d1 <= 6 {
return d1
}
return d1 + 7
}
// NextWeekday returns the next weekday number after wd
func NextWeekday(wd int) int {
if wd < 6 {
return wd + 1
}
return 0
}
// PrevWeekday returns the weekday number proceeding wd
func PrevWeekday(wd int) int {
if wd > 0 {
return wd - 1
}
return 6
}
// DaysFromSecsEpoch returns the number of days in the internal representation
// since the epoch in seconds
func DaysFromSecsEpoch(secs int64) int {
return int(secs / Day)
}
func SecsEpochFromDays(days int) int64 {
return int64(days) * Day
}
// DaysInMonth returns the number of days in the given month and year
func DaysInMonth(m, y int) int {
ily := IsLeapYear(y)
if ily {
return daysPerLeapMonth[m]
}
return daysPerNonLeapMonth[m]
}
// Gregorian represents a moment in the Proleptic Gregorian Calendar and the TAI time system
type Gregorian struct {
Asec int64
Year int
Month int
Day int
Hour int
Min int
Sec int
}
// Before returns true if g is before o
func (g Gregorian) Before(o Gregorian) bool {
t1 := FromGregorian(g)
t2 := FromGregorian(o)
return t1.Before(t2)
}
// After returns true if g is after o
func (g Gregorian) After(o Gregorian) bool {
t1 := FromGregorian(g)
t2 := FromGregorian(o)
return t1.After(t2)
}
// Eq returns true if g and o represent the same instant in time
func (g Gregorian) Eq(o Gregorian) bool {
return (g.Asec == o.Asec &&
g.Year == o.Year &&
g.Month == o.Month &&
g.Day == o.Day &&
g.Hour == o.Hour &&
g.Min == o.Min &&
g.Sec == o.Sec)
}