/
day.go
267 lines (231 loc) · 6.3 KB
/
day.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
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
package shiftpad
import (
"sort"
"time"
"github.com/wansing/shiftpad/datefmt"
"github.com/wansing/shiftpad/ical"
)
type Event struct {
ical.Event
Shifts []Shift
}
// Group is used for displaying. It can represent an event or a bunch of independent shifts.
type Group struct {
*ical.Event // can be nil
Shifts []Shift
}
type Day struct {
Begin time.Time // inclusive
End time.Time // exclusive
Events []Event // both with and without shifts
Shifts []Shift // without an event
}
type Repository interface {
GetICalFeedCache(url string) *ical.FeedCache
GetShifts(pad *Pad, from, to int64) ([]Shift, error) // begin: from inclusive, to exclusive
GetShiftsByEvent(pad *Pad, eventUID string) ([]Shift, error)
}
// date must be any time in the day
func GetDay(repo Repository, pad *Pad, date time.Time, location *time.Location) (Day, error) {
from := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, location)
to := from.AddDate(0, 0, 1)
events, shifts, err := GetInterval(repo, pad, from, to, location)
if err != nil {
return Day{}, err
}
return Day{
Begin: from,
End: to,
Events: events,
Shifts: shifts,
}, nil
}
// Groups returns the events plus an event with empty UID, which contains all shifts without an event.
func (day Day) Groups() []Group {
var groups []Group
if len(day.Shifts) > 0 {
groups = append(groups, Group{
Shifts: day.Shifts,
})
}
for _, event := range day.Events {
event := event // see https://github.com/golang/go/discussions/56010
groups = append(groups, Group{
Event: &event.Event,
Shifts: event.Shifts,
})
}
return groups
}
func (day Day) FmtDateTime(t time.Time) string {
return datefmt.DateTime(t, day.Begin)
}
func (day Day) FmtDateTimeRange(begin, end time.Time) string {
return datefmt.DateTimeRange(begin, end, day.Begin)
}
func (day Day) FmtEventTime(event ical.Event) string {
return datefmt.DateTimeRange(event.Start, event.End, day.Begin)
}
func (day Day) FmtShiftTime(shift Shift) string {
return datefmt.DateTimeRange(shift.Begin, shift.End, day.Begin)
}
type Week struct {
Begin time.Time
End time.Time
Days [7]*Day
}
// date must be any time in the week
func GetWeek(repo Repository, pad *Pad, year, week int, location *time.Location) (Week, error) {
// get start of week
// "Jan 01 to Jan 03 of year n might belong to week 52 or 53 of year n-1", so January 4th is always in week one
begin := time.Date(year, time.January, 4, 0, 0, 0, 0, location)
begin = begin.AddDate(0, 0, 7*(week-1))
// go back to monday
for begin.Weekday() != time.Monday {
begin = begin.AddDate(0, 0, -1)
}
to := begin.AddDate(0, 0, 7)
events, shifts, err := GetInterval(repo, pad, begin, to, location)
if err != nil {
return Week{}, err
}
// create days
var days = [7]*Day{}
for i := range days {
days[i] = &Day{
Begin: begin.AddDate(0, 0, i),
End: begin.AddDate(0, 0, i+1),
}
}
// move independent shifts into their individual day
for _, shift := range shifts {
shiftDay := closestDay(days, shift.Begin)
shiftDay.Shifts = append(shiftDay.Shifts, shift)
}
// move events
for _, event := range events {
eventDay := closestDay(days, event.Start)
eventDay.Events = append(eventDay.Events, event)
}
return Week{
Begin: begin,
End: to,
Days: days,
}, nil
}
func closestDay(days [7]*Day, t time.Time) *Day {
if t.Before(days[0].Begin) {
return days[0]
}
for _, day := range days {
if t.After(day.Begin) && t.Before(day.End) {
return day
}
}
return days[6]
}
// weeks can't be split into days because shifts and events may be contained in multiple days then, but we want them just once in a week (or any other interval)
func GetInterval(repo Repository, pad *Pad, from, to time.Time, location *time.Location) ([]Event, []Shift, error) {
// get shifts from time interval
var eventUIDs = make(map[string]interface{})
var independentShifts = []Shift{}
shifts, err := repo.GetShifts(pad, from.Unix(), to.Unix())
if err != nil {
return nil, nil, err
}
for _, shift := range shifts {
if shift.EventUID == "" {
independentShifts = append(independentShifts, shift)
} else {
eventUIDs[shift.EventUID] = struct{}{}
}
}
// get every event which is referenced in a shift or overlaps with the time interval
var events = []Event{}
icalEvents, _ := repo.GetICalFeedCache(pad.ICalOverlay).Get(location)
for _, icalEvent := range icalEvents {
if _, ok := eventUIDs[icalEvent.UID]; ok || overlaps(icalEvent.Start, icalEvent.End, from, to) {
events = append(events, Event{
Event: icalEvent,
})
delete(eventUIDs, icalEvent.UID)
}
}
// create dummies for remaining event uids
for uid := range eventUIDs {
events = append(events, Event{
Event: ical.Event{
UID: uid,
Summary: "Unknown event " + uid,
},
})
}
// get shifts of each event (including shifts that are not in the time interval)
for i, event := range events {
shifts, err := repo.GetShiftsByEvent(pad, event.UID)
if err != nil {
return nil, nil, err
}
events[i].Shifts = shifts
}
// adjust start and end times of dummy events
for i, event := range events {
if event.Start.IsZero() && event.End.IsZero() {
events[i].Start = minStart(event.Shifts)
events[i].End = maxEnd(event.Shifts)
}
}
// sort events by begin
sort.Slice(events, func(i, j int) bool {
a := events[i]
b := events[j]
if a.Event.Start.Equal(b.Event.Start) {
return a.Event.Summary < b.Event.Summary
}
return a.Event.Start.Before(b.Event.Start)
})
return events, independentShifts, nil
}
func maxEnd(shifts []Shift) time.Time {
var max time.Time
for _, shift := range shifts {
if max.IsZero() || max.Before(shift.End) {
max = shift.End
}
}
return max
}
func minStart(shifts []Shift) time.Time {
var min time.Time
for _, shift := range shifts {
if min.IsZero() || min.After(shift.Begin) {
min = shift.Begin
}
}
return min
}
func overlaps(begin1, end1, begin2, end2 time.Time) bool {
switch {
case begin1.IsZero() && end1.IsZero():
return false
case begin1.IsZero():
begin1 = end1
case end1.IsZero():
end1 = begin1
}
switch {
case begin2.IsZero() && end2.IsZero():
return false
case begin2.IsZero():
begin2 = end2
case end2.IsZero():
end2 = begin2
}
if end1.Before(begin2) || end1.Equal(begin2) {
return false
}
if begin1.After(end2) || begin1.Equal(end2) {
return false
}
return true
}