-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
151 lines (125 loc) · 3.6 KB
/
utils.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
package goc
import (
"fmt"
"log"
"math"
"strings"
"time"
"google.golang.org/api/calendar/v3"
)
const TIME_FORMAT = time.RFC3339
func insertToCalendar(data *FileData, newEvent *calendar.Event) {
client, source := GetClient()
_, err := client.Events.Insert(data.CalendarId, newEvent).Do()
if err != nil {
log.Fatalf("unable to add event to calendar: %v", err)
}
updateTotalDuration(client, data)
updateToken(source)
}
func updateTotalDuration(client *calendar.Service, data *FileData) {
eventList := getTodaysCalendarEvents(client, data)
totalDuration := 0.0
for _, evt := range eventList.Items {
start, _ := time.Parse(TIME_FORMAT, evt.Start.DateTime)
end, _ := time.Parse(TIME_FORMAT, evt.End.DateTime)
totalDuration += end.Sub(start).Seconds()
}
year, month, day := time.Now().Date()
data.DurationToday = time.Duration(math.Round(totalDuration)) * time.Second
data.CurrentDate = CurDate{year, month, day}
}
func getTodaysCalendarEvents(client *calendar.Service, data *FileData) *calendar.Events {
listCall := client.Events.List(data.CalendarId)
year, month, day := time.Now().Date()
minTime := time.Date(year, month, day, 0, 0, 0, 0, time.Now().Location())
listCall.TimeMin(minTime.Format(TIME_FORMAT))
listCall.TimeMax(getTime(false))
eventList, err := listCall.Do()
if err != nil {
log.Fatalf("unable to get calendar events: %v", err)
}
return eventList
}
func createEvent(data *FileData, endTime string) *calendar.Event {
return &calendar.Event{
Summary: data.CurrentTask.Name,
Start: &calendar.EventDateTime{
DateTime: data.CurrentTask.Start,
TimeZone: "Europe/Oslo",
},
End: &calendar.EventDateTime{
DateTime: endTime,
TimeZone: "Europe/Oslo",
},
}
}
func getTime(roundForward bool) string {
now := time.Now()
t := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, now.Location())
diff := t.Minute() % 5
if diff == 0 {
return t.Format(TIME_FORMAT)
}
if roundForward {
return t.Add(time.Duration(5-diff) * time.Minute).Format(TIME_FORMAT)
} else {
return t.Add(time.Duration(-diff) * time.Minute).Format(TIME_FORMAT)
}
}
func getTimeSince(start string) time.Duration {
startTime, err := time.Parse(TIME_FORMAT, start)
if err != nil {
log.Fatalf("unable to parse time: %v", err)
}
duration := time.Since(startTime).Round(time.Second)
return duration
}
func stringToTime(s string) string {
if len(s) == 3 {
s = "0" + s
}
now := time.Now()
timezone, _ := now.Zone()
fs := fmt.Sprintf("%d-%d-%d %v %v", now.Year(), now.Month(), now.Day(), s, timezone)
t, err := time.Parse("2006-1-2 1504 MST", fs)
if err != nil {
log.Fatalf("unable to parse time: %v", err)
}
return t.Format(TIME_FORMAT)
}
func formatTimeString(s string) string {
data := strings.Split(s, "T")
return fmt.Sprintf("%v", strings.Split(data[1], "+")[0][:5])
}
func checkAndUseAlias(name string, data *FileData) string {
aliasName := data.TaskAlias[name]
if aliasName != "" {
return aliasName
}
return name
}
func updatePrevTaskAlias(data *FileData) {
val := data.CurrentTask.Name
val2, ok2 := data.TaskAlias["prev"]
if ok2 && val == val2 {
// avoid many multiple of the same task alias in the list
return
}
if val, ok := data.TaskAlias["prev4"]; ok {
data.TaskAlias["prev5"] = val
}
if val, ok := data.TaskAlias["prev3"]; ok {
data.TaskAlias["prev4"] = val
}
if val, ok := data.TaskAlias["prev2"]; ok {
data.TaskAlias["prev3"] = val
}
if val, ok := data.TaskAlias["prev"]; ok {
data.TaskAlias["prev2"] = val
}
if data.TaskAlias == nil {
data.TaskAlias = make(map[string]string)
}
data.TaskAlias["prev"] = data.CurrentTask.Name
}