forked from ahuret/caldav-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
datetime.go
439 lines (395 loc) · 17 KB
/
datetime.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package values
import (
"fmt"
"log"
"strings"
"time"
"github.com/iPaladinLLC/caldav-go/icalendar/properties"
"github.com/iPaladinLLC/caldav-go/utils"
)
var _ = log.Print
var tzidDict map[string]string
const DateFormatString = "20060102"
const DateTimeFormatString = "20060102T150405"
const UTCDateTimeFormatString = "20060102T150405Z"
// a representation of a date and time for iCalendar
type DateTime struct {
t time.Time
fullTime bool
SkipAddUTC bool // if true, don't add trailing "Z" for UTC times
}
type DateTimeFullDay DateTime
type DateTimes []*DateTime
// The exception dates, if specified, are used in computing the recurrence set. The recurrence set is the complete set
// of recurrence instances for a calendar component. The recurrence set is generated by considering the initial
// "DTSTART" property along with the "RRULE", "RDATE", "EXDATE" and "EXRULE" properties contained within the iCalendar
// object. The "DTSTART" property defines the first instance in the recurrence set. Multiple instances of the "RRULE"
// and "EXRULE" properties can also be specified to define more sophisticated recurrence sets. The final recurrence set
// is generated by gathering all of the start date-times generated by any of the specified "RRULE" and "RDATE"
// properties, and then excluding any start date and times which fall within the union of start date and times
// generated by any specified "EXRULE" and "EXDATE" properties. This implies that start date and times within exclusion
// related properties (i.e., "EXDATE" and "EXRULE") take precedence over those specified by inclusion properties
// (i.e., "RDATE" and "RRULE"). Where duplicate instances are generated by the "RRULE" and "RDATE" properties, only
// one recurrence is considered. Duplicate instances are ignored.
//
// The "EXDATE" property can be used to exclude the value specified in "DTSTART". However, in such cases the original
// "DTSTART" date MUST still be maintained by the calendaring and scheduling system because the original "DTSTART"
// value has inherent usage dependencies by other properties such as the "RECURRENCE-ID".
type ExceptionDateTimes DateTimes
// The recurrence dates, if specified, are used in computing the recurrence set. The recurrence set is the complete set
// of recurrence instances for a calendar component. The recurrence set is generated by considering the initial
// "DTSTART" property along with the "RRULE", "RDATE", "EXDATE" and "EXRULE" properties contained within the iCalendar
// object. The "DTSTART" property defines the first instance in the recurrence set. Multiple instances of the "RRULE"
// and "EXRULE" properties can also be specified to define more sophisticated recurrence sets. The final recurrence set
// is generated by gathering all of the start date-times generated by any of the specified "RRULE" and "RDATE"
// properties, and then excluding any start date and times which fall within the union of start date and times
// generated by any specified "EXRULE" and "EXDATE" properties. This implies that start date and times within exclusion
// related properties (i.e., "EXDATE" and "EXRULE") take precedence over those specified by inclusion properties
// (i.e., "RDATE" and "RRULE"). Where duplicate instances are generated by the "RRULE" and "RDATE" properties, only
// one recurrence is considered. Duplicate instances are ignored.
type RecurrenceDateTimes DateTimes
// creates a new icalendar datetime representation
func NewDateTime(t time.Time) *DateTime {
return &DateTime{t: t.Truncate(time.Second), fullTime: true}
}
func NewDateTimeDate(t time.Time) *DateTime {
return &DateTime{t: t.Truncate(time.Second), fullTime: false}
}
// creates a new icalendar datetime array representation
func NewDateTimes(dates ...*DateTime) DateTimes {
return DateTimes(dates)
}
// creates a new icalendar datetime representation
func NewDateTimeFullDay(t time.Time) *DateTimeFullDay {
datetimes := NewDateTimeDate(t)
return (*DateTimeFullDay)(datetimes)
}
// creates a new icalendar datetime array representation
func NewExceptionDateTimes(dates ...*DateTime) *ExceptionDateTimes {
datetimes := NewDateTimes(dates...)
return (*ExceptionDateTimes)(&datetimes)
}
// creates a new icalendar datetime array representation
func NewRecurrenceDateTimes(dates ...*DateTime) *RecurrenceDateTimes {
datetimes := NewDateTimes(dates...)
return (*RecurrenceDateTimes)(&datetimes)
}
// checks to see if two datetimes are equal
func (d *DateTime) Equals(test *DateTime) bool {
return d.t.Equal(test.t)
}
// returns the native time for the datetime object
func (d *DateTime) NativeTime() time.Time {
return d.t
}
// encodes the datetime value for the iCalendar specification
func (d *DateTime) EncodeICalValue() (string, error) {
var val string
if d.fullTime {
val = d.t.Format(DateTimeFormatString)
loc := d.t.Location()
if loc == time.UTC && !d.SkipAddUTC {
val = fmt.Sprintf("%sZ", val)
}
} else {
val = d.t.Format(DateFormatString)
}
return val, nil
}
// decodes the datetime value from the iCalendar specification
func (d *DateTime) DecodeICalValue(value string) error {
layout := DateTimeFormatString
d.fullTime = true
if strings.HasSuffix(value, "Z") {
layout = UTCDateTimeFormatString
} else if len(value) == 15 {
layout = DateTimeFormatString
} else if len(value) == 8 {
layout = DateFormatString
d.fullTime = false
}
var err error
d.t, err = time.ParseInLocation(layout, value, time.UTC)
if err != nil {
return utils.NewError(d.DecodeICalValue, "unable to parse datetime value", d, err)
} else {
return nil
}
}
// encodes the datetime params for the iCalendar specification
func (d *DateTime) EncodeICalParams() (params properties.Params, err error) {
loc := d.t.Location()
if loc != time.UTC {
params = properties.Params{{properties.TimeZoneIdPropertyName, loc.String()}}
}
return
}
// decodes the datetime params from the iCalendar specification
func (d *DateTime) DecodeICalParams(params properties.Params) error {
var name string
layout := DateTimeFormatString
value := d.t.Format(layout)
for n, param := range params {
if param.Name == properties.TimeZoneIdPropertyName {
name = param.Value
break
}
if n == len(params)-1 {
return nil
}
}
loc, err := time.LoadLocation(name)
if err != nil {
// Attempt to get Olson's time
olson := tzidDict[name]
if olson != "" {
loc, err = time.LoadLocation(olson)
if err != nil {
return utils.NewError(d.DecodeICalValue, "unable to parse timezone after converting to Olson's time", d, err)
}
} else {
return utils.NewError(d.DecodeICalValue, "unable to parse timezone", d, err)
}
}
if t, err := time.ParseInLocation(layout, value, loc); err != nil {
return utils.NewError(d.DecodeICalValue, "unable to parse datetime value", d, err)
} else {
d.t = t
return nil
}
return nil
}
func loadLocation(name string) (*time.Location, error) {
loc, err := time.LoadLocation(name)
if err != nil {
return time.UTC, nil
}
return loc, nil
}
// validates the datetime value against the iCalendar specification
func (d *DateTime) ValidateICalValue() error {
loc := d.t.Location()
if loc.String() == "Local" {
msg := "DateTime location may not Local, please use UTC or explicit Location"
return utils.NewError(d.ValidateICalValue, msg, d, nil)
}
if loc.String() == "" {
msg := "DateTime location must have a valid name"
return utils.NewError(d.ValidateICalValue, msg, d, nil)
}
return nil
}
// encodes the datetime value for the iCalendar specification
func (d *DateTime) String() string {
if s, err := d.EncodeICalValue(); err != nil {
panic(err)
} else {
return s
}
}
// encodes the datetime for full day value for the iCalendar specification
func (d *DateTimeFullDay) EncodeICalValue() (string, error) {
val := d.t.Format(DateFormatString)
loc := d.t.Location()
if loc == time.UTC && !d.SkipAddUTC {
val = fmt.Sprintf("%sZ", val)
}
return val, nil
}
// encodes a list of datetime values for the iCalendar specification
func (ds *DateTimes) EncodeICalValue() (string, error) {
var csv CSV
for i, d := range *ds {
if s, err := d.EncodeICalValue(); err != nil {
msg := fmt.Sprintf("unable to encode datetime at index %d", i)
return "", utils.NewError(ds.EncodeICalValue, msg, ds, err)
} else {
csv = append(csv, s)
}
}
return csv.EncodeICalValue()
}
// encodes a list of datetime params for the iCalendar specification
func (ds *DateTimes) EncodeICalParams() (params properties.Params, err error) {
if len(*ds) > 0 {
params, err = (*ds)[0].EncodeICalParams()
}
return
}
// decodes a list of datetime params from the iCalendar specification
func (ds *DateTimes) DecodeICalParams(params properties.Params) error {
for i, d := range *ds {
if err := d.DecodeICalParams(params); err != nil {
msg := fmt.Sprintf("unable to decode datetime params for index %d", i)
return utils.NewError(ds.DecodeICalValue, msg, ds, err)
}
}
return nil
}
// encodes a list of datetime values for the iCalendar specification
func (ds *DateTimes) DecodeICalValue(value string) error {
csv := new(CSV)
if err := csv.DecodeICalValue(value); err != nil {
return utils.NewError(ds.DecodeICalValue, "unable to decode datetime list as CSV", ds, err)
}
for i, value := range *csv {
d := new(DateTime)
if err := d.DecodeICalValue(value); err != nil {
msg := fmt.Sprintf("unable to decode datetime at index %d", i)
return utils.NewError(ds.DecodeICalValue, msg, ds, err)
} else {
*ds = append(*ds, d)
}
}
return nil
}
// encodes exception date times property name for icalendar
func (e *ExceptionDateTimes) EncodeICalName() (properties.PropertyName, error) {
return properties.ExceptionDateTimesPropertyName, nil
}
// encodes recurrence date times property name for icalendar
func (r *RecurrenceDateTimes) EncodeICalName() (properties.PropertyName, error) {
return properties.RecurrenceDateTimesPropertyName, nil
}
// encodes exception date times property value for icalendar
func (e *ExceptionDateTimes) EncodeICalValue() (string, error) {
return (*DateTimes)(e).EncodeICalValue()
}
// encodes recurrence date times property value for icalendar
func (r *RecurrenceDateTimes) EncodeICalValue() (string, error) {
return (*DateTimes)(r).EncodeICalValue()
}
// decodes exception date times property value for icalendar
func (e *ExceptionDateTimes) DecodeICalValue(value string) error {
return (*DateTimes)(e).DecodeICalValue(value)
}
// decodes recurrence date times property value for icalendar
func (r *RecurrenceDateTimes) DecodeICalValue(value string) error {
return (*DateTimes)(r).DecodeICalValue(value)
}
// encodes exception date times property params for icalendar
func (e *ExceptionDateTimes) EncodeICalParams() (params properties.Params, err error) {
return (*DateTimes)(e).EncodeICalParams()
}
// encodes recurrence date times property params for icalendar
func (r *RecurrenceDateTimes) EncodeICalParams() (params properties.Params, err error) {
return (*DateTimes)(r).EncodeICalParams()
}
// encodes exception date times property params for icalendar
func (e *ExceptionDateTimes) DecodeICalParams(params properties.Params) error {
return (*DateTimes)(e).DecodeICalParams(params)
}
// encodes recurrence date times property params for icalendar
func (r *RecurrenceDateTimes) DecodeICalParams(params properties.Params) error {
return (*DateTimes)(r).DecodeICalParams(params)
}
func init() {
tzidDict = map[string]string{
"AUS Central Standard Time": "Australia/Darwin",
"AUS Eastern Standard Time": "Australia/Sydney",
"Afghanistan Standard Time": "Asia/Kabul",
"Alaskan Standard Time": "America/Anchorage",
"Arab Standard Time": "Asia/Riyadh",
"Arabian Standard Time": "Asia/Dubai",
"Arabic Standard Time": "Asia/Baghdad",
"Argentina Standard Time": "America/Buenos_Aires",
"Atlantic Standard Time": "America/Halifax",
"Azerbaijan Standard Time": "Asia/Baku",
"Azores Standard Time": "Atlantic/Azores",
"Bahia Standard Time": "America/Bahia",
"Bangladesh Standard Time": "Asia/Dhaka",
"Belarus Standard Time": "Europe/Minsk",
"Canada Central Standard Time": "America/Regina",
"Cape Verde Standard Time": "Atlantic/Cape_Verde",
"Caucasus Standard Time": "Asia/Yerevan",
"Cen. Australia Standard Time": "Australia/Adelaide",
"Central America Standard Time": "America/Guatemala",
"Central Asia Standard Time": "Asia/Almaty",
"Central Brazilian Standard Time": "America/Cuiaba",
"Central Europe Standard Time": "Europe/Budapest",
"Central European Standard Time": "Europe/Warsaw",
"Central Pacific Standard Time": "Pacific/Guadalcanal",
"Central Standard Time": "America/Chicago",
"Central Standard Time (Mexico)": "America/Mexico_City",
"China Standard Time": "Asia/Shanghai",
"Dateline Standard Time": "Etc/GMT+12",
"E. Africa Standard Time": "Africa/Nairobi",
"E. Australia Standard Time": "Australia/Brisbane",
"E. Europe Standard Time": "Europe/Chisinau",
"E. South America Standard Time": "America/Sao_Paulo",
"Eastern Standard Time": "America/New_York",
"Eastern Standard Time (Mexico)": "America/Cancun",
"Egypt Standard Time": "Africa/Cairo",
"Ekaterinburg Standard Time": "Asia/Yekaterinburg",
"FLE Standard Time": "Europe/Kiev",
"Fiji Standard Time": "Pacific/Fiji",
"GMT Standard Time": "Europe/London",
"GTB Standard Time": "Europe/Bucharest",
"Georgian Standard Time": "Asia/Tbilisi",
"Greenland Standard Time": "America/Godthab",
"Greenwich Standard Time": "Atlantic/Reykjavik",
"Hawaiian Standard Time": "Pacific/Honolulu",
"India Standard Time": "Asia/Calcutta",
"Iran Standard Time": "Asia/Tehran",
"Israel Standard Time": "Asia/Jerusalem",
"Jordan Standard Time": "Asia/Amman",
"Kaliningrad Standard Time": "Europe/Kaliningrad",
"Korea Standard Time": "Asia/Seoul",
"Libya Standard Time": "Africa/Tripoli",
"Line Islands Standard Time": "Pacific/Kiritimati",
"Magadan Standard Time": "Asia/Magadan",
"Mauritius Standard Time": "Indian/Mauritius",
"Middle East Standard Time": "Asia/Beirut",
"Montevideo Standard Time": "America/Montevideo",
"Morocco Standard Time": "Africa/Casablanca",
"Mountain Standard Time": "America/Denver",
"Mountain Standard Time (Mexico)": "America/Chihuahua",
"Myanmar Standard Time": "Asia/Rangoon",
"N. Central Asia Standard Time": "Asia/Novosibirsk",
"Namibia Standard Time": "Africa/Windhoek",
"Nepal Standard Time": "Asia/Katmandu",
"New Zealand Standard Time": "Pacific/Auckland",
"Newfoundland Standard Time": "America/St_Johns",
"North Asia East Standard Time": "Asia/Irkutsk",
"North Asia Standard Time": "Asia/Krasnoyarsk",
"North Korea Standard Time": "Asia/Pyongyang",
"Pacific SA Standard Time": "America/Santiago",
"Pacific Standard Time": "America/Los_Angeles",
"Pakistan Standard Time": "Asia/Karachi",
"Paraguay Standard Time": "America/Asuncion",
"Romance Standard Time": "Europe/Paris",
"Russia Time Zone 10": "Asia/Srednekolymsk",
"Russia Time Zone 11": "Asia/Kamchatka",
"Russia Time Zone 3": "Europe/Samara",
"Russian Standard Time": "Europe/Moscow",
"SA Eastern Standard Time": "America/Cayenne",
"SA Pacific Standard Time": "America/Bogota",
"SA Western Standard Time": "America/La_Paz",
"SE Asia Standard Time": "Asia/Bangkok",
"Samoa Standard Time": "Pacific/Apia",
"Singapore Standard Time": "Asia/Singapore",
"South Africa Standard Time": "Africa/Johannesburg",
"Sri Lanka Standard Time": "Asia/Colombo",
"Syria Standard Time": "Asia/Damascus",
"Taipei Standard Time": "Asia/Taipei",
"Tasmania Standard Time": "Australia/Hobart",
"Tokyo Standard Time": "Asia/Tokyo",
"Tonga Standard Time": "Pacific/Tongatapu",
"Turkey Standard Time": "Europe/Istanbul",
"US Eastern Standard Time": "America/Indianapolis",
"US Mountain Standard Time": "America/Phoenix",
"UTC": "Etc/GMT",
"UTC+12": "Etc/GMT-12",
"UTC-02": "Etc/GMT+2",
"UTC-11": "Etc/GMT+11",
"Ulaanbaatar Standard Time": "Asia/Ulaanbaatar",
"Venezuela Standard Time": "America/Caracas",
"Vladivostok Standard Time": "Asia/Vladivostok",
"W. Australia Standard Time": "Australia/Perth",
"W. Central Africa Standard Time": "Africa/Lagos",
"W. Europe Standard Time": "Europe/Berlin",
"West Asia Standard Time": "Asia/Tashkent",
"West Pacific Standard Time": "Pacific/Port_Moresby",
"Yakutsk Standard Time": "Asia/Yakutsk",
}
}