forked from cpucycle/astrotime
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathastrotime_test.go
91 lines (81 loc) · 2.76 KB
/
astrotime_test.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
package astrotime
import (
"fmt"
"math"
"testing"
"time"
)
type location struct {
timezone string
latitude float64
longitude float64
}
type times struct {
date string
sunrise string
sunset string
dawn string
dusk string
}
type test struct {
times times
location location
error time.Duration
}
// test data from http://www.timeanddate.com/worldclock/sunrise.html
var (
SYDNEY = location{
timezone: "Australia/Sydney",
latitude: -33.86,
longitude: -151.20,
}
STOCKHOLM = location{
timezone: "Europe/Stockholm",
latitude: 59.33,
longitude: -18.067,
}
NEWYORK = location{
timezone: "America/New_York",
latitude: 40.642,
longitude: 74.017,
}
NOVEMBER = "2014-11-01"
JULY = "2015-07-01"
MINUTE = time.Minute
DATA = []*test{
&test{times{NOVEMBER, "05:55", "19:23", "05:29", "19:49"}, SYDNEY, MINUTE},
&test{times{NOVEMBER, "07:07", "15:55", "06:23", "16:39"}, STOCKHOLM, MINUTE},
&test{times{NOVEMBER, "07:26", "17:52", "06:58", "18:21"}, NEWYORK, MINUTE},
&test{times{JULY, "07:01", "16:57", "06:33", "17:25"}, SYDNEY, MINUTE},
&test{times{JULY, "03:37", "22:06", "02:09", "23:33"}, STOCKHOLM, MINUTE},
&test{times{JULY, "05:29", "20:31", "04:55", "21:04"}, NEWYORK, MINUTE},
}
)
func TestData(t *testing.T) {
for _, datum := range DATA {
if loc, err := time.LoadLocation(datum.location.timezone); err != nil {
t.Fatalf("bad location %s", datum.location.timezone)
} else {
formatted := fmt.Sprintf("%s 00:00:00", datum.times.date)
midnight, err := time.ParseInLocation("2006-01-02 15:04:05", formatted, loc)
if err != nil {
t.Fatalf("failed to parse date/timestamp '%s': %s", formatted, err)
}
checkDate := func(description string, timeOfDay string, calculated time.Time) {
formatted := fmt.Sprintf("%s %s", datum.times.date, timeOfDay)
timestamp, err := time.ParseInLocation("2006-01-02 15:04", formatted, loc)
if err != nil {
t.Fatalf("failed to parse date/timestamp '%s': %s", formatted, err)
}
actualError := math.Abs((float64)(timestamp.Sub(calculated)))
if actualError > float64(datum.error) {
t.Errorf("calculated -> %v, wanted -> %v %d -> (wanted < %d). location=%s date=%s", calculated, timestamp, actualError, datum.error, datum.location.timezone, datum.times.date)
}
}
checkDate("dawn", datum.times.dawn, NextDawn(midnight, datum.location.latitude, datum.location.longitude, CIVIL_DAWN))
checkDate("sunrise", datum.times.sunrise, NextSunrise(midnight, datum.location.latitude, datum.location.longitude))
checkDate("sunset", datum.times.sunset, NextSunset(midnight, datum.location.latitude, datum.location.longitude))
checkDate("dusk", datum.times.dusk, NextDusk(midnight, datum.location.latitude, datum.location.longitude, CIVIL_DUSK))
}
}
}