-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathut.go
81 lines (65 loc) · 1.8 KB
/
ut.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 ut
import (
"errors"
"time"
"strings"
)
// Mon Jan 2 15:04:05 -0700 MST 2006
var ref = map[string]string{
`YYYY`: `2006`, `MM`: `01`, `DD`: `02`,
`hh`: `15`, `mm`: `04`, `ss`: `05`,
}
func Ut(input, format string, noon bool) (int64, error) {
format, err := convertFormat(format)
if err != nil {
return 0, err
}
if noon {
format = strings.Join([]string{format, `15:04:05Z`}, `T`)
input = strings.Join([]string{input, `12:00:00Z`}, `T`)
}
d, err := time.Parse(format, input)
if err != nil {
return 0, err
}
return d.Unix(), err
}
func convertFormat(format string) (string, error) {
if strings.Contains(format, `D`) {
if strings.Count(format, `D`) != 2 {
return ``, errors.New(`Length of day format is not two`)
}
format = strings.Replace(format, `DD`, ref[`DD`], 1)
}
if strings.Contains(format, `M`) {
if strings.Count(format, `M`) != 2 {
return ``, errors.New(`Length of month format is not two`)
}
format = strings.Replace(format, `MM`, ref[`MM`], 1)
}
if strings.Contains(format, `Y`) {
if strings.Count(format, `Y`) != 4 {
return ``, errors.New(`Length of year format is not four`)
}
format = strings.Replace(format, `YYYY`, ref[`YYYY`], 1)
}
if strings.Contains(format, `h`) {
if strings.Count(format, `h`) != 2 {
return ``, errors.New(`Length of hour format is not two`)
}
format = strings.Replace(format, `hh`, ref[`hh`], 1)
}
if strings.Contains(format, `m`) {
if strings.Count(format, `m`) != 2 {
return ``, errors.New(`Length of minute format is not two`)
}
format = strings.Replace(format, `mm`, ref[`mm`], 1)
}
if strings.Contains(format, `s`) {
if strings.Count(format, `s`) != 2 {
return ``, errors.New(`Length of second format is not two`)
}
format = strings.Replace(format, `ss`, ref[`ss`], 1)
}
return format, nil
}