-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalendar.go
71 lines (60 loc) · 1.3 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
package ical
import (
"log"
"strings"
)
// Calendar struct follows the iCalendar object,
// defined as VCALENDAR in RFC 5545
type Calendar struct {
// Required
PRODID string
VERSION string
// Optional
CALSCALE string
METHOD string
// Optional
EVENT []Event
TODO []string // TODO
JOURNAL []string // TODO
FREEBUSY []string // TODO
TIMEZONE []string // TODO
ALARM []string // TODO
}
// NewCalendar creates an instance of struct Calendar
func NewCalendar() *Calendar {
c := new(Calendar)
c.PRODID = "-//loozhengyuan//ical//EN"
c.VERSION = "2.0"
return c
}
func (c *Calendar) isReady() bool {
if c.VERSION == "" {
return false
}
if c.PRODID == "" {
return false
}
return true
}
// GenerateCalendarProp method creates .ics contents
func (c *Calendar) GenerateCalendarProp() string {
// Validate first
status := c.isReady()
if !status {
log.Fatal("Event is not ready!")
}
// Create object
var str strings.Builder
// Write headers
str.WriteString("BEGIN:VCALENDAR\r\n")
// Write required params
str.WriteString("VERSION:" + c.VERSION + "\r\n")
str.WriteString("PRODID:" + c.PRODID + "\r\n")
// Loop EVENT if exits
for _, event := range c.EVENT {
str.WriteString(event.GenerateEventProp())
}
// Write footers
str.WriteString("END:VCALENDAR\r\n")
return str.String()
}