-
Notifications
You must be signed in to change notification settings - Fork 20
/
dates.go
366 lines (307 loc) · 9.19 KB
/
dates.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
package utils
import (
"bytes"
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
// patterns for date and time formats supported for human-entered data
var dd_mm_yyyy = regexp.MustCompile(`([0-9]{1,2})[-.\\/_ ]([0-9]{1,2})[-.\\/_ ]([0-9]{4}|[0-9]{2})`)
var mm_dd_yyyy = regexp.MustCompile(`([0-9]{1,2})[-.\\/_ ]([0-9]{1,2})[-.\\/_ ]([0-9]{4}|[0-9]{2})`)
var yyyy_mm_dd = regexp.MustCompile(`([0-9]{4}|[0-9]{2})[-.\\/_ ]([0-9]{1,2})[-.\\/_ ]([0-9]{1,2})`)
var hh_mm_ss = regexp.MustCompile(`([0-9]{1,2}):([0-9]{2})(:([0-9]{2})(\.(\d+))?)?\W*([aApP][mM])?`)
type DateFormat string
type TimeFormat string
const (
DateFormat_yyyy_MM_dd DateFormat = "yyyy-MM-dd"
DateFormat_MM_dd_yyyy DateFormat = "MM-dd-yyyy"
DateFormat_dd_MM_yyyy DateFormat = "dd-MM-yyyy"
TimeFormat_HH_mm TimeFormat = "hh:mm"
TimeFormat_hh_mm_tt TimeFormat = "hh:mm tt"
TimeFormat_HH_mm_ss TimeFormat = "HH:mm:ss"
TimeFormat_hh_mm_ss_tt TimeFormat = "hh:mm:ss tt"
)
func (df DateFormat) String() string { return string(df) }
func (tf TimeFormat) String() string { return string(tf) }
// ZeroTime is our uninitialized time value
var ZeroTime = time.Time{}
func dateFromFormats(env Environment, currentYear int, pattern *regexp.Regexp, d int, m int, y int, str string) (time.Time, error) {
matches := pattern.FindAllStringSubmatch(str, -1)
for _, match := range matches {
// does our day look believable?
day, _ := strconv.Atoi(match[d])
if day == 0 || day > 31 {
continue
}
month, _ := strconv.Atoi(match[m])
if month == 0 || month > 12 {
continue
}
year, _ := strconv.Atoi(match[y])
// convert to four digit year if necessary
if len(match[y]) == 2 {
if year > currentYear%1000 {
year += 1900
} else {
year += 2000
}
}
// looks believable, go for it
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, env.Timezone()), nil
}
return ZeroTime, fmt.Errorf("No date found in string: %s", str)
}
// DaysBetween returns the number of calendar days (an int) between the two dates. Note
// that if these are in different timezones then the local calendar day is used for each
// and the difference is calculated from that.
func DaysBetween(date1 time.Time, date2 time.Time) int {
d1 := time.Date(date1.Year(), date1.Month(), date1.Day(), 0, 0, 0, 0, time.UTC)
d2 := time.Date(date2.Year(), date2.Month(), date2.Day(), 0, 0, 0, 0, time.UTC)
return int(d1.Sub(d2) / (time.Hour * 24))
}
// MonthsBetween returns the number of calendar months (an int) between the two dates. Note
// that if these are in different timezones then the local calendar day is used for each
// and the difference is calculated from that.
func MonthsBetween(date1 time.Time, date2 time.Time) int {
// difference in months
months := int(date1.Month() - date2.Month())
// difference in years
months += (date1.Year() - date2.Year()) * 12
return months
}
// format we use for output
var iso8601Default = "2006-01-02T15:04:05.000000Z07:00"
// generic format for parsing any 8601 date
var iso8601Format = "2006-01-02T15:04:05Z07:00"
var iso8601NoSecondsFormat = "2006-01-02T15:04Z07:00"
var isoFormats = []string{iso8601Format, iso8601NoSecondsFormat}
// DateToISO converts the passed in time.Time to a string in ISO8601 format
func DateToISO(date time.Time) string {
return date.Format(iso8601Default)
}
// DateToString converts the passed in time element to the right format based on the environment settings
func DateToString(env Environment, date time.Time) string {
goFormat, _ := ToGoDateFormat(string(env.DateFormat()) + " " + string(env.TimeFormat()))
return date.Format(goFormat)
}
// DateFromString returns a date constructed from the passed in string, or an error if we
// are unable to extract one
func DateFromString(env Environment, str string) (time.Time, error) {
// first see if we can parse in any known iso formats, if so return that
for _, format := range isoFormats {
parsed, err := time.Parse(format, str)
if err == nil {
if env.Timezone() != nil {
parsed = parsed.In(env.Timezone())
}
return parsed, nil
}
}
// otherwise, try to parse according to their env settings
parsed := ZeroTime
currentYear := time.Now().Year()
var err error
switch env.DateFormat() {
case DateFormat_yyyy_MM_dd:
parsed, err = dateFromFormats(env, currentYear, yyyy_mm_dd, 3, 2, 1, str)
case DateFormat_dd_MM_yyyy:
parsed, err = dateFromFormats(env, currentYear, dd_mm_yyyy, 1, 2, 3, str)
case DateFormat_MM_dd_yyyy:
parsed, err = dateFromFormats(env, currentYear, mm_dd_yyyy, 2, 1, 3, str)
default:
err = fmt.Errorf("unknown date format: %s", env.DateFormat())
}
// couldn't find a date? bail
if err != nil {
return parsed, err
}
// can we pull out a time?
matches := hh_mm_ss.FindAllStringSubmatch(str, -1)
for _, match := range matches {
hour, _ := strconv.Atoi(match[1])
// do we have an AM/PM
if match[7] != "" {
if strings.ToLower(match[7]) == "pm" {
hour += 12
}
}
// is this a valid hour?
if hour > 24 {
continue
}
minute, _ := strconv.Atoi(match[2])
if minute > 60 {
continue
}
seconds := 0
if match[4] != "" {
seconds, _ = strconv.Atoi(match[4])
if seconds > 60 {
continue
}
}
ns := 0
if match[6] != "" {
ns, _ = strconv.Atoi(match[6])
if len(match[6]) == 3 {
// these are milliseconds, multi by 1,000,000 for nano
ns = ns * 1000000
} else if len(match[6]) == 6 {
// these are microseconds, times 1000 for nano
ns = ns * 1000
}
}
parsed = time.Date(parsed.Year(), parsed.Month(), parsed.Day(), hour, minute, seconds, ns, env.Timezone())
break
}
// set our timezone if we have one
if env.Timezone() != nil && parsed != ZeroTime {
parsed = parsed.In(env.Timezone())
}
return parsed, nil
}
// ToGoDateFormat converts the passed in format to a GoLang format string.
//
// Format strings we support:
//
// d - day of month, 1-31
// dd - day of month, zero padded 0-31
// fff - thousandths of a second
// h - hour of the day 1-12
// hh - hour of the day 01-12
// H - hour of the day 1-23
// HH - hour of the day 01-23
// K - hour and minute offset from UTC, or Z fo UTC
// m - minute 0-59
// mm - minute 00-59
// M - month 1-12
// MM - month 01-12
// s - second 0-59
// ss - second 00-59
// TT - AM or PM
// tt - am or pm
// yy - last two digits of year 0-99
// yyyy - four digits of your 0000-9999
// zzz - hour and minute offset from UTC
// ignored chars: ' ', ':', ',', 'T', 'Z', '-', '_', '/'
func ToGoDateFormat(format string) (string, error) {
runes := []rune(format)
goFormat := bytes.Buffer{}
repeatCount := func(runes []rune, offset int, test rune) int {
count := 0
for i := offset; i < len(runes); i++ {
if runes[i] == test {
count++
} else {
break
}
}
return count
}
for i := 0; i < len(runes); i++ {
r := runes[i]
count := repeatCount(runes, i, r)
switch r {
case 'd':
if count == 1 {
goFormat.WriteString("2")
} else if count >= 2 {
goFormat.WriteString("02")
i++
}
case 'f':
if count >= 9 {
goFormat.WriteString("000000000")
i += 8
} else if count >= 6 {
goFormat.WriteString("000000")
i += 5
} else if count >= 3 {
goFormat.WriteString("000")
i += 2
} else {
return "", fmt.Errorf("invalid date format, invalid count of 'f' format: %d", count)
}
case 'h':
if count == 1 {
goFormat.WriteString("3")
} else if count >= 2 {
goFormat.WriteString("03")
i++
}
case 'H':
if count >= 2 {
goFormat.WriteString("15")
i++
} else {
return "", fmt.Errorf("invalid date format, invalid count of 'H' format: %d", count)
}
case 'K':
goFormat.WriteString("Z07:00")
case 'm':
if count == 1 {
goFormat.WriteString("4")
} else if count >= 2 {
goFormat.WriteString("04")
i++
}
case 'M':
if count == 1 {
goFormat.WriteString("1")
} else if count >= 2 {
goFormat.WriteString("01")
i++
}
case 's':
if count == 1 {
goFormat.WriteString("5")
} else if count >= 2 {
goFormat.WriteString("05")
i++
}
case 't':
if count >= 2 {
goFormat.WriteString("pm")
i++
} else {
return "", fmt.Errorf("invalid date format, invalid count of 't' format: %d", count)
}
case 'T':
if count == 1 {
goFormat.WriteString("T")
} else if count >= 2 {
goFormat.WriteString("PM")
i++
}
case 'y':
if count == 2 {
goFormat.WriteString("06")
i++
} else if count >= 4 {
goFormat.WriteString("2006")
i += 3
} else {
return "", fmt.Errorf("invalid date format, invalid count of 'y' format: %d", count)
}
case 'z':
if count == 3 {
goFormat.WriteString("-07:00")
i += 2
} else {
return "", fmt.Errorf("invalid date format, invalid count of 'z' format: %d", count)
}
case ' ', ':', '/', '.', 'Z', '-', '_':
goFormat.WriteRune(r)
default:
return "", fmt.Errorf("invalid date format, unknown format char: %c", r)
}
}
return goFormat.String(), nil
}
func DateToUTCRange(d time.Time, tz *time.Location) (time.Time, time.Time) {
localMidnight := time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
utcMidnight := localMidnight.In(tz)
return utcMidnight, utcMidnight.Add(24 * time.Hour)
}