-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.go
580 lines (540 loc) · 15 KB
/
utility.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// Copyright (C) 2021 Alexander Staudt
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package libcalendar
import (
"fmt"
"math"
"sort"
)
// FromAbsoluteToString converts a given absolute (fixed) date to the date
// representation specified in `calendar`.
//
// The list of supported calendar strings is:
// - "gregorian"
// - "iso"
// - "julian"
// - "islamic"
// - "hebrew"
// - "mayanLongCount"
// - "mayanHaab"
// - "mayanTzolkin"
// - "french"
// - "oldHinduSolar"
// - "oldHinduLunar"
//
// For more information about these calendars, see:
//
// Dershowitz, Nachum, and Edward Reingold. 1990. "Calendrical Calculations", Software - Practice and Experience, 20 (9), 899-928. https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.17.4274.
//
// Reingold, Edward, Nachum Dershowitz, and Stewart Clamen. 1993. "Calendrical Calculations, II: Three Historical Calendars", Software - Practice & Experience, 23 (4), 383-404. https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.13.9215.
// FromAbsolute returns a string-formatted calendar date from a given absolute
// date and calendar name.
func FromAbsolute(absoluteDate float64, calendar string) string {
switch calendar {
case "gregorian":
return fmt.Sprint(GregorianFromAbsolute(absoluteDate))
case "julian":
return fmt.Sprint(JulianFromAbsolute(absoluteDate))
case "iso":
return fmt.Sprint(IsoFromAbsolute(absoluteDate))
case "islamic":
return fmt.Sprint(IslamicFromAbsolute(absoluteDate))
case "hebrew":
return fmt.Sprint(HebrewFromAbsolute(absoluteDate))
case "mayanLongCount":
return fmt.Sprint(MayanLongCountFromAbsolute(absoluteDate))
case "mayanHaab":
return fmt.Sprint(MayanHaabFromAbsolute(absoluteDate))
case "mayanTzolkin":
return fmt.Sprint(MayanTzolkinFromAbsolute(absoluteDate))
case "french":
return fmt.Sprint(FrenchFromAbsolute(absoluteDate))
case "oldHinduSolar":
return fmt.Sprint(OldHinduSolarFromAbsolute(absoluteDate))
case "oldHinduLunar":
return fmt.Sprint(OldHinduLunarFromAbsolute(absoluteDate))
default:
return ""
}
}
// Utilities for a more generic approach to converting dates
// Date represents a generic container for dates, holding information about
// arbitrary dates. Possible calendar names are:
type Date struct {
Calendar string `json:"calendar"` // calendar name, e.g. "gregorian"
Components []float64 `json:"components"` // e.g. []float64{2022, 05, 28}
ComponentNames []string `json:"componentNames"` // e.g. []string{"year", "month", "day"}
MonthNames []string `json:"monthNames"` // e.g. []string{"January", ..., "December"}
}
// jsonFromNumSlice returns a JSON array from a slice of float64
func jsonFromNumSlice(s []float64) string {
n := len(s)
switch {
case n == 1:
return fmt.Sprint(s[0])
case n > 1:
elems := fmt.Sprint(s[0])
for i := 1; i < n; i++ {
elems = elems + "," + fmt.Sprint(s[i])
}
return fmt.Sprintf("[%s]", elems)
default: // empty array, i.e. n == 0:
return "[]"
}
}
// jsonFromStringSlice returns a JSON array from a slice of strings
func jsonFromStringSlice(s []string) string {
n := len(s)
switch {
case n == 1:
return fmt.Sprint(s[0])
case n > 1:
elems := fmt.Sprintf("\"%s\"", s[0])
for i := 1; i < n; i++ {
elems = elems + "," + fmt.Sprintf("\"%s\"", s[i])
}
return fmt.Sprintf("[%s]", elems)
default: // empty array, i.e. n == 0:
return "[]"
}
}
// Json serializes the receiver into a JSON-formatted string.
func (d Date) Json() string {
calendar :=
fmt.Sprintf("\"calendar\":\"%s\"", d.Calendar)
components :=
fmt.Sprintf("\"components\":%s", jsonFromNumSlice(d.Components))
componentNames :=
fmt.Sprintf("\"componentNames\":%s", jsonFromStringSlice(d.ComponentNames))
monthNames :=
fmt.Sprintf("\"monthNames\":%s", jsonFromStringSlice(d.MonthNames))
return fmt.Sprintf(
"{%s,%s,%s,%s}",
calendar,
components,
componentNames,
monthNames,
)
}
// String creates a string representation of its receiver.
//
// This function reuses the String() method of GregorianDate{} etc. by
// internally converting the given Date struct into a GregorianDate{},
// IsoDate{} or other appropriate struct.
func (d Date) String() string {
switch d.Calendar {
case "gregorian":
return fmt.Sprint(gregorianFromDate(d))
case "iso":
return fmt.Sprint(isoFromDate(d))
case "julian":
return fmt.Sprint(gregorianFromDate(d))
case "islamic":
return fmt.Sprint(islamicFromDate(d))
case "hebrew":
return fmt.Sprint(hebrewFromDate(d))
case "mayanLongCount":
return fmt.Sprint(mayanLongCountFromDate(d))
case "mayanHaab":
return fmt.Sprint(mayanHaabFromDate(d))
case "mayanTzolkin":
return fmt.Sprint(mayanTzolkinFromDate(d))
case "french":
return fmt.Sprint(frenchFromDate(d))
case "oldHinduSolar":
return fmt.Sprint(oldHinduSolarFromDate(d))
case "oldHinduLunar":
return fmt.Sprint(oldHinduLunarFromDate(d))
default:
return ""
}
}
// values returns the values of map[float64]string x as a slice of type string, sorted
// by the keys.
func values(x map[float64]string) []string {
values := make([]string, 0, len(x))
for _, key := range keys(x) {
values = append(values, x[key])
}
return values
}
// keys returns the keys of map[float64]string x as a sorted slice of type float64.
func keys(x map[float64]string) []float64 {
keys := make([]float64, 0, len(x))
for key := range x {
keys = append(keys, key)
}
sort.Float64s(keys)
return keys
}
// lastValidDate checks if the day provided in the parameters is valid
// for a given year and month. If the day is valid, returns that day,
// otherwise returns the last valid day of the given year and month.
func LastValidDayOfMonth(year, month, day float64, calendar string) (validDay float64) {
lastValidDay := 0.0
switch calendar {
case "gregorian":
lastValidDay = LastDayOfGregorianMonth(month, year)
case "julian":
lastValidDay = LastDayOfJulianMonth(month, year)
case "islamic":
lastValidDay = LastDayOfIslamicMonth(month, year)
case "hebrew":
lastValidDay = LastDayOfHebrewMonth(month, year)
case "french":
lastValidDay = FrenchLastDayOfMonth(month, year)
default:
lastValidDay = day
}
if day < lastValidDay {
return day
} else {
return lastValidDay
}
}
// Date() creates a Date from its receiver.
func (d GregorianDate) Date() Date {
return Date{
Calendar: "gregorian",
Components: []float64{
d.Year,
d.Month,
d.Day,
},
ComponentNames: []string{
"year", "month", "day",
},
MonthNames: values(gregorianMonths),
}
}
// Date() creates a Date from its receiver.
func (d JulianDate) Date() Date {
return Date{
Calendar: "julian",
Components: []float64{
d.Year,
d.Month,
d.Day,
},
ComponentNames: []string{
"year", "month", "day",
},
MonthNames: values(gregorianMonths),
}
}
// Date() creates a Date from its receiver.
func (d IsoDate) Date() Date {
return Date{
Calendar: "iso",
Components: []float64{
d.Year,
d.Week,
d.Day,
},
ComponentNames: []string{
"year", "week", "day",
},
MonthNames: []string{},
}
}
// Date() creates a Date from its receiver.
func (d IslamicDate) Date() Date {
return Date{
Calendar: "islamic",
Components: []float64{
d.Year,
d.Month,
d.Day,
},
ComponentNames: []string{
"year", "month", "day",
},
MonthNames: values(islamicMonths),
}
}
// Date() creates a Date from its receiver.
func (d HebrewDate) Date() Date {
return Date{
Calendar: "hebrew",
Components: []float64{
d.Year,
d.Month,
d.Day,
},
ComponentNames: []string{
"year", "month", "day",
},
MonthNames: HebrewMonthNames(d),
}
}
// HebrewMonthNames returns the month names for a given Hebrew year, taking
// into account the Hebrew leap year rule.
func HebrewMonthNames(d HebrewDate) []string {
if !HebrewLeapYear(d.Year) {
return values(hebrewMonths)
} else {
months := values(hebrewMonths)
months[11] = "Adar I"
return append(months, "Adar II")
}
}
// Date() creates a Date from its receiver.
func (d MayanLongCount) Date() Date {
return Date{
Calendar: "mayanLongCount",
Components: []float64{
d.Baktun,
d.Katun,
d.Tun,
d.Uinal,
d.Kin,
},
ComponentNames: []string{
"baktun", "katun", "tun", "uinal", "kin",
},
MonthNames: []string{},
}
}
// Date() creates a Date from its receiver.
func (d MayanHaabDate) Date() Date {
return Date{
Calendar: "mayanHaab",
Components: []float64{
d.Day,
d.Month,
},
ComponentNames: []string{
"day", "month",
},
MonthNames: values(mayanHaabMonths),
}
}
// Date() creates a Date from its receiver.
func (d MayanTzolkinDate) Date() Date {
return Date{
Calendar: "mayanTzolkin",
Components: []float64{
d.Number,
d.Name,
},
ComponentNames: []string{
"number", "name",
},
MonthNames: values(mayanTzolkinNames),
}
}
// Date() creates a Date from its receiver.
func (d FrenchDate) Date() Date {
return Date{
Calendar: "french",
Components: []float64{
d.Year,
d.Month,
d.Day,
},
ComponentNames: []string{
"year", "month", "day",
},
MonthNames: values(frenchMonths),
}
}
// Date() creates a Date from its receiver.
func (d OldHinduSolarDate) Date() Date {
return Date{
Calendar: "oldHinduSolar",
Components: []float64{
d.Year,
d.Month,
d.Day,
},
ComponentNames: []string{
"year", "month", "day",
},
MonthNames: values(hinduSolarMonths),
}
}
// Date() creates a Date from its receiver.
func (d OldHinduLunarDate) Date() Date {
return Date{
Calendar: "oldHinduLunar",
Components: []float64{
d.Year,
d.Month,
d.Day,
},
ComponentNames: []string{
"year", "month", "day",
},
MonthNames: values(hinduLunarMonths),
}
}
// gregorianFromDate computes a GregorianDate from a given libcalendar Date.
func gregorianFromDate(d Date) GregorianDate {
return GregorianDate{
Year: d.Components[0],
Month: d.Components[1],
Day: d.Components[2],
}
}
// julianFromDate computes a JulianDate from a given libcalendar Date.
func julianFromDate(d Date) JulianDate {
return JulianDate{
Year: d.Components[0],
Month: d.Components[1],
Day: d.Components[2],
}
}
// isoFromDate computes a IsoDate from a given libcalendar Date.
func isoFromDate(d Date) IsoDate {
return IsoDate{
Year: d.Components[0],
Week: d.Components[1],
Day: d.Components[2],
}
}
// islamicFromDate computes a IslamicDate from a given libcalendar Date.
func islamicFromDate(d Date) IslamicDate {
return IslamicDate{
Year: d.Components[0],
Month: d.Components[1],
Day: d.Components[2],
}
}
// hebrewFromDate computes a HebrewDate from a given libcalendar Date.
func hebrewFromDate(d Date) HebrewDate {
return HebrewDate{
Year: d.Components[0],
Month: d.Components[1],
Day: d.Components[2],
}
}
// mayanLongCountFromDate computes a MayanLongCount from a given
// libcalendar Date.
func mayanLongCountFromDate(d Date) MayanLongCount {
return MayanLongCount{
Baktun: d.Components[0],
Katun: d.Components[1],
Tun: d.Components[2],
Uinal: d.Components[3],
Kin: d.Components[4],
}
}
// mayanHaabFromDate computes a MayanHaabDate from a given libcalendar Date.
func mayanHaabFromDate(d Date) MayanHaabDate {
return MayanHaabDate{
Day: d.Components[0],
Month: d.Components[1],
}
}
// mayanTzolkinFromDate computes a MayanTzolkinDate from a given
// libcalendar Date.
func mayanTzolkinFromDate(d Date) MayanTzolkinDate {
return MayanTzolkinDate{
Number: d.Components[0],
Name: d.Components[1],
}
}
// frenchFromDate computes a FrenchDate from a given libcalendar Date.
func frenchFromDate(d Date) FrenchDate {
return FrenchDate{
Year: d.Components[0],
Month: d.Components[1],
Day: d.Components[2],
}
}
// oldHinduSolarFromDate computes a OldHinduSolarDate from a given
// libcalendar Date.
func oldHinduSolarFromDate(d Date) OldHinduSolarDate {
return OldHinduSolarDate{
Year: d.Components[0],
Month: d.Components[1],
Day: d.Components[2],
}
}
// oldHinduLunarFromDate computes a OldHinduLunarDate from a given
// libcalendar Date.
func oldHinduLunarFromDate(d Date) OldHinduLunarDate {
return OldHinduLunarDate{
Year: d.Components[0],
Month: d.Components[1],
Day: d.Components[2],
}
}
// AbsoluteFromDate returns the absolute (fixed) date from a given calendar
// date. Note that no checks are performed as to whether the given date is
// valid, i.e. in the date range for which the calendar was defined. For
// instance, conversion of dates preceding the 19.07.622 (1 Muharram 1 A.H.)
// into the Islamic calendar or dates preceding the 22.09.1792 (1 Vendémiare
// an 1) may yield non-sensical results.
func AbsoluteFromDate(d Date) (absoluteDate float64) {
switch d.Calendar {
case "gregorian":
return AbsoluteFromGregorian(gregorianFromDate(d))
case "iso":
return AbsoluteFromIso(isoFromDate(d))
case "julian":
return AbsoluteFromJulian(julianFromDate(d))
case "islamic":
return AbsoluteFromIslamic(islamicFromDate(d))
case "hebrew":
return AbsoluteFromHebrew(hebrewFromDate(d))
case "mayanLongCount":
return AbsoluteFromMayanLongCount(mayanLongCountFromDate(d))
case "french":
return AbsoluteFromFrench(frenchFromDate(d))
case "oldHinduSolar":
return AbsoluteFromOldHinduSolar(oldHinduSolarFromDate(d))
case "oldHinduLunar":
return AbsoluteFromOldHinduLunar(oldHinduLunarFromDate(d))
default:
return math.NaN()
}
}
// DateFromAbsolute converts a given absolute (fixed) date into the date
// representation specified in `calendar`
func DateFromAbsolute(absoluteDate float64, calendar string) Date {
switch calendar {
case "gregorian":
return GregorianFromAbsolute(absoluteDate).Date()
case "iso":
return IsoFromAbsolute(absoluteDate).Date()
case "julian":
return JulianFromAbsolute(absoluteDate).Date()
case "islamic":
return IslamicFromAbsolute(absoluteDate).Date()
case "hebrew":
return HebrewFromAbsolute(absoluteDate).Date()
case "mayanLongCount":
return MayanLongCountFromAbsolute(absoluteDate).Date()
case "mayanHaab":
return MayanHaabFromAbsolute(absoluteDate).Date()
case "mayanTzolkin":
return MayanTzolkinFromAbsolute(absoluteDate).Date()
case "french":
return FrenchFromAbsolute(absoluteDate).Date()
case "oldHinduSolar":
return OldHinduSolarFromAbsolute(absoluteDate).Date()
case "oldHinduLunar":
return OldHinduLunarFromAbsolute(absoluteDate).Date()
default:
return Date{}
}
}
// JsonDateFromAbsolute converts a given absolute (fixed) date into the date
// representation specified in `calendar`. It returns a Date marshalled into
// a JSON-string.
func JsonDateFromAbsolute(rd float64, calendar string) string {
return DateFromAbsolute(rd, calendar).Json()
}