-
Notifications
You must be signed in to change notification settings - Fork 3
/
date_and_time.go
229 lines (190 loc) · 4.23 KB
/
date_and_time.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
package rrule
import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"
"time"
"unicode"
)
var BadFormatError func(string) error = func(key string) error { return errors.New(fmt.Sprintf("Invalid Format: %s", key)) }
func ParseDateTimeChunks(s string) ([]time.Time, error) {
var dtDates []time.Time
var dtLocation *time.Location = time.UTC
var parts []string
if strings.Contains(s, ":") {
parts = strings.Split(s, ":")
for _, item := range strings.Split(parts[0], ";") {
args := strings.SplitN(item, "=", 2)
if args[0] == "TZID" {
loc, err := time.LoadLocation(args[1])
if err != nil {
return dtDates, err
}
dtLocation = loc
} else {
panic(fmt.Sprintf("Unknown key/value %s=%s", args[0], args[1]))
}
}
parts = strings.Split(parts[1], ",")
} else {
parts = strings.Split(s, ",")
}
for _, item := range parts {
dt, err := ParseDateTime(item, dtLocation)
if err != nil {
return dtDates, err
}
dtDates = append(dtDates, dt)
}
return dtDates, nil
}
func ParseDateTime(s string, InLocation *time.Location) (time.Time, error) {
now := time.Now()
s_len := len(s)
if s_len < 8 {
return now, BadFormatError("Too short")
}
year := s[0:4]
month := s[4:6]
day := s[6:8]
y, err := strconv.ParseInt(year, 10, 32)
if err != nil {
return now, err
}
m, err := strconv.ParseInt(month, 10, 32)
if err != nil {
return now, err
}
d, err := strconv.ParseInt(day, 10, 32)
if err != nil {
return now, err
}
if len(s) == 8 {
return time.Date(int(y), time.Month(m), int(d), 0, 0, 0, 0, time.UTC), nil
}
if s[8] != 'T' {
return now, BadFormatError("No T for time")
}
if s_len < 15 || s_len > 16 {
return now, BadFormatError("not correct lenght for datetime")
}
hours := s[9:11]
minutes := s[11:13]
seconds := s[13:15]
is_utc := strings.HasSuffix(s, "Z")
hrs, err := strconv.ParseInt(hours, 10, 32)
if err != nil {
return now, err
}
min, err := strconv.ParseInt(minutes, 10, 32)
if err != nil {
return now, err
}
sec, err := strconv.ParseInt(seconds, 10, 32)
if err != nil {
return now, err
}
var locale *time.Location = InLocation
if is_utc {
locale = time.UTC
}
return time.Date(
int(y),
time.Month(m),
int(d),
int(hrs),
int(min),
int(sec), 0, locale), nil
}
func ParseDuration(s string) (time.Duration, error) {
no_duration := time.Duration(0)
var pos_or_neg int64 = 1
if s[0] == '-' {
pos_or_neg = -1
s = s[1:]
}
var chunk bytes.Buffer
if s[0] != 'P' {
return no_duration, BadFormatError(s)
}
s = s[1:]
var durationSeconds int64 = 0
for _, c := range s {
if unicode.IsDigit(c) {
chunk.WriteRune(c)
} else if c == 'T' {
// pass
} else {
value, err := strconv.ParseInt(chunk.String(), 10, 64)
if err != nil {
return no_duration, BadFormatError(fmt.Sprintf("%d", value))
}
switch c {
case 'W':
durationSeconds += (value * 60 * 60 * 24 * 7)
case 'D':
durationSeconds += (value * 60 * 60 * 24)
case 'H':
durationSeconds += (value * 60 * 60)
case 'M':
durationSeconds += (value * 60)
case 'S':
durationSeconds += (value)
}
chunk.Reset()
}
}
durationSeconds = durationSeconds * pos_or_neg
return time.Duration(durationSeconds) * time.Second, nil
}
func DateTimeToString(t time.Time) string {
if t.Location() == time.UTC {
return fmt.Sprintf("%04d%02d%02dT%02d%02d%02dZ",
t.Year(),
int(t.Month()),
t.Day(),
t.Hour(),
t.Minute(),
t.Second(),
)
} else {
return fmt.Sprintf("%04d%02d%02dT%02d%02d%02d",
t.Year(),
int(t.Month()),
t.Day(),
t.Hour(),
t.Minute(),
t.Second(),
)
}
}
func DatesToList(times []time.Time) string {
if len(times) == 0 {
return ""
}
var loc = times[0].Location()
var correctedTimes []time.Time = []time.Time{times[0]}
// All times must be in the same timezone.
for _, t2 := range times[1:] {
correctedTimes = append(correctedTimes, t2.In(loc))
}
var correctedTimeStrings []string
for _, ct := range correctedTimes {
correctedTimeStrings = append(correctedTimeStrings,
DateTimeToString(ct))
}
if loc == time.UTC {
return fmt.Sprintf(
":%s",
strings.Join(correctedTimeStrings, ","),
)
} else {
return fmt.Sprintf(
";TZID=%s:%s",
loc.String(),
strings.Join(correctedTimeStrings, ","),
)
}
}