-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
301 lines (275 loc) · 6.64 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package xtime
import (
"database/sql/driver"
"fmt"
xerr "github.com/goclub/error"
"math"
"strconv"
"time"
)
type Range struct {
Start time.Time
End time.Time
}
func (r Range) Validator(err ...error) error {
if r.Start.After(r.End) {
return xerr.New("goclub/time: Range.Start can not be after Range.End")
}
return nil
}
func InRange(target time.Time, r Range) (in bool) {
begin := r.Start
end := r.End
// 开始结束时间顺序错误,在后续逻辑可能造成验证错误
if err := r.Validator(); err != nil {
panic(err)
}
// begin <= target <= end -> true
if (begin.Before(target) || begin.Equal(target)) &&
(target.Before(end) || target.Equal(end)) {
in = true
return
}
return
}
type DateRange struct {
Start Date `note:"当日期是 2022-01-01 时等同于 Range{Start: 2022-01-01 00:00:00}"`
End Date `note:"当日期是 2022-01-03 时等同于 Range{End: 2022-01-03 23:59:59}"`
}
func (r DateRange) Validator(err ...error) error {
if r.Start.After(r.End) {
return xerr.New("goclub/time: DateRange.Start can not be after DateRange.End")
}
return nil
}
func InRangeFromDate(target time.Time, r DateRange) (in bool) {
timeRange := Range{
Start: FirstSecondOfDate(r.Start.Time(target.Location())),
End: LastSecondOfDate(r.End.Time(target.Location())),
}
return InRange(target, timeRange)
}
type Date struct {
Year int
Month time.Month
Day int
}
func Today(loc *time.Location) Date {
year, month, day := time.Now().In(loc).Date()
return Date{
Year: year,
Month: month,
Day: day,
}
}
func NewDate(year int, month time.Month, day int) Date {
return Date{
year, month, day,
}
}
func NewDateFromTime(t time.Time) Date {
return NewDate(t.Date())
}
func NewDateFromString(value string) (d Date, err error) {
t, err := time.Parse(LayoutDate, value)
if err != nil {
err = xerr.WithStack(err)
return
}
return NewDateFromTime(t), nil
}
func (d Date) IsZero() bool {
if d.Year == 0 && d.Month == 0 && d.Day == 0 {
return true
}
return false
}
func (d *Date) MarshalRequest(value string) error {
newDate, err := NewDateFromString(value) // indivisible begin
if err != nil { // indivisible end
return err
}
d = &newDate
return nil
}
func (d *Date) UnmarshalJSON(b []byte) error {
value, err := strconv.Unquote(string(b))
if err != nil {
return xerr.WithStack(err)
}
date, err := NewDateFromString(value)
if err != nil {
return xerr.WithStack(err)
}
*d = date
return err
}
func (d Date) MarshalJSON() ([]byte, error) {
return []byte(`"` + d.String() + `"`), nil
}
func (d Date) Time(loc *time.Location) time.Time {
return time.Date(d.Year, d.Month, d.Day, 0, 0, 0, 0, loc)
}
func (d Date) LocalTime() time.Time {
return d.Time(time.Local)
}
func (d Date) UTCTime() time.Time {
return d.Time(time.UTC)
}
func (d Date) ChinaTime() ChinaTime {
return NewChinaTime(d.Time(LocChina))
}
func (d Date) String() string {
return time.Date(d.Year, d.Month, d.Day, 0, 0, 0, 0, time.UTC).Format(LayoutDate)
}
func (d Date) Value() (driver.Value, error) {
return d.String(), nil
}
func (d *Date) Scan(value interface{}) (err error) {
if value == nil {
return xerr.New("unsupported NULL xtime.Date value, maybe you should use xtime.NullDate")
}
switch v := value.(type) {
case time.Time:
*d = NewDateFromTime(v)
default:
var date Date
date, err = NewDateFromString(fmt.Sprintf("%s", value))
if err != nil {
return
}
*d = date
}
return
}
func (d Date) AddDate(years int, months int, days int) (date Date) {
return NewDateFromTime(d.UTCTime().AddDate(years, months, days))
}
func (d Date) Sub(u Date) (days int) {
return int(math.Round(d.UTCTime().Sub(u.UTCTime()).Hours())) / 24
}
// FirstDateOfMonth 2022-11-11 => 2022-11-01
func (d Date) FirstDateOfMonth() (first Date) {
return NewDate(d.Year, d.Month, 1)
}
// LastDateOfMonth 2022-11-11 => 2022-11-30
func (d Date) LastDateOfMonth() (first Date) {
return d.FirstDateOfMonth().AddDate(0, 1, -1)
}
func (d Date) After(t Date) bool {
return d.UTCTime().After(t.UTCTime())
}
func (d Date) Before(t Date) bool {
return d.UTCTime().Before(t.UTCTime())
}
func (d Date) Equal(t Date) bool {
return d == t
}
type NullDate struct {
date Date
valid bool
}
func (v NullDate) Date() Date {
return v.date
}
func (v NullDate) Valid() bool {
return v.valid
}
func NewNullDate(year int, month time.Month, day int) NullDate {
return NullDate{
date: NewDate(year, month, day),
valid: true,
}
}
func (d *NullDate) UnmarshalJSON(b []byte) error {
if string(b) == "null" || string(b) == "" {
d.valid = false
return nil
}
date := Date{}
err := date.UnmarshalJSON(b)
if err != nil {
return xerr.WithStack(err)
}
*d = NullDate{
date: date,
valid: true,
}
return err
}
func (d NullDate) MarshalJSON() ([]byte, error) {
if d.valid {
return d.date.MarshalJSON()
} else {
return []byte(`null`), nil
}
}
func (d NullDate) String() string {
if d.valid {
return d.date.String()
}
return "null"
}
func (d NullDate) Value() (driver.Value, error) {
if d.valid {
return d.date.Value()
}
return nil, nil
}
func (d *NullDate) Scan(value interface{}) error {
if value == nil {
d.valid = false
return nil
}
d.valid = true
return d.date.Scan(value)
}
// LastSecondOfDate 2022-11-11 xx:xx:xx => 2022-11-11 23:59:59.999
func LastSecondOfDate(t time.Time) time.Time {
y, m, d := t.Date()
return time.Date(y, m, d, 23, 59, 59, 999999999, t.Location())
}
// FirstSecondOfDate 2022-11-11 xx:xx:xx => 2022-11-11 00:00:00
func FirstSecondOfDate(t time.Time) time.Time {
y, m, d := t.Date()
return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
}
// TomorrowFirstSecond 2022-11-11 xx:xx:xx => 2022-11-12 00:00:00
func TomorrowFirstSecond(t time.Time) time.Time {
return FirstSecondOfDate(t.AddDate(0, 0, 1))
}
// TomorrowFirstSecondDuration 2022-11-11 23:59:50 => 10s
func TomorrowFirstSecondDuration(t time.Time) time.Duration {
return TomorrowFirstSecond(t).Sub(t)
}
func Now(loc *time.Location) time.Time {
return time.Now().In(loc)
}
func SplitRange(days uint, r DateRange) (splitRanges []DateRange) {
if days == 0 {
days = 1
}
splitRanges = []DateRange{}
// 边界: 2022-01-01~2022-01-01
if r.Start.Equal(r.End) {
splitRanges = append(splitRanges, r)
return
}
// 边界: 2022-01-02~2022-01-01
if err := r.Validator(); err != nil {
// 格式错误必须 panic 否则即使当前逻辑不出错后续逻辑也会出错
panic(err)
}
slow := r.Start
for {
itemEnd := slow.AddDate(0, 0, int(days-1))
if itemEnd.Before(r.End) {
splitRanges = append(splitRanges, DateRange{slow, itemEnd})
slow = itemEnd.AddDate(0, 0, 1)
} else {
splitRanges = append(splitRanges, DateRange{slow, r.End})
break
}
}
return splitRanges
}