This repository has been archived by the owner on Dec 3, 2020. It is now read-only.
/
week.go
165 lines (130 loc) · 3.37 KB
/
week.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
package recurrence
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
// A Week represents a week of the month. This is most useful in combination
// with other entities satisfying the Schedule interface.
type Week int
// implements the Stringer interface.
func (w Week) String() string {
switch int(w) {
case 1:
return "(First Week)"
case 2:
return "(Second Week)"
case 3:
return "(Third Week)"
case 4:
return "(Fourth Week)"
case 5:
return "(Fifth Week)"
case Last:
return "(Last Week)"
default:
return "(Never Week)"
}
}
// IsOccurring implements the Schedule interface.
func (w Week) IsOccurring(t time.Time) bool {
weekInt := int(w)
if weekInt == Last {
return isLastWeekInMonth(t)
}
return weekInMonth(t) == weekInt
}
// Occurrences implements the Schedule interface.
func (w Week) Occurrences(tr TimeRange) chan time.Time {
return occurrencesFor(w, tr)
}
func (w Week) nextAfter(t time.Time) (time.Time, error) {
desiredWeek := int(w)
if desiredWeek == 1 {
if t.Day() < 7 || isLastDayInMonth(t) {
return t.AddDate(0, 0, 1), nil
}
return firstDayOfMonth(t).AddDate(0, 1, 0), nil
}
if desiredWeek == 2 {
if t.Day() < 7 {
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC).AddDate(0, 0, 7), nil
}
if t.Day() > 13 {
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC).AddDate(0, 1, 7), nil
}
return t.AddDate(0, 0, 1), nil
}
if desiredWeek == 3 {
if t.Day() < 14 {
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC).AddDate(0, 0, 14), nil
}
if t.Day() > 20 {
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC).AddDate(0, 1, 14), nil
}
return t.AddDate(0, 0, 1), nil
}
if desiredWeek == 4 {
if t.Day() < 21 {
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC).AddDate(0, 0, 21), nil
}
if t.Day() > 27 {
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC).AddDate(0, 1, 21), nil
}
return t.AddDate(0, 0, 1), nil
}
if desiredWeek == 5 {
totalDaysInMonth := lastDayOfMonth(t).Day()
if totalDaysInMonth < 29 {
return w.nextAfter(t.AddDate(0, 1, 0))
}
if isLastDayInMonth(t) {
return w.nextAfter(t.AddDate(0, 0, 1))
}
if t.Day() < 27 {
return firstDayOfMonth(t).AddDate(0, 0, 28), nil
}
return t.AddDate(0, 0, 1), nil
}
if desiredWeek == Last {
totalDaysInMonth := lastDayOfMonth(t).Day()
if isLastDayInMonth(t) {
return w.nextAfter(t.AddDate(0, 0, 1))
}
if t.Day() < totalDaysInMonth-7 {
return lastDayOfMonth(t).AddDate(0, 0, -6), nil
}
return t.AddDate(0, 0, 1), nil
}
return t, fmt.Errorf("You should never get here")
}
// MarshalJSON implements the json.Marshaler interface.
func (w Week) MarshalJSON() ([]byte, error) {
if int(w) == Last {
return json.Marshal(map[string]interface{}{"week": "Last"})
}
return json.Marshal(map[string]interface{}{"week": int(w)})
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (w *Week) UnmarshalJSON(b []byte) error {
switch s := string(b); s {
case `1`, `2`, `3`, `4`, `5`:
i, err := strconv.ParseInt(s, 10, 0)
if err != nil {
return err
}
*w = Week(i)
case `"Last"`:
*w = Week(Last)
default:
return fmt.Errorf("Week cannot unmarshal %s", b)
}
return nil
}
func isLastWeekInMonth(t time.Time) bool {
return t.Month() != t.AddDate(0, 0, 7).Month()
}
func weekInMonth(t time.Time) int {
return ((t.Day() - 1) / 7) + 1
}