-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtime.go
234 lines (195 loc) · 6.58 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package zutils
import (
"time"
)
var T = &timeUtil{
SecondStamp: 1e3,
MinuteStamp: 60e3,
HourStamp: 3600e3,
DayStamp: 86400e3,
Year2100Stamp: 4102416000000,
Nanosecond: time.Nanosecond,
Microsecond: time.Microsecond,
Millisecond: time.Millisecond,
Second: time.Second,
Minute: time.Minute,
Hour: time.Hour,
Day: time.Hour * 24,
Week: time.Hour * 24 * 7,
Year365: time.Hour * 24 * 365,
Year366: time.Hour * 24 * 366,
Layout: "2006-01-02 15:04:05",
LayoutDate: "2006-01-02",
LayoutTime: "15:04:05",
LayoutTimeMinute: "15:04",
LayoutDateMinute: "2006-01-02 15:04",
}
// 时间工具
//
// 这个工具内的所有时间戳均以毫秒为单位
type timeUtil struct {
SecondStamp int64 // 一秒的毫秒数
MinuteStamp int64 // 一分的毫秒数
HourStamp int64 // 一小时的毫秒数
DayStamp int64 // 一天的毫秒数
Year2100Stamp int64 // 2100年的时间戳
Nanosecond time.Duration // 纳秒
Microsecond time.Duration // 微秒
Millisecond time.Duration // 毫秒
Second time.Duration // 秒
Minute time.Duration // 分
Hour time.Duration // 时
Day time.Duration // 天
Week time.Duration // 周
Year365 time.Duration // 年
Year366 time.Duration // 年
Layout string // 默认时间样式 YYYY-MM-DD hh:mm:ss
LayoutDate string // 日期样式 YYYY-MM-DD
LayoutTime string // 时间样式 hh:mm:ss
LayoutTimeMinute string // 分钟样式 hh:mm
LayoutDateMinute string // 带日期的分钟样式 YYYY-MM-DD hh:mm
}
func Time(loc *time.Location) *TimeUtil {
return &TimeUtil{
loc: loc,
}
}
type TimeUtil struct {
loc *time.Location
}
// region 基础
// 获取当前毫秒级时间戳
func (z *TimeUtil) GetStamp() int64 {
return time.Now().UnixNano() / 1e6
}
// 获取当前时间
func (z *TimeUtil) GetTime() time.Time {
return time.Now().In(z.loc)
}
// 获取当前时间默认样式, YYYY-MM-DD hh:mm:ss
func (z *TimeUtil) GetText() string {
return time.Now().In(z.loc).Format(T.Layout)
}
// 获取当前时间日期样式, YYYY-MM-DD
func (z *TimeUtil) GetDateText() string {
return time.Now().In(z.loc).Format(T.LayoutDate)
}
// 获取当前时间时间样式, hh:mm:ss
func (z *TimeUtil) GetTimeText() string {
return time.Now().In(z.loc).Format(T.LayoutTime)
}
// 获取当前时间分钟样式, hh:mm
func (z *TimeUtil) GetTimeMinuteText() string {
return time.Now().In(z.loc).Format(T.LayoutTimeMinute)
}
// 获取当前时间带日期的分钟样式, YYYY-MM-DD hh:mm
func (z *TimeUtil) GetDateMinuteTextHour() string {
return time.Now().In(z.loc).Format(T.LayoutDateMinute)
}
// endregion
// region 转换
// 将时间转为毫秒级时间戳
func (z *TimeUtil) TimeToStamp(t time.Time) int64 {
return t.UnixNano() / 1e6
}
// 将时间转为默认样式的字符串
func (z *TimeUtil) TimeToText(t time.Time) string {
return t.In(z.loc).Format(T.Layout)
}
// 将时间转为日期样式的字符串
func (z *TimeUtil) TimeToDateText(t time.Time) string {
return t.In(z.loc).Format(T.LayoutDate)
}
// 将时间转为指定样式的字符串
func (z *TimeUtil) TimeToTextOfLayout(t time.Time, layout string) string {
return t.In(z.loc).Format(layout)
}
// 毫秒级时间戳转时间
func (z *TimeUtil) StampToTime(stamp int64) time.Time {
return time.Unix(0, stamp*1e6).In(z.loc)
}
// 将毫秒级时间戳转为默认样式的字符串
func (z *TimeUtil) StampToText(stamp int64) string {
return time.Unix(0, stamp*1e6).In(z.loc).Format(T.Layout)
}
// 将毫秒级时间戳转为日期样式的字符串
func (z *TimeUtil) StampToDateText(stamp int64) string {
return time.Unix(0, stamp*1e6).In(z.loc).Format(T.LayoutDate)
}
// 将毫秒级时间戳转为指定样式的字符串
func (z *TimeUtil) StampToTextOfLayout(stamp int64, layout string) string {
return time.Unix(0, stamp*1e6).In(z.loc).Format(layout)
}
// 将标准样式时间文本转为时间
func (z *TimeUtil) TextToTime(text string) (time.Time, error) {
return time.ParseInLocation(T.Layout, text, z.loc)
}
// 将日期样式时间文本转为时间
func (z *TimeUtil) DateTextToTime(text string) (time.Time, error) {
return time.ParseInLocation(T.LayoutDate, text, z.loc)
}
// 将指定样式时间文本转为时间
func (z *TimeUtil) TextToTimeOfLayout(text, layout string) (time.Time, error) {
return time.ParseInLocation(layout, text, z.loc)
}
// 将标准样式时间文本转为毫秒级时间戳
func (z *TimeUtil) TextToStamp(text string) (int64, error) {
return z.TextToStampOfLayout(text, T.Layout)
}
// 将日期样式时间文本转为毫秒级时间戳
func (z *TimeUtil) DateTextToStamp(text string) (int64, error) {
return z.TextToStampOfLayout(text, T.LayoutDate)
}
// 将时间文本转为毫秒级时间戳
func (z *TimeUtil) TextToStampOfLayout(text, layout string) (int64, error) {
t, e := time.ParseInLocation(layout, text, z.loc)
if e != nil {
return 0, e
}
return t.UnixNano() / 1e6, nil
}
// endregion
// 获取当天开始时的毫秒级时间戳(0时0分0秒)
func (z *TimeUtil) GetDayStartTime() time.Time {
t := time.Now().In(z.loc)
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, z.loc)
}
// 获取当天开始时的毫秒级时间戳(0时0分0秒)
func (z *TimeUtil) GetDayStartStamp() int64 {
t := time.Now().In(z.loc)
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, z.loc)
return t.Unix() * 1e3
}
// 获取传入时间戳当天的开始时间戳(0时0分0秒)
func (z *TimeUtil) GetDayStartTimeOfTime(t time.Time) time.Time {
t = t.In(z.loc)
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, z.loc)
}
// 获取传入时间戳当天的开始时间戳(0时0分0秒)
func (z *TimeUtil) GetDayStartStampOfStamp(stamp int64) int64 {
t := time.Unix(0, stamp*1e6).In(z.loc)
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, z.loc)
return t.Unix() * 1e3
}
// GetWeekStartTimeOfWeek0 获取指定时间当周的起始时间(周日凌晨0点)
func (z *TimeUtil) GetWeekStartTimeOfWeek0(t time.Time) time.Time {
t = t.In(z.loc)
// 计算当前周的起始天(周日)
weekday := int(t.Weekday())
t = t.AddDate(0, 0, -weekday)
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, z.loc)
return t
}
// GetWeekStartTimeOfWeek1 获取指定时间当周的起始时间(周一凌晨0点)
func (z *TimeUtil) GetWeekStartTimeOfWeek1(t time.Time) time.Time {
t = t.In(z.loc)
// 计算当前周的起始天(周一)
weekday := int(t.Weekday())
if weekday == 0 {
weekday = 7
}
offset := weekday - 1
t = t.AddDate(0, 0, -offset)
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, z.loc)
return t
}