-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
72 lines (63 loc) · 2.09 KB
/
parse.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
package dtf
import (
"errors"
"strings"
"time"
)
// Parse generate time.Time from W3C-DTF string
func Parse(timeStr string) (time.Time, error) {
err := errors.New("provided string is not W3C-DTF format")
if !strings.Contains(timeStr, "T") {
switch true {
case IsYear(timeStr):
return ParseYear(timeStr)
case IsYearAndMonth(timeStr):
return ParseYearAndMonth(timeStr)
case IsCompleteDate(timeStr):
return ParseCompleteDate(timeStr)
default:
return time.Time{}, err
}
} else {
switch true {
case IsCompleteDateWithMinutes(timeStr):
return ParseCompleteDateWithMinutes(timeStr)
case IsCompleteDateWithSeconds(timeStr):
return ParseCompleteDateWithSeconds(timeStr)
case IsCompleteDateWithFractionOfSecond(timeStr):
return ParseCompleteDateWithFractionOfSecond(timeStr)
default:
return time.Time{}, err
}
}
}
// ParseYear generate time.Time from 'YYYY' string
func ParseYear(timeStr string) (time.Time, error) {
return time.Parse("2006", timeStr)
}
// ParseYearAndMonth generate time.Time from 'YYYY-MM' string
func ParseYearAndMonth(timeStr string) (time.Time, error) {
return time.Parse("2006-01", timeStr)
}
// ParseCompleteDate generate time.Time from 'YYYY-MM-DD' string
func ParseCompleteDate(timeStr string) (time.Time, error) {
return time.Parse("2006-01-02", timeStr)
}
// ParseCompleteDateWithMinutes generate time.Time from 'YYYY-MM-DDThh:ii+00:00'
func ParseCompleteDateWithMinutes(timeStr string) (time.Time, error) {
if IsUTC(timeStr) {
timeStr = rUTC.ReplaceAllString(timeStr, "+00:00")
}
return time.Parse("2006-01-02T15:04-07:00", timeStr)
}
// ParseCompleteDateWithSeconds generate time.Time from 'YYYY-MM-DDThh:ii:ss+00:00'
func ParseCompleteDateWithSeconds(timeStr string) (time.Time, error) {
if IsUTC(timeStr) {
timeStr = rUTC.ReplaceAllString(timeStr, "+00:00")
}
return time.Parse("2006-01-02T15:04:05-07:00", timeStr)
}
// ParseCompleteDateWithFractionOfSecond generate time.Time from 'YYYY-MM-DDThh:ii:ss.s+00:00'
func ParseCompleteDateWithFractionOfSecond(timeStr string) (time.Time, error) {
return ParseCompleteDateWithSeconds(timeStr)
}