-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalendar_time.go
More file actions
76 lines (60 loc) · 1.49 KB
/
Copy pathcalendar_time.go
File metadata and controls
76 lines (60 loc) · 1.49 KB
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
package main
import (
"errors"
"time"
"google.golang.org/api/calendar/v3"
)
type (
calendarDate string
calendarTime string
)
type calendarPeriod interface {
ToTaskWarriorTime() (taskWarriorTime, error)
}
func toCalendarTime(t time.Time) calendarTime {
return calendarTime(t.Format(time.RFC3339))
}
func toCalendarDate(t time.Time) calendarDate {
return calendarDate(t.Format(time.RFC3339))
}
func eventToCalendarPeriod(event *calendar.Event) (calendarPeriod, error) {
var (
period calendarPeriod
err error
)
if event.Start.DateTime != "" {
period = calendarTime(event.Start.DateTime)
} else if event.Start.Date != "" {
period = calendarDate(event.Start.Date)
} else {
err = errors.New("no date to convert")
}
if err != nil {
return period, err
}
return period, nil
}
func eventDue(event *calendar.Event) (taskWarriorTime, error) {
if event == nil {
return "", errors.New("event is nil")
}
period, err := eventToCalendarPeriod(event)
if err != nil {
return "", err
}
return period.ToTaskWarriorTime()
}
func (c calendarTime) ToTaskWarriorTime() (taskWarriorTime, error) {
parsed, err := time.ParseInLocation(time.RFC3339, string(c), time.Local)
if err != nil {
return "", err
}
return toTaskWarriorTime(parsed.In(time.UTC)), nil
}
func (c calendarDate) ToTaskWarriorTime() (taskWarriorTime, error) {
parsed, err := time.ParseInLocation("2006-01-02", string(c), time.Local)
if err != nil {
return "", err
}
return toTaskWarriorTime(parsed.In(time.UTC)), nil
}