-
Notifications
You must be signed in to change notification settings - Fork 0
/
timewindow.go
137 lines (113 loc) · 3.14 KB
/
timewindow.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
package usts
import (
"fmt"
"time"
"github.com/rickar/cal/v2"
)
type AddSlotMode int
const (
Add AddSlotMode = 0
Remove AddSlotMode = 1
And AddSlotMode = 2
)
type TimeWindow struct {
ID string
Slots []*TimeSlot
SlotMode []AddSlotMode
Calendar *cal.Calendar
timeZone string
}
func NewTimeWindow(id string) *TimeWindow {
return &TimeWindow{ID: id}
}
func (tw *TimeWindow) SetCalendar(c *cal.Calendar) {
tw.Calendar = c
}
func (tw *TimeWindow) SetTimeZone(tz string) (*time.Location, error) {
loc, err := time.LoadLocation(tz)
if err != nil {
return nil, err
}
tw.timeZone = tz
return loc, nil
}
func (tw *TimeWindow) AddSlot(sl *TimeSlot, mode AddSlotMode) {
tw.Slots = append(tw.Slots, sl)
tw.SlotMode = append(tw.SlotMode, mode)
}
func CalendarWindowEvents(c *cal.Calendar, tz string, start, end time.Time) (*USTimeSerie, error) {
if c == nil {
return nil, fmt.Errorf("Error Calendar not set ")
}
cron_tpl := "00 00 * * *"
if len(tz) > 0 {
cron_tpl = "CRON_TZ=" + tz + " 00 00 * * * "
}
ilog.Debugf(">>>>USTS DEBUG:CRON START: %s ", cron_tpl)
sched, err := crparser.Parse(cron_tpl)
if err != nil {
return nil, fmt.Errorf("ERROR on parse Start cron expression : %s", err)
}
ret := NewUSTimeSerie(0)
yesterday := sched.Next(start.AddDate(0, 0, -1))
a, b, _ := c.IsHoliday(yesterday)
ret.SetDefault(!(a || b))
tnext := start.Add(-1 * time.Second)
for {
t := sched.Next(tnext)
if t.After(end) {
break
}
a, b, _ := c.IsHoliday(t)
ret.Add(t, !(a || b))
tnext = t
}
return ret, nil
}
func (tw *TimeWindow) GetTimeEvents(start, end time.Time) (*USTimeSerie, error) {
var ret *USTimeSerie
var err error
//first events from calendar
if tw.Calendar != nil {
ret, err = CalendarWindowEvents(tw.Calendar, tw.timeZone, start, end)
if err != nil {
return nil, err
}
} else {
ret = NewUSTimeSerie(0)
ret.SetDefault(false)
}
// for each slot add/remove slots from
for k, v := range tw.Slots {
ilog.Debugf(">>>>USTS DEBUG [TimeWindow:GetTimeEvents] Compute events for timeslot = %s / mode %d", v.ID, tw.SlotMode[k])
slotEvents, err := v.GetTimeEvents(start, end, tw.timeZone)
if err != nil {
return ret, fmt.Errorf("Error on get time events for slot %d/%s", k, v.ID)
}
//fmt.Printf("-------%s/%d/%s---", tw.ID, k, v.ID)
//slotEvents.DumpInTimezone(tw.timeZone)
switch tw.SlotMode[k] {
case Add:
ret, err = ret.Or(slotEvents)
if err != nil {
return ret, fmt.Errorf("Error on Window [%s] can not Add slot %d/[%s] : Err: %s", tw.ID, k, v.ID, err)
}
case And:
ret, err = ret.And(slotEvents)
if err != nil {
return ret, fmt.Errorf("Error on Window [%s] can not Add slot %d/[%s] : Err: %s", tw.ID, k, v.ID, err)
}
case Remove:
slotEventsNeg, _ := slotEvents.Not()
//fmt.Printf("-------%s/%d/NOT[%s]---", tw.ID, k, v.ID)
//slotEventsNeg.DumpInTimezone(tw.timeZone)
ret, err = ret.And(slotEventsNeg)
if err != nil {
return ret, fmt.Errorf("Error on Window [%s] can not Remove slot %d/[%s] : Err: %s", tw.ID, k, v.ID, err)
}
}
//fmt.Printf("-------%s/%d/ CURRENT RET---", tw.ID, k)
//ret.DumpInTimezone(tw.timeZone)
}
return ret, nil
}