forked from PuloV/ics-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.go
153 lines (127 loc) · 3.59 KB
/
calendar.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
package ics
import (
"errors"
"fmt"
"time"
)
type Calendar struct {
name string
description string
url string
version float64
timezone time.Location
events []Event
eventsByDate map[string][]*Event
eventByID map[string]*Event
eventByImportedID map[string]*Event
}
func NewCalendar() *Calendar {
c := new(Calendar)
// c.events = make([]Event)
c.eventsByDate = make(map[string][]*Event)
c.eventByID = make(map[string]*Event)
c.eventByImportedID = make(map[string]*Event)
return c
}
func (c *Calendar) SetName(n string) *Calendar {
c.name = n
return c
}
func (c *Calendar) GetName() string {
return c.name
}
func (c *Calendar) SetDesc(desc string) *Calendar {
c.description = desc
return c
}
func (c *Calendar) GetDesc() string {
return c.description
}
func (c *Calendar) SetVersion(ver float64) *Calendar {
c.version = ver
return c
}
func (c *Calendar) GetVersion() float64 {
return c.version
}
func (c *Calendar) SetTimezone(tz time.Location) *Calendar {
c.timezone = tz
return c
}
func (c *Calendar) GetTimezone() time.Location {
return c.timezone
}
// add event to the calendar
func (c *Calendar) SetEvent(event Event) (*Calendar, error) {
// lock so that the events array doesn't change its size from other goruote
mutex.Lock()
// reference to the calendar
if event.GetCalendar() == nil || event.GetCalendar() != c {
event.SetCalendar(c)
}
// add the event to the main array with events
c.events = append(c.events, event)
// pointer to the added event in the main array
eventPtr := &c.events[len(c.events)-1]
// calculate the start day of the event
eventStartTime := event.GetStart()
tz := c.GetTimezone()
eventDate := time.Date(eventStartTime.Year(), eventStartTime.Month(), eventStartTime.Day(), 0, 0, 0, 0, &tz)
// faster search by date
c.eventsByDate[eventDate.Format(YmdHis)] = append(c.eventsByDate[eventDate.Format(YmdHis)], eventPtr)
// faster search by id
c.eventByID[event.GetID()] = eventPtr
if event.GetImportedID() != "" {
c.eventByImportedID[event.GetImportedID()] = eventPtr
}
mutex.Unlock()
return c, nil
}
// get event by id
func (c *Calendar) GetEventByID(eventID string) (*Event, error) {
event, ok := c.eventByID[eventID]
if ok {
return event, nil
}
return nil, errors.New(fmt.Sprintf("There is no event with id %s", eventID))
}
// get event by imported id
func (c *Calendar) GetEventByImportedID(eventID string) (*Event, error) {
event, ok := c.eventByImportedID[eventID]
if ok {
return event, nil
}
return nil, errors.New(fmt.Sprintf("There is no event with id %s", eventID))
}
// get all events in the calendar
func (c *Calendar) GetEvents() []Event {
return c.events
}
// get all events in the calendar ordered by date
func (c *Calendar) GetEventsByDates() map[string][]*Event {
return c.eventsByDate
}
// get all events for specified date
func (c *Calendar) GetEventsByDate(dateTime time.Time) ([]*Event, error) {
tz := c.GetTimezone()
day := time.Date(dateTime.Year(), dateTime.Month(), dateTime.Day(), 0, 0, 0, 0, &tz)
events, ok := c.eventsByDate[day.Format(YmdHis)]
if ok {
return events, nil
}
return nil, errors.New(fmt.Sprintf("There are no events for the day %s", day.Format(YmdHis)))
}
func (c *Calendar) String() string {
eventsCount := len(c.GetEvents())
name := c.GetName()
desc := c.GetDesc()
url := c.GetUrl()
return fmt.Sprintf("Calendar %s about %s has %d events. Downloaded from %s .", name, desc, eventsCount, url)
}
func (c *Calendar) SetUrl(u string) *Calendar {
c.url = u
return c
}
func (c *Calendar) GetUrl() string {
return c.url
}