-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcalendar.go
84 lines (69 loc) · 1.43 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
package main
import (
"io"
"os"
"time"
"bytes"
"text/template"
)
type Holiday struct {
Name string
StartDate string
EndDate string
DTStamp string
Rest bool
}
type Calendar struct {
Name string
Color string
Holidays []Holiday
t *template.Template
prefix string
suffix string
}
func NewCalendar(name string, color string) *Calendar {
t := template.Must(template.New("iCalendar").ParseFiles("icalendar.tmpl"))
return &Calendar{
Name: name,
Color: color,
t: t,
}
}
func NewWorkCalendar() *Calendar {
c := NewCalendar("调休", "#DD2222")
c.SetSuffix("(班)")
return c
}
func NewOffCalendar() *Calendar {
c := NewCalendar("节假日", "#22DD22")
c.SetSuffix("(休)")
return c
}
func (c *Calendar) SetPrefix(prefix string) {
c.prefix = prefix
}
func (c *Calendar) SetSuffix(suffix string) {
c.suffix = suffix
}
func (c *Calendar) Render(writer io.Writer) {
c.t.ExecuteTemplate(writer, "icalendar.tmpl", c)
}
func (c *Calendar) RenderFile(path string) {
f, _ := os.Create(path)
defer f.Close()
c.Render(f)
}
func (c *Calendar) RenderString() string {
var b bytes.Buffer
c.Render(&b)
return b.String()
}
func (c *Calendar) AddHoliday(name string, date time.Time, rest bool) {
c.Holidays = append(c.Holidays, Holiday{
Name: c.prefix + name + c.suffix,
StartDate: date.Format("20060102"),
EndDate: date.AddDate(0, 0, 1).Format("20060102"),
DTStamp: date.Format("20060102T150405Z"),
Rest: rest,
})
}