-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseFuncs.go
More file actions
277 lines (223 loc) · 6.26 KB
/
parseFuncs.go
File metadata and controls
277 lines (223 loc) · 6.26 KB
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
package gotime
import (
"regexp"
"strings"
"time"
"github.com/emylincon/golist"
)
func match(pattern, input string) bool {
boolean, _ := regexp.MatchString(pattern, input)
return boolean
}
func getYear(year int) int {
if year >= 69 && year <= 99 { // Unix time starts Dec 31 1969 in some time zones
year += 1900
} else if year < 69 {
year += 2000
}
return year
}
func getMonth(month string) time.Month {
switch strings.ToLower(month)[:3] {
case "jan":
return time.January
case "feb":
return time.February
case "mar":
return time.March
case "apr":
return time.April
case "may":
return time.May
case "jun":
return time.June
case "jul":
return time.July
case "aug":
return time.August
case "sep":
return time.September
case "oct":
return time.October
case "nov":
return time.November
case "dec":
return time.December
}
return time.January
}
func getTimeZone(timeZone string) *time.Location {
timeLocation, err := time.LoadLocation(timeZone)
if err != nil {
return time.UTC
}
return timeLocation
}
// getTimeZoneInt
func getTimeZoneInt(hour, minutes int) *time.Location {
secondsOfUTC := int((time.Duration(hour) * time.Hour).Seconds() + (time.Duration(minutes) * time.Minute).Seconds())
return time.FixedZone("UTC", secondsOfUTC)
}
// formatPattern1 2022/10/12, 2022-10-12, 2020 10 12
func formatPattern1(stringTime string) (parsedTime time.Time, err error) {
s := regexp.MustCompile(`(-|/|[[:space:]])`).Split(stringTime, 3)
list := golist.NewList(s)
arr, err := list.ConvertToSliceInt()
if err != nil {
return time.Time{}, err
}
year := getYear(arr[0])
return time.Date(year, time.Month(arr[1]), arr[2], 0, 0, 0, 0, time.UTC), nil
}
// formatPattern2 12/10/2022, 12-10-2022, 25 07 2016
func formatPattern2(stringTime string) (parsedTime time.Time, err error) {
s := regexp.MustCompile(`(-|/|[[:space:]])`).Split(stringTime, 3)
list := golist.NewList(s)
arr, err := list.ConvertToSliceInt()
if err != nil {
return time.Time{}, err
}
year := getYear(arr[2])
return time.Date(year, time.Month(arr[1]), arr[0], 0, 0, 0, 0, time.UTC), nil
}
// formatPattern3 01 Jan 1970 00:00:00 GMT, 02 Jan 06 15:04 MST
func formatPattern3(stringTime string) (parsedTime time.Time, err error) {
s := regexp.MustCompile(`[[:space:]]`).Split(stringTime, 5)
timeZone := getTimeZone(s[len(s)-1])
list := golist.NewList([]string{s[0]})
list.Append(s[2])
timeStamp := strings.Split(s[3], ":")
list, _ = list.Add(golist.NewList(timeStamp))
arr, err := list.ConvertToSliceInt()
if err != nil {
return time.Time{}, err
}
year := getYear(arr[1])
month := getMonth(s[1])
seconds := 0
if len(arr) >= 5 {
seconds = arr[4]
}
return time.Date(year, month, arr[0], arr[2], arr[3], seconds, 0, timeZone), nil
}
// formatPattern4 2011-10-10T14:48:00, 2011-10-10T14:48:00 GMT
func formatPattern4(stringTime string) (parsedTime time.Time, err error) {
s := regexp.MustCompile(`(-|/|[[:space:]])`).Split(stringTime, 4)
timeZone := getTimeZone(s[len(s)-1])
list := golist.NewList(s[:2])
timeArr := strings.Split(s[2], "T")
list.Append(timeArr[0])
timeStamp := strings.Split(timeArr[1], ":")
list, _ = list.Add(golist.NewList(timeStamp))
arr, err := list.ConvertToSliceInt()
if err != nil {
return time.Time{}, err
}
year := getYear(arr[0])
month := time.Month(arr[1])
day := arr[2]
hour := arr[3]
minute := arr[4]
seconds := 0
if len(arr) >= 6 {
seconds = arr[5]
}
return time.Date(year, month, day, hour, minute, seconds, 0, timeZone), nil
}
// formatPattern5 2011-10-10T14:48:00.000+09:00
func formatPattern5(stringTime string) (parsedTime time.Time, err error) {
s := regexp.MustCompile(`(T|-|/|[[:space:]]|:|\.|\+)`).Split(stringTime, 10)
list := golist.NewList(s)
arr, err := list.ConvertToSliceInt()
if err != nil {
return time.Time{}, err
}
year := getYear(arr[0])
month := time.Month(arr[1])
day := arr[2]
hour := arr[3]
minute := arr[4]
seconds := arr[5]
nseconds := arr[6]
timeZone := getTimeZoneInt(arr[7], arr[8])
return time.Date(year, month, day, hour, minute, seconds, nseconds, timeZone), nil
}
// formatPattern6 January 1, 1970 00:00:00 UTC, January 1, 09 00:00:00.00 GMT
func formatPattern6(stringTime string) (parsedTime time.Time, err error) {
s := regexp.MustCompile(`(\.|[[:space:]]|:|(,[[:space:]]))`).Split(stringTime, 9)
month := getMonth(s[0])
timeZone := getTimeZone(s[len(s)-1])
var list *golist.List
if match(`[A-Z]+`, s[len(s)-1]) {
list = golist.NewList(s[1 : len(s)-1])
} else {
list = golist.NewList(s[1:])
}
arr, err := list.ConvertToSliceInt()
if err != nil {
return time.Time{}, err
}
year := getYear(arr[1])
day := arr[0]
var hour, minute, seconds, nseconds int
if len(arr) != 2 {
hour = arr[2]
minute = arr[3]
if len(arr) >= 5 {
seconds = arr[4]
}
if len(arr) >= 6 {
nseconds = arr[5]
}
}
return time.Date(year, month, day, hour, minute, seconds, nseconds, timeZone), nil
}
// formatPattern7 Wed, 09 Aug 1995 00:00:00 GMT or Wed, 09 Aug 1995 00:00:00
func formatPattern7(stringTime string) (parsedTime time.Time, err error) {
s := regexp.MustCompile(`(\.|[[:space:]]|:|(,[[:space:]]))`).Split(stringTime, 10)
month := getMonth(s[2])
timeZone := getTimeZone(s[len(s)-1])
list := golist.NewList([]string{s[1]})
if match(`[A-Z]+`, s[len(s)-1]) {
list.Extend(s[3 : len(s)-1])
} else {
list.Extend(s[3:])
}
arr, err := list.ConvertToSliceInt()
if err != nil {
return time.Time{}, err
}
year := getYear(arr[1])
day := arr[0]
var hour, minute, seconds, nseconds int
if len(arr) != 2 {
hour = arr[2]
minute = arr[3]
if len(arr) >= 5 {
seconds = arr[4]
}
if len(arr) >= 6 {
nseconds = arr[5]
}
}
return time.Date(year, month, day, hour, minute, seconds, nseconds, timeZone), nil
}
// formatPattern8 Mon Jan 2 15:04:05 MST 2006
func formatPattern8(stringTime string) (parsedTime time.Time, err error) {
s := regexp.MustCompile(`(\.|[[:space:]]|:)`).Split(stringTime, 10)
month := getMonth(s[1])
timeZone := getTimeZone(s[6])
list := golist.NewList(s[2:6])
list.Append(s[7])
arr, err := list.ConvertToSliceInt()
if err != nil {
return time.Time{}, err
}
day := arr[0]
hour := arr[1]
minute := arr[2]
seconds := arr[3]
nseconds := 0
year := getYear(arr[4])
return time.Date(year, month, day, hour, minute, seconds, nseconds, timeZone), nil
}