-
Notifications
You must be signed in to change notification settings - Fork 9
/
datetime_patterns.go
178 lines (167 loc) · 3.69 KB
/
datetime_patterns.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
package php
import (
"errors"
"time"
)
// pattern stores the mapping of golang datetime layout and php datetime format
type pattern struct {
regexp string // golang regular expression
layout string // golang datetime layout
format string // php datetime format
}
// patterns is an array of pattern
type patterns []pattern
var _defaultPatterns patterns
// formatMap is the mapping of php date format to golang layout
var formatMap map[string]string
func init() {
_defaultPatterns = patterns{
pattern{
regexp: "^\\d{4}-\\d{2}-\\d{2}$",
layout: "2006-01-02",
format: "Y-m-d",
},
pattern{
regexp: "^\\d{4}-\\d{2}-\\d{1}$",
layout: "2006-01-2",
format: "Y-m-j",
},
pattern{
regexp: "^\\d{4}-\\d{1}-\\d{2}$",
layout: "2006-1-02",
format: "Y-n-d",
},
pattern{
regexp: "^\\d{4}-\\d{1}-\\d{1,2}$",
layout: "2006-1-2",
format: "Y-n-j",
},
pattern{
regexp: "^\\d{2}-\\d{2}-\\d{2}$",
layout: "06-01-02",
format: "y-m-d",
},
pattern{
regexp: "^\\d{2}-\\d{2}-\\d{1}$",
layout: "06-01-2",
format: "y-m-j",
},
pattern{
regexp: "^\\d{2}-\\d{1}-\\d{2}$",
layout: "06-1-02",
format: "y-n-d",
},
pattern{
regexp: "^\\d{2}-\\d{1}-\\d{1,2}$",
layout: "06-1-2",
format: "y-n-j",
},
pattern{
regexp: "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$",
layout: "2006-01-02 15:04:05",
format: "Y-m-d H:i:s",
},
pattern{
regexp: "^\\d{4}-\\d{2}-\\d{1} \\d{2}:\\d{2}:\\d{2}$",
layout: "2006-01-2 15:04:05",
format: "Y-m-j H:i:s",
},
pattern{
regexp: "^\\d{4}-\\d{1}-\\d{2} \\d{2}:\\d{2}:\\d{2}$",
layout: "2006-1-02 15:04:05",
format: "Y-n-d H:i:s",
},
pattern{
regexp: "^\\d{4}-\\d{1}-\\d{1} \\d{2}:\\d{2}:\\d{2}$",
layout: "2006-1-2 15:04:05",
format: "Y-n-j H:i:s",
},
pattern{
regexp: "^\\d{2}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$",
layout: "06-01-02 15:04:05",
format: "y-m-d H:i:s",
},
pattern{
regexp: "^\\d{2}-\\d{2}-\\d{1} \\d{2}:\\d{2}:\\d{2}$",
layout: "06-01-2 15:04:05",
format: "y-m-j H:i:s",
},
pattern{
regexp: "^\\d{2}-\\d{1}-\\d{2} \\d{2}:\\d{2}:\\d{2}$",
layout: "06-1-02 15:04:05",
format: "y-n-d H:i:s",
},
pattern{
regexp: "^\\d{2}-\\d{1}-\\d{1} \\d{2}:\\d{2}:\\d{2}$",
layout: "06-1-2 15:04:05",
format: "y-n-j H:i:s",
},
pattern{
regexp: "^\\d{8}$",
layout: "20060102",
format: "Ymd",
},
pattern{
regexp: "^\\d{14}$",
layout: "20060102150405",
format: "YmdHis",
},
pattern{
regexp: "^\\d{4}$",
layout: "2006",
format: "Y",
},
pattern{
regexp: "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}[+-]\\d{2}:\\d{2}$",
layout: "2006-01-02T15:04:05-07:00",
format: "c", // ISO 8601 date
},
pattern{
regexp: "^[[:alpha:]]{3}, \\d{2} [[:alpha:]]{3} \\d{4} \\d{2}:\\d{2}:\\d{2} [+-]\\d{4}$",
layout: time.RFC1123Z,
format: "r", // RFC 2822 formatted date
},
}
formatMap = map[string]string{
"d": "02",
"j": "2",
"m": "01",
"n": "1",
"Y": "2006",
"y": "06",
"H": "15",
"h": "03",
"g": "3",
"i": "04",
"s": "05",
"M": "Jan",
"F": "January",
"D": "Mon",
"l": "Monday",
"e": "MST",
"T": "MST",
"O": "-0700",
"P": "-07:00",
}
}
// getPattern gets the matched pattern by the given php style date/time format string
func getPattern(format string) (pattern, error) {
for _, p := range _defaultPatterns {
if p.format == format {
return p, nil
}
}
return pattern{}, errors.New("No pattern found")
}
// convertLayout converts php date format string to golang date layout
func convertLayout(format string) string {
var layout string
for _, s := range format {
if v, ok := formatMap[string(s)]; ok {
layout += v
} else {
layout += string(s)
}
}
return layout
}