-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeutils.go
106 lines (94 loc) · 2.78 KB
/
timeutils.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
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
package goutils
import (
"fmt"
"strings"
"time"
)
func convertLayout(layout string) string {
parsedLayout := layout
switch layout {
case "datetime":
parsedLayout = "20060102150405"
case "-datetime":
parsedLayout = "2006-01-02 15:04:05"
case "-datetime-":
parsedLayout = "2006-01-02 15:04"
case "-datetime--":
parsedLayout = "2006-01-02 15"
case "-date":
parsedLayout = "2006-01-02"
case "date":
parsedLayout = "20060102"
case "-date-":
parsedLayout = "2006-01"
case "-date--":
parsedLayout = "2006"
case "/datetime":
parsedLayout = "2006/01/02 15:04:05"
case "/datetime-":
parsedLayout = "2006/01/02 15:04"
case "/datetime--":
parsedLayout = "2006/01/02 15"
case "/date":
parsedLayout = "2006/01/02"
case "/date-":
parsedLayout = "2006/01"
case "/date--":
parsedLayout = "2006"
case ".datetime":
parsedLayout = "2006.01.02 15:04:05"
case ".datetime-":
parsedLayout = "2006.01.02 15:04"
case ".datetime--":
parsedLayout = "2006.01.02 15"
case ".date":
parsedLayout = "2006.01.02"
case ".date-":
parsedLayout = "2006.01"
case ".date--":
parsedLayout = "2006"
case "time":
parsedLayout = "15:04:05"
case "time-":
parsedLayout = "15:04"
case "time--":
parsedLayout = "15"
}
return parsedLayout
}
// FmtTime format time by specified layout.
func FmtTime(time time.Time, layout string) (string, error) {
parsedLayout := convertLayout(layout)
if strings.TrimSpace(parsedLayout) == "" {
return "", fmt.Errorf("invalid layout format: %s", layout)
}
return time.Format(parsedLayout), nil
}
// MustFmtTime format time by specified layout, will panic if layout is invalid.
func MustFmtTime(time time.Time, layout string) string {
ti, err := FmtTime(time, layout)
if err != nil {
panic(fmt.Sprintf("Format time failed with layout: %s", layout))
}
return ti
}
// ParseLocaltime time with zone set to local.
// parsable layout can be [-datetime|-datetime-|-datetime--|-date|-date-|-date--|time|time-|time--]
// and any other layout format that can pass to time.ParseInLocation
func ParseLocaltime(toParseTime, layout string) (time.Time, error) {
parsedLayout := convertLayout(layout)
if strings.TrimSpace(parsedLayout) == "" {
return time.Time{}, fmt.Errorf("invalid layout format: %s", layout)
}
return time.ParseInLocation(parsedLayout, toParseTime, time.Now().Location())
}
// MustParseLocaltime time with zone set to local, will panic if layout specified is invalid.
// parsable layout can be [-datetime|-datetime-|-datetime--|-date|-date-|-date--|time|time-|time--]
// and any other layout format that can pass to time.ParseInLocation
func MustParseLocaltime(toParseTime, layout string) time.Time {
ti, err := ParseLocaltime(toParseTime, layout)
if err != nil {
panic(fmt.Sprintf("Parse local time failed with format :%s", layout))
}
return ti
}