-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrtime.go
49 lines (42 loc) · 924 Bytes
/
strtime.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
package strftime
import (
"fmt"
"strings"
"time"
)
// YYYY-MM-DD HH:MM:SS
// define a pre-defined format hash
var formatHash = map[string]string{
"YYYY": "2006",
"MM": "01",
"DD": "02",
"HH": "15",
"mm": "04",
"SS": "05",
}
var defaultHooks = map[string]string{
"month": "YYYY-MM",
"date": "YYYY-MM-DD",
"time": "HH:mm:SS",
"datetime": "YYYY-MM-DD HH:mm:SS",
"dbdt": "YYYYMMDD_HHmmSS",
"unix": "unix",
}
func Format(format string, t time.Time) string {
// 1. check if the format is in the default hooks
if defaultFormat, ok := defaultHooks[format]; ok {
format = defaultFormat
}
if format == "unix" {
timestamp := t.Unix()
return fmt.Sprintf("%d", timestamp)
}
for k, v := range formatHash {
// replace all
format = strings.Replace(format, k, v, -1)
}
return t.Format(format)
}
func Now() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}