-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.go
67 lines (54 loc) · 1.29 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
package goastro
import (
"time"
)
type Date struct {
Year, Month, Day int
}
func MakeDate(t time.Time) Date {
return Date{t.Year(), int(t.Month()), t.Day()}
}
func (d Date) Time() time.Time {
return time.Date(d.Year, time.Month(d.Month), d.Day, 0, 0, 0, 0, time.UTC)
}
func (d Date) AddDays(n int) Date {
return MakeDate(d.Time().AddDate(0, 0, n))
}
func (d Date) compareTo(d2 Date) int {
if d.Year != d2.Year {
return d.Year - d2.Year
}
if d.Month != d2.Month {
return d.Month - d2.Month
}
return d.Day - d2.Day
}
var gregorianStart = Date{1582, 10, 15}
type JulianDay float64
type Time interface {
Date() Date
Hours() float64
}
// Ch 7 p.60
func MakeJulianDay(t Time) JulianDay {
y, m, d := t.Date().Year, t.Date().Month, t.Date().Day
if m <= 2 {
y--
m += 12
}
b := 0
if t.Date().compareTo(gregorianStart) >= 0 {
a := y / 100
b = 2 - a + (a / 4)
}
fd := float64(d) + t.Hours()/24
jdint := int(365.25*float64(y+4716)) + int(30.6001*float64(m+1)) + b
jd := float64(jdint) + fd - 1524.5
return JulianDay(jd)
}
func DayOfYear(date time.Time) int {
date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location())
jan0 := time.Date(date.Year(), time.January, 0, 0, 0, 0, 0, date.Location())
diff := date.Sub(jan0)
return int(diff / (24 * time.Hour))
}