-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday.go
84 lines (66 loc) · 1.84 KB
/
day.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
package chronos
import "time"
// creates a new month day (day must be >0 and <=31)
func NewMonthDay(day uint, at DayTime) MonthDay {
if day > 31 {
panic("month day can't be greater than 31")
}
// we use the actual real day number
if day == 0 {
panic("month day can't be 0")
}
return MonthDay{day, at}
}
// a day in a month and a specific time on that day
type MonthDay struct {
Day uint `json:"day"`
At DayTime `json:"at"`
}
type MonthDaysSorted []MonthDay
func (mds MonthDaysSorted) Len() int { return len(mds) }
func (mds MonthDaysSorted) Swap(i, j int) { mds[i], mds[j] = mds[j], mds[i] }
func (mds MonthDaysSorted) Less(i, j int) bool {
this := mds[i]
next := mds[j]
if this.Day < next.Day {
return true
}
if this.Day > next.Day {
return false
}
return this.At.AsTime().Before(next.At.AsTime())
}
// a weekday and a specific time on that day
type Weekday struct {
Day time.Weekday `json:"day"`
At DayTime `json:"at"`
}
type WeekdaysSorted []Weekday
func (wds WeekdaysSorted) Len() int { return len(wds) }
func (wds WeekdaysSorted) Swap(i, j int) { wds[i], wds[j] = wds[j], wds[i] }
func (wds WeekdaysSorted) Less(i, j int) bool {
this := wds[i]
next := wds[j]
if this.Day < next.Day {
return true
}
if this.Day > next.Day {
return false
}
return this.At.AsTime().Before(next.At.AsTime())
}
// represents a 24 hour day
type DayTime struct {
Hour int `json:"hour"`
Minute int `json:"minute"`
Second int `json:"second"`
}
func (dt *DayTime) AsTime() time.Time {
return time.Date(0, 0, 0, dt.Hour, dt.Minute, dt.Second, 0, time.Local)
}
type DayTimesSorted []DayTime
func (dts DayTimesSorted) Len() int { return len(dts) }
func (dts DayTimesSorted) Swap(i, j int) { dts[i], dts[j] = dts[j], dts[i] }
func (dts DayTimesSorted) Less(i, j int) bool {
return dts[i].AsTime().Before(dts[j].AsTime())
}