-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.go
133 lines (115 loc) · 3.79 KB
/
calendar.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
package calendar
import (
"fmt"
"time"
"go.uber.org/zap"
)
//Calendar ..
type Calendar struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Timezone string `json:"timezone"`
Periods []Period `json:"periods"`
UnionCalendarIDs []int64 `json:"unionCalendarIDs,omitempty"`
Enabled bool `json:"enabled"`
}
// InPeriodContains ..
type InPeriodContains struct {
Contains bool `json:"contains"`
}
// containsWithTz check if a calendar contains a specific time (based on inclusion and exclusion periods)
// This function only checks the calendar periods (but not the unioned calendars)
// Therefore, the calendar MUST have been resolved / flatten before calling this function
func (c Calendar) containsWithTz(t time.Time) (bool, PeriodStatus, PeriodStatus, PeriodStatus) {
statusMonth := NoInfo
statusDay := NoInfo
statusTime := NoInfo
tz := time.UTC
if c.Timezone != "" {
var err error
tz, err = time.LoadLocation(c.Timezone)
if err != nil {
zap.L().Warn("Invalid timezone", zap.Any("timezone", c.Timezone))
}
}
for _, period := range c.Periods {
month, day, time := period.containsWithTz(t, tz)
if month == OutOfPeriod || day == OutOfPeriod || time == OutOfPeriod {
month = OutOfPeriod
day = OutOfPeriod
time = OutOfPeriod
}
if month == InPeriod {
statusMonth = includedToStatus(period.Included)
}
if day == InPeriod {
statusDay = includedToStatus(period.Included)
}
if time == InPeriod {
statusTime = includedToStatus(period.Included)
}
}
status := true
if statusMonth == NoInfo && statusDay == NoInfo && statusTime == NoInfo {
status = false
}
if statusMonth == OutOfPeriod || statusDay == OutOfPeriod || statusTime == OutOfPeriod {
status = false
}
return status, statusMonth, statusDay, statusTime
}
func includedToStatus(included bool) PeriodStatus {
if included {
return InPeriod
}
return OutOfPeriod
}
func getCalendar(id int64) (Calendar, bool, error) {
calendar, found := _globalCBase.calendars[id]
return calendar, found, nil
}
func setCalendar(calendar Calendar) {
_globalCBase.calendars[calendar.ID] = calendar
}
// ResolveCalendar resolve a calendar definition dynamically and recursively with its subcalendars
// Resolution order :
// - For each sub-calendar (with respect of order)
// - Sub-calendar Periods (with respect of order)
// - Calendar Periods (with respect of order)
func (c Calendar) ResolveCalendar(enabledCalendars map[int64]Calendar, joinedCalendars []int64) Calendar {
joinedCalendars = append(joinedCalendars, c.ID)
periods := make([]Period, 0)
// Append current calendar periods
periods = append(periods, c.Periods...)
// Append unioned calendars periods
for _, unionCalendarID := range c.UnionCalendarIDs {
var circularReference bool
for _, id := range joinedCalendars {
if id == unionCalendarID {
circularReference = true
break
}
}
if circularReference {
zap.L().Warn("Skipping Calendar union in order to avoid a circular reference", zap.Int64s("joinedCalendars", joinedCalendars), zap.Int64("unionCalendarID", unionCalendarID))
continue
}
unionCalendar, found := enabledCalendars[unionCalendarID]
if !found {
zap.L().Warn("The calendar to join was not found", zap.Int64("calendarID", c.ID), zap.Int64("unionedCalendarID", unionCalendarID))
continue
}
unionCalendarResolved := unionCalendar.ResolveCalendar(enabledCalendars, joinedCalendars)
periods = append(periods, unionCalendarResolved.Periods...)
}
return Calendar{
ID: c.ID,
Name: fmt.Sprintf("%s (resolved)", c.Name),
Description: c.Description,
Timezone: c.Timezone,
Periods: periods,
Enabled: c.Enabled,
UnionCalendarIDs: []int64{},
}
}