forked from rickar/cal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcal.go
More file actions
470 lines (413 loc) · 11.8 KB
/
cal.go
File metadata and controls
470 lines (413 loc) · 11.8 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// (c) 2014 Rick Arnold. Licensed under the BSD license (see LICENSE).
package cal
import (
"math"
"time"
)
const (
dayStart = 9
dayEnd = 17
)
// IsWeekend reports whether the given date falls on a weekend.
func IsWeekend(date time.Time) bool {
day := date.Weekday()
return day == time.Saturday || day == time.Sunday
}
// IsWeekdayN reports whether the given date is the nth occurrence of the
// day in the month.
//
// The value of n affects the direction of counting:
// n > 0: counting begins at the first day of the month.
// n == 0: the result is always false.
// n < 0: counting begins at the end of the month.
func IsWeekdayN(date time.Time, day time.Weekday, n int) bool {
cday := date.Weekday()
if cday != day || n == 0 {
return false
}
if n > 0 {
return (date.Day()-1)/7 == (n - 1)
}
n = -n
last := time.Date(date.Year(), date.Month()+1,
1, 12, 0, 0, 0, date.Location())
lastCount := 0
for {
last = last.AddDate(0, 0, -1)
if last.Weekday() == day {
lastCount++
}
if lastCount == n || last.Month() != date.Month() {
break
}
}
return lastCount == n && last.Month() == date.Month() &&
last.Day() == date.Day()
}
// MonthStart reports the starting day of the month in t. The time portion is
// unchanged.
func MonthStart(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), 1, t.Hour(), t.Minute(), t.Second(),
t.Nanosecond(), t.Location())
}
// MonthEnd reports the ending day of the month in t. The time portion is
// unchanged.
func MonthEnd(t time.Time) time.Time {
return time.Date(t.Year(), t.Month()+1, 0, t.Hour(), t.Minute(),
t.Second(), t.Nanosecond(), t.Location())
}
// JulianDayNumber reports the Julian Day Number for t. Note that Julian days
// start at 12:00 UTC.
func JulianDayNumber(t time.Time) int {
// algorithm from http://www.tondering.dk/claus/cal/julperiod.php#formula
utc := t.UTC()
a := (14 - int(utc.Month())) / 12
y := utc.Year() + 4800 - a
m := int(utc.Month()) + 12*a - 3
jdn := utc.Day() + (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 - 32045
if utc.Hour() < 12 {
jdn--
}
return jdn
}
// JulianDate reports the Julian Date (which includes time as a fraction) for t.
func JulianDate(t time.Time) float32 {
utc := t.UTC()
jdn := JulianDayNumber(t)
if utc.Hour() < 12 {
jdn++
}
return float32(jdn) + (float32(utc.Hour())-12.0)/24.0 +
float32(utc.Minute())/1440.0 + float32(utc.Second())/86400.0
}
// WorkdayFn reports whether the given date is a workday.
// This is useful for situations where work days change throughout the year.
//
// If your workdays are fixed (Mon-Fri for example) then a WorkdayFn
// is not necessary and you can use cal.SetWorkday() instead.
type WorkdayFn func(date time.Time) bool
// Calendar represents a yearly calendar with a list of holidays.
type Calendar struct {
holidays [13][]Holiday // 0 for offset based holidays, 1-12 for month based
workday [7]bool // flags to indicate a day of the week is a workday
dayStart time.Duration // the time offset at which the workdays starts
dayEnd time.Duration // the time offset at which workdays end
WorkdayFunc WorkdayFn // optional function to override workday flags
Observed ObservedRule
}
// NewCalendar creates a new Calendar with no holidays defined
// and work days of Monday through Friday.
func NewCalendar() *Calendar {
c := &Calendar{}
for i := range c.holidays {
c.holidays[i] = make([]Holiday, 0, 2)
}
c.workday[time.Monday] = true
c.workday[time.Tuesday] = true
c.workday[time.Wednesday] = true
c.workday[time.Thursday] = true
c.workday[time.Friday] = true
c.dayStart = time.Duration(dayStart * time.Hour)
c.dayEnd = time.Duration(dayEnd * time.Hour)
return c
}
// AddHoliday adds a holiday to the calendar's list.
func (c *Calendar) AddHoliday(h ...Holiday) {
for _, hd := range h {
c.holidays[hd.Month] = append(c.holidays[hd.Month], hd)
}
}
// SetWorkday changes the given day's status as a standard working day
func (c *Calendar) SetWorkday(day time.Weekday, workday bool) {
c.workday[day] = workday
}
// IsHoliday reports whether a given date is a holiday. It does not account
// for the observation of holidays on alternate days.
func (c *Calendar) IsHoliday(date time.Time) bool {
idx := date.Month()
for i := range c.holidays[idx] {
if c.holidays[idx][i].matches(date) {
return true
}
}
for i := range c.holidays[0] {
if c.holidays[0][i].matches(date) {
return true
}
}
return false
}
// IsWorkday reports whether a given date is a work day (business day).
func (c *Calendar) IsWorkday(date time.Time) bool {
day := date.Weekday()
var workday bool
if c.WorkdayFunc == nil {
workday = c.workday[day]
} else {
workday = c.WorkdayFunc(date)
}
if !workday || c.IsHoliday(date) {
return false
}
if c.Observed == ObservedExact {
return true
}
if (c.Observed == ObservedMonday || c.Observed == ObservedMondayTuesday) && day == time.Monday {
sun := date.AddDate(0, 0, -1)
sat := date.AddDate(0, 0, -2)
return !c.IsHoliday(sat) && !c.IsHoliday(sun)
} else if c.Observed == ObservedMondayTuesday && day == time.Tuesday {
mon := date.AddDate(0, 0, -1)
sun := date.AddDate(0, 0, -2)
sat := date.AddDate(0, 0, -3)
return !(c.IsHoliday(sat) && c.IsHoliday(sun)) && !(c.IsHoliday(sat) && c.IsHoliday(mon)) && !(c.IsHoliday(sun) && c.IsHoliday(mon))
} else if c.Observed == ObservedNearest {
if day == time.Friday {
sat := date.AddDate(0, 0, 1)
return !c.IsHoliday(sat)
} else if day == time.Monday {
sun := date.AddDate(0, 0, -1)
return !c.IsHoliday(sun)
}
}
return true
}
// countWorkdays reports the number of workdays from the given date to the end
// of the month.
func (c *Calendar) countWorkdays(dt time.Time, month time.Month) int {
n := 0
for ; month == dt.Month(); dt = dt.AddDate(0, 0, 1) {
if c.IsWorkday(dt) {
n++
}
}
return n
}
// Workdays reports the total number of workdays for the given year and month.
func (c *Calendar) Workdays(year int, month time.Month) int {
return c.countWorkdays(time.Date(year, month, 1, 12, 0, 0, 0, time.UTC), month)
}
// WorkdaysRemain reports the total number of remaining workdays in the month
// for the given date.
func (c *Calendar) WorkdaysRemain(date time.Time) int {
return c.countWorkdays(date.AddDate(0, 0, 1), date.Month())
}
// WorkdayN reports the day of the month that corresponds to the nth workday
// for the given year and month.
//
// The value of n affects the direction of counting:
// n > 0: counting begins at the first day of the month.
// n == 0: the result is always 0.
// n < 0: counting begins at the end of the month.
func (c *Calendar) WorkdayN(year int, month time.Month, n int) int {
var date time.Time
var add int
if n == 0 {
return 0
}
if n > 0 {
date = time.Date(year, month, 1, 12, 0, 0, 0, time.UTC)
add = 1
} else {
date = time.Date(year, month+1, 1, 12, 0, 0, 0, time.UTC).AddDate(0, 0, -1)
add = -1
n = -n
}
ndays := 0
for ; month == date.Month(); date = date.AddDate(0, 0, add) {
if c.IsWorkday(date) {
ndays++
if ndays == n {
return date.Day()
}
}
}
return 0
}
// WorkdaysFrom reports the date of a workday that is offset days
// away from start.
//
// If n > 0, then the date returned is start + offset workdays.
// If n == 0, then the date is returned unchanged.
// If n < 0, then the date returned is start - offset workdays.
func (c *Calendar) WorkdaysFrom(start time.Time, offset int) time.Time {
date := start
var add int
if offset == 0 {
return start
}
if offset > 0 {
add = 1
} else {
add = -1
offset = -offset
}
for ndays := 0; ndays < offset; {
date = date.AddDate(0, 0, add)
if c.IsWorkday(date) {
ndays++
}
}
return date
}
// CountHolidayHoursWithOffset returns the number of working hours in a range starting from the consumed start date
// to the end date set by the offset
func (c *Calendar) CountHolidayHoursWithOffset(start time.Time, offsetHour int) int {
days := int(math.Ceil(float64(offsetHour) / float64(24)))
holidayHours := 0
day := 0
for day <= days {
date := start.AddDate(0, 0, day)
if !c.IsWorkday(date) {
holidayHours += 24
days++
}
day++
}
return holidayHours
}
//CountWorkdays return amount of workdays between start and end dates
func (c *Calendar) CountWorkdays(start, end time.Time) int64 {
factor := 1
if end.Before(start) {
factor = -1
start, end = end, start
}
result := 0
var i time.Time
for i = start; i.Before(end); i = i.AddDate(0, 0, 1) {
if c.IsWorkday(i) {
result++
}
}
if i.Equal(end) && c.IsWorkday(end) {
result++
}
return int64(factor * result)
}
func maxTime(ts ...time.Time) time.Time {
r := time.Time{}
for _, t := range ts {
if t.After(r) {
r = t
}
}
return r
}
func minTime(ts ...time.Time) time.Time {
if len(ts) == 0 {
return time.Time{}
}
r := ts[0]
for _, t := range ts {
if t.Before(r) {
r = t
}
}
return r
}
// DailyWorkedTime returns the total time worked per day
// it makes it easy to compute working times per day
// allowing calls like c.AddWorkHours(time.Now(), 8 * c.DailyWorkedTime())
func (c *Calendar) DailyWorkedTime() time.Duration {
return c.dayEnd - c.dayStart
}
// StartWorkTime returns the time at which work starts in the current day
func (c *Calendar) StartWorkTime(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0,
0, t.Location()).Add(c.dayStart)
}
// EndWorkTime returns the time at which work ends in the current day
func (c *Calendar) EndWorkTime(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0,
0, t.Location()).Add(c.dayEnd)
}
// NextWorkStart determines what will be the next future time work will start
func (c *Calendar) NextWorkStart(t time.Time) time.Time {
start := c.StartWorkTime(t)
for !c.IsWorkday(start) || t.After(start) {
start = start.Add(24 * time.Hour)
}
return start
}
// CountWorkHours counts the actual number of worked hours between 2 different times
func (c *Calendar) CountWorkHours(start, end time.Time) time.Duration {
r := time.Duration(0)
if end.Before(start) {
start, end = end, start
}
current := maxTime(start, c.StartWorkTime(start))
if current.After(c.EndWorkTime(start)) {
current = c.NextWorkStart(start)
}
for current.Before(end) {
lastTimeInDay := minTime(c.EndWorkTime(current), end)
r += lastTimeInDay.Sub(current)
current = c.NextWorkStart(lastTimeInDay)
}
return r
}
// AddWorkHours determines the time in the future where the worked hours will be completed
func (c *Calendar) AddWorkHours(t time.Time, worked time.Duration) time.Time {
wStart := maxTime(t, c.StartWorkTime(t))
for !c.IsWorkday(wStart) {
wStart = c.NextWorkStart(wStart)
}
for worked > 0 {
t = minTime(wStart.Add(worked), c.EndWorkTime(wStart))
worked -= c.CountWorkHours(wStart, t)
wStart = c.NextWorkStart(t)
}
return t
}
// SetWorkingHours configures the calendar to override the default 9-17 working hours
func (c *Calendar) SetWorkingHours(start time.Duration, end time.Duration) {
if start > end {
// This should not really happen, but SetWorkingHours(18*time.Hour, 9*time.Hour) should also mean a 9-18 time range
c.dayStart = end
c.dayEnd = start
} else {
c.dayStart = start
c.dayEnd = end
}
}
// AddSkipNonWorkdays returns start time plus d working duration
func (c *Calendar) AddSkipNonWorkdays(start time.Time, d time.Duration) time.Time {
const day = 24 * time.Hour
s := start
for {
for !c.IsWorkday(s) {
s = s.Add(day)
}
if d >= day {
s = s.Add(day)
d = d - day
} else if d > 0 {
s = s.Add(d)
d = 0
} else {
break
}
}
return s
}
// SubSkipNonWorkdays returns start time minus d working duration
func (c *Calendar) SubSkipNonWorkdays(start time.Time, d time.Duration) time.Time {
const day = 24 * time.Hour * -1
s := start
for {
for !c.IsWorkday(s) {
s = s.Add(day)
}
if d >= day*-1 {
s = s.Add(day)
d = d + day
} else if d > 0 {
s = s.Add(-d)
d = 0
} else {
break
}
}
return s
}