-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent.go
111 lines (96 loc) · 2.34 KB
/
event.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
package ical
import (
"fmt"
"log"
"strings"
"time"
)
// Event struct type is a iCalendar component, defined
// as VEVENT in RFC 5545
type Event struct {
// Required
UID string
DTSTAMP *time.Time
// Required only if Calendar object does not
// specify the METHOD property
DTSTART *time.Time
// Optional
CLASS string
CREATED *time.Time
DESCRIPTION string
GEO float64
LASTMOD *time.Time // LAST-MOD
LOCATION string
ORGANIZER string // TODO: SPECIAL TYPE: CAL_ADDRESS
PRIORITY uint8
SEQ uint8
STATUS string
SUMMARY string
TRANSP string
URL string
RECURID string // TODO: SPECIAL TYPE!
RRULE string // TODO: SPECIAL TYPE!
// Optional but should not be declared together
DTEND *time.Time
DURATION string // TODO: SPECIAL TYPE PT0H0M0S
// Optional and supports multiple declarations
ATTACH []string
ATTENDEE []string // TODO: SPECIAL TYPE: CAL_ADDRESS
CATEGORIES []string
COMMENT []string
CONTACT []string
EXDATE []*time.Time
RSTATUS []string
RELATED []string
RESOURCES []string
RDATE []*time.Time
XPROP []string // X-PROP
IANAPROP []string // IANA-PROP
}
// NewEvent creates an instance of struct Event
func NewEvent() *Event {
// Creates a new instance
e := new(Event)
// Get timestamp
currentTimestamp := time.Now()
// Assign struct values
e.UID = fmt.Sprintf("%[email protected]", FormatDateTime(currentTimestamp))
e.DTSTAMP = ¤tTimestamp
return e
}
func (e *Event) isReady() bool {
if e.UID == "" {
return false
}
if e.DTSTAMP == nil {
return false
}
if e.DTSTART == nil {
return false
}
if e.DTEND != nil && e.DURATION != "" {
return false
}
return true
}
// GenerateEventProp method creates .ics contents
func (e *Event) GenerateEventProp() string {
// Validate first
status := e.isReady()
if !status {
log.Fatal("Event is not ready!")
}
// Create object
var str strings.Builder
// Write headers
str.WriteString("BEGIN:VEVENT\r\n")
// Write required params
str.WriteString("UID:" + e.UID + "\r\n")
str.WriteString("DTSTAMP:" + FormatDateTime(*e.DTSTAMP) + "\r\n")
str.WriteString("DTSTART:" + FormatDateTime(*e.DTSTART) + "\r\n")
// Write optional params
str.WriteString("SUMMARY:" + e.SUMMARY + "\r\n")
// Write footers
str.WriteString("END:VEVENT\r\n")
return str.String()
}