-
Notifications
You must be signed in to change notification settings - Fork 1
/
span.go
48 lines (41 loc) · 1.19 KB
/
span.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
package core
import (
"time"
"cloud.google.com/go/civil"
"google.golang.org/api/calendar/v3"
)
type span struct {
start time.Time
events map[*calendar.Event]CategoryName
categories []*Category
}
func newSpan(categories []*Category) *span {
return &span{categories: categories, events: make(map[*calendar.Event]CategoryName)}
}
func (s *span) checkpoint(dayTotals map[civil.Date]time.Duration, categoryTotals map[CategoryName]time.Duration, end time.Time) {
timeSpent := end.Sub(s.start)
eventCount := len(s.events)
var timePerEvent int64
if eventCount > 0 {
timePerEvent = int64(timeSpent) / int64(eventCount)
dayTotals[civil.DateOf(s.start)] += timeSpent
}
for _, categoryName := range s.events {
categoryTotals[categoryName] += time.Duration(timePerEvent)
}
s.start = end
}
func (s *span) eventEnd(event *calendar.Event) {
delete(s.events, event)
}
// eventStart returns false if the event was not recognized to belong to a category.
func (s *span) eventStart(event *calendar.Event) bool {
for _, aCategory := range s.categories {
if aCategory.recognizes(event) {
s.events[event] = aCategory.Name
return true
}
}
s.events[event] = Uncategorized
return false
}