-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
81 lines (68 loc) · 1.47 KB
/
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
package util
import (
"fmt"
"strconv"
"time"
)
// format time to 2006-01-02 15:04:05 format
func FormatTime(ts interface{}) string {
t := ParseTime(ts)
return t.Format("2006-01-02 15:04:05")
}
// parse time from any time format
func ParseTime(timeStr interface{}) (t time.Time) {
switch v := timeStr.(type) {
case int, int64, uint64:
val, _ := strconv.Atoi(fmt.Sprint(v))
t = time.Unix(int64(val), 0)
case string:
t = parse(v)
case time.Time:
t = v
}
return
}
// parse all kind of time format
func parse(ts string) (t time.Time) {
t, _ = time.ParseInLocation("2006-01-02", ts, time.Local)
if !t.IsZero() {
return
}
t, _ = time.ParseInLocation("2006-1-02", ts, time.Local)
if !t.IsZero() {
return
}
t, _ = time.ParseInLocation("2006-01-02 15:04:05", ts, time.Local)
if !t.IsZero() {
return
}
t, _ = time.ParseInLocation("2006-1-2 15:04", ts, time.Local)
if !t.IsZero() {
return
}
t, _ = time.ParseInLocation("2006-01-02T15:04:05Z", ts, time.Local)
if !t.IsZero() {
return
}
t, _ = time.ParseInLocation("2006-01-02T15:04:05", ts, time.Local)
if !t.IsZero() {
return
}
t, _ = time.ParseInLocation("2006-01-02T15:04:05Z07:00", ts, time.Local)
if !t.IsZero() {
return
}
t, _ = time.ParseInLocation("01/02/2006", ts, time.Local)
if !t.IsZero() {
return
}
t, _ = time.ParseInLocation("20060102", ts, time.Local)
if !t.IsZero() {
return
}
t, _ = time.ParseInLocation(time.RFC822, ts, time.Local)
if !t.IsZero() {
return
}
return
}