/
time.go
149 lines (130 loc) · 3.31 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package gone
import (
"encoding/json"
"strconv"
"strings"
"time"
)
const timeTpl string = "2006-01-02 15:04:05"
// https://github.com/dxvgef/gommon/blob/master/datatime/datetime.go
// const timeShortTpl string = "2006-01-02 15:04"
// TimeToStr 返回时间的字符串格式.
func TimeToStr(t time.Time, format ...string) string {
if len(format) == 0 {
return t.Format(timeTpl)
}
return t.Format(format[0])
}
// TimestampToStr Timestamp将unix时间转为时间字符串.
func TimestampToStr(t int64, format ...string) string {
if len(format) == 0 {
return time.Unix(t, 0).Format(timeTpl)
}
return time.Unix(t, 0).Format(format[0])
}
// FormatByStr 将字符串中的时间变量(y年/m月/d日/h时/i分/s秒)转换成时间字符串.
func FormatByStr(tpl string, t int64) string {
tpl = strings.ReplaceAll(tpl, "y", "2006")
tpl = strings.ReplaceAll(tpl, "m", "01")
tpl = strings.ReplaceAll(tpl, "d", "02")
tpl = strings.ReplaceAll(tpl, "h", "15")
tpl = strings.ReplaceAll(tpl, "i", "04")
tpl = strings.ReplaceAll(tpl, "s", "05")
return time.Unix(t, 0).Format(tpl)
}
// GetMonthRange 获得指定年份和月份的起始unix时间和截止unix时间.
func GetMonthRange(year, month int) (beginTime, endTime int64, err error) {
// 获得当前时间
t := time.Now()
if year == 0 {
year = t.Year()
}
if month == 0 {
month = int(t.Month())
}
yearStr := strconv.Itoa(year)
monthStr := strconv.Itoa(month)
// 拼接当时时间的字符串格式
str := yearStr + "-" + monthStr + "-1 00:00:00"
// 起始时间
begin, err := time.ParseInLocation(timeTpl, str, time.Local)
if err != nil {
return
}
beginTime = begin.Unix()
month = int(begin.Month())
day := 30
if month == 2 {
day = 28
}
if month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 {
day = 31
}
// 截止时间
str = yearStr + "-" + monthStr + "-" + strconv.Itoa(day) + " 23:59:59"
end, err := time.ParseInLocation(timeTpl, str, time.Local)
if err != nil {
return
}
endTime = end.Unix()
return beginTime, endTime, err
}
// GetWeek 获得星期的数字.
func GetWeek(t time.Time) int {
week := t.Weekday().String()
switch week {
case "Monday":
return 1
case "Tuesday":
return 2
case "Wednesday":
return 3
case "Thursday":
return 4
case "Friday":
return 5
case "Saturday":
return 6
case "Sunday":
return 7
}
return 0
}
// https://github.com/gbrlsnchs/jwt/v3@v3.0.0/time.go
// Epoch is 01/01/1970.
var Epoch = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC)
// Time is the allowed format for time, as per the RFC 7519.
type Time struct {
time.Time
}
// NumericDate is a resolved Unix time.
func NumericDate(tt time.Time) *Time {
if tt.Before(Epoch) {
tt = Epoch
}
return &Time{time.Unix(tt.Unix(), 0)} // set time using Unix time
}
// MarshalJSON implements a marshaling function for time-related claims.
func (t Time) MarshalJSON() ([]byte, error) {
if t.Before(Epoch) {
return json.Marshal(0)
}
return json.Marshal(t.Unix())
}
// UnmarshalJSON implements an unmarshaling function for time-related claims.
func (t *Time) UnmarshalJSON(b []byte) error {
var unix *int64
err := json.Unmarshal(b, &unix)
if err != nil {
return err
}
if unix == nil {
return nil
}
tt := time.Unix(*unix, 0)
if tt.Before(Epoch) {
tt = Epoch
}
t.Time = tt
return nil
}