-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrftime.go
61 lines (55 loc) · 1.42 KB
/
strftime.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
/**
* Author: Tony.Shao
* Email: [email protected]
* Github: github.com/xiocode
* File: strftime.go
* Description: strftime
*/
package strftime
import (
"strings"
"time"
)
var conversion = map[string]string{
"B": "January", //月 英文 完整
"b": "Jan", //月 英文 缩写
"m": "01", //月 数字
"A": "Monday", //周 英文 完整
"a": "Mon", //周 缩写 完整
"d": "02", //日 数字
"H": "15", //时 24小时制 数字
"I": "03", //时 12小时制 数字
"M": "04", //分 数字
"S": "05", //秒 数字
"Y": "2006", //年 完整 数字
"y": "06", //年 缩写 数字
"p": "PM", //12小时制 上下午 AM PM
"Z": "MST", //时区
"z": "-0700", //时区 数字
}
func Format(format string, t time.Time) string {
layout := layoutParser(format)
return t.Format(layout)
}
func Parse(format, value string) (time.Time, error) {
layout := layoutParser(format)
return time.Parse(layout, value)
}
func layoutParser(format string) string {
formatChunks := strings.Split(format, "%")
var layout []string
for _, chunk := range formatChunks {
if len(chunk) == 0 {
continue
}
if layoutCmd, ok := conversion[chunk[0:1]]; ok {
layout = append(layout, layoutCmd)
if len(chunk) > 1 {
layout = append(layout, chunk[1:])
}
} else {
layout = append(layout, "%", chunk)
}
}
return strings.Join(layout, "")
}