44 "fmt"
55 "time"
66
7+ "github.com/sunshineplan/utils"
78 "github.com/sunshineplan/utils/clock"
89)
910
@@ -52,6 +53,9 @@ type sched struct {
5253}
5354
5455func NewSchedule (year int , month time.Month , day int , clock * Clock ) Schedule {
56+ if clock == nil {
57+ clock = new (Clock )
58+ }
5559 return sched {year , month , day , clock }
5660}
5761
@@ -69,25 +73,16 @@ func (s sched) IsMatched(t time.Time) bool {
6973 if (s .year == 0 || s .year == year ) &&
7074 (s .month == 0 || s .month == month ) &&
7175 (s .day == 0 || s .day == day ) {
72- if s .clock == nil {
73- s .clock = & Clock {}
74- }
7576 return s .clock .IsMatched (t )
7677 }
7778 return false
7879}
7980
8081func (s sched ) Next (t time.Time ) time.Time {
81- if s .clock == nil {
82- s .clock = & Clock {}
83- }
8482 return s .clock .Next (t )
8583}
8684
8785func (s sched ) TickerDuration () time.Duration {
88- if s .clock == nil {
89- return 24 * time .Hour
90- }
9186 return s .clock .TickerDuration ()
9287}
9388
@@ -108,9 +103,6 @@ func (s sched) String() string {
108103 } else {
109104 day = fmt .Sprintf ("%02d" , s .day )
110105 }
111- if s .clock == nil {
112- s .clock = & Clock {}
113- }
114106 return fmt .Sprintf ("%s/%s/%s %s" , year , month , day , s .clock )
115107}
116108
@@ -121,6 +113,9 @@ type weekSched struct {
121113}
122114
123115func ISOWeekSchedule (year int , week int , weekday * time.Weekday , clock * Clock ) Schedule {
116+ if clock == nil {
117+ clock = new (Clock )
118+ }
124119 return weekSched {year , week , weekday , clock }
125120}
126121
@@ -130,25 +125,16 @@ func (s weekSched) IsMatched(t time.Time) bool {
130125 if (s .year == 0 || s .year == year ) &&
131126 (s .week == 0 || s .week == week ) &&
132127 (s .weekday == nil || * s .weekday == weekday ) {
133- if s .clock == nil {
134- s .clock = & Clock {}
135- }
136128 return s .clock .IsMatched (t )
137129 }
138130 return false
139131}
140132
141133func (s weekSched ) Next (t time.Time ) time.Time {
142- if s .clock == nil {
143- s .clock = & Clock {}
144- }
145134 return s .clock .Next (t )
146135}
147136
148137func (s weekSched ) TickerDuration () time.Duration {
149- if s .clock == nil {
150- return 24 * time .Hour
151- }
152138 return s .clock .TickerDuration ()
153139}
154140
@@ -169,9 +155,6 @@ func (s weekSched) String() string {
169155 } else {
170156 weekday = fmt .Sprint (s .weekday )
171157 }
172- if s .clock == nil {
173- s .clock = & Clock {}
174- }
175158 return fmt .Sprintf ("%s/ISOWeek:%s/Weekday:%s %s" , year , week , weekday , s .clock )
176159}
177160
@@ -183,17 +166,16 @@ type weekdaySched struct {
183166}
184167
185168func WeekdaySchedule (year int , month time.Month , weekday * time.Weekday , clock * Clock ) Schedule {
169+ if clock == nil {
170+ clock = new (Clock )
171+ }
186172 return weekdaySched {year , month , weekday , clock }
187173}
188174
189- func ptrWeekday (weekday time.Weekday ) * time.Weekday {
190- return & weekday
191- }
192-
193175func Weekday (weekday ... time.Weekday ) Schedule {
194176 var s multiSched
195177 for _ , weekday := range weekday {
196- s = append (s , WeekdaySchedule (0 , 0 , ptrWeekday (weekday ), FullClock ()))
178+ s = append (s , WeekdaySchedule (0 , 0 , utils . Ptr (weekday ), FullClock ()))
197179 }
198180 return s
199181}
@@ -209,25 +191,16 @@ func (s weekdaySched) IsMatched(t time.Time) bool {
209191 if (s .year == 0 || s .year == year ) &&
210192 (s .month == 0 || s .month == month ) &&
211193 (s .weekday == nil || * s .weekday == weekday ) {
212- if s .clock == nil {
213- s .clock = & Clock {}
214- }
215194 return s .clock .IsMatched (t )
216195 }
217196 return false
218197}
219198
220199func (s weekdaySched ) Next (t time.Time ) time.Time {
221- if s .clock == nil {
222- s .clock = & Clock {}
223- }
224200 return s .clock .Next (t )
225201}
226202
227203func (s weekdaySched ) TickerDuration () time.Duration {
228- if s .clock == nil {
229- return 24 * time .Hour
230- }
231204 return s .clock .TickerDuration ()
232205}
233206
@@ -248,9 +221,6 @@ func (s weekdaySched) String() string {
248221 } else {
249222 weekday = fmt .Sprint (s .weekday )
250223 }
251- if s .clock == nil {
252- s .clock = & Clock {}
253- }
254224 return fmt .Sprintf ("%s/%s/Weekday:%s %s" , year , month , weekday , s .clock )
255225}
256226
0 commit comments