forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparer.go
executable file
·496 lines (442 loc) · 10.3 KB
/
comparer.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package carbon
import (
"time"
)
// IsZero reports whether is zero time.
// 是否是零值时间
func (c Carbon) IsZero() bool {
return c.time.IsZero()
}
// IsValid reports whether is valid time.
// 是否是有效时间
func (c Carbon) IsValid() bool {
if c.Error == nil && !c.IsZero() {
return true
}
return false
}
// IsInvalid reports whether is invalid time.
// 是否是无效时间
func (c Carbon) IsInvalid() bool {
return !c.IsValid()
}
// IsNow reports whether is now time.
// 是否是当前时间
func (c Carbon) IsNow() bool {
if c.IsInvalid() {
return false
}
return c.Timestamp() == c.Now().Timestamp()
}
// IsFuture reports whether is future time.
// 是否是未来时间
func (c Carbon) IsFuture() bool {
if c.IsInvalid() {
return false
}
return c.Timestamp() > c.Now().Timestamp()
}
// IsPast reports whether is past time.
// 是否是过去时间
func (c Carbon) IsPast() bool {
if c.IsInvalid() {
return false
}
return c.Timestamp() < c.Now().Timestamp()
}
// IsLeapYear reports whether is a leap year.
// 是否是闰年
func (c Carbon) IsLeapYear() bool {
if c.IsInvalid() {
return false
}
year := c.Year()
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
return true
}
return false
}
// IsLongYear reports whether is a long year, see https://en.wikipedia.org/wiki/ISO_8601#Week_dates.
// 是否是长年
func (c Carbon) IsLongYear() bool {
if c.IsInvalid() {
return false
}
_, w := time.Date(c.Year(), 12, 31, 0, 0, 0, 0, c.loc).ISOWeek()
return w == weeksPerLongYear
}
// IsJanuary reports whether is January.
// 是否是一月
func (c Carbon) IsJanuary() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.January)
}
// IsFebruary reports whether is February.
// 是否是二月
func (c Carbon) IsFebruary() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.February)
}
// IsMarch reports whether is March.
// 是否是三月
func (c Carbon) IsMarch() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.March)
}
// IsApril reports whether is April.
// 是否是四月
func (c Carbon) IsApril() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.April)
}
// IsMay reports whether is May.
// 是否是五月
func (c Carbon) IsMay() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.May)
}
// IsJune reports whether is June.
// 是否是六月
func (c Carbon) IsJune() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.June)
}
// IsJuly reports whether is July.
// 是否是七月
func (c Carbon) IsJuly() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.July)
}
// IsAugust reports whether is August.
// 是否是八月
func (c Carbon) IsAugust() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.August)
}
// IsSeptember reports whether is September.
// 是否是九月
func (c Carbon) IsSeptember() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.September)
}
// IsOctober reports whether is October.
// 是否是十月
func (c Carbon) IsOctober() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.October)
}
// IsNovember reports whether is November.
// 是否是十一月
func (c Carbon) IsNovember() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.November)
}
// IsDecember reports whether is December.
// 是否是十二月
func (c Carbon) IsDecember() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.December)
}
// IsMonday reports whether is Monday.
// 是否是周一
func (c Carbon) IsMonday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Monday
}
// IsTuesday reports whether is Tuesday.
// 是否是周二
func (c Carbon) IsTuesday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Tuesday
}
// IsWednesday reports whether is Wednesday.
// 是否是周三
func (c Carbon) IsWednesday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Wednesday
}
// IsThursday reports whether is Thursday.
// 是否是周四
func (c Carbon) IsThursday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Thursday
}
// IsFriday reports whether is Friday.
// 是否是周五
func (c Carbon) IsFriday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Friday
}
// IsSaturday reports whether is Saturday.
// 是否是周六
func (c Carbon) IsSaturday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Saturday
}
// IsSunday reports whether is Sunday.
// 是否是周日
func (c Carbon) IsSunday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Sunday
}
// IsWeekday reports whether is weekday.
// 是否是工作日
func (c Carbon) IsWeekday() bool {
if c.IsInvalid() {
return false
}
return !c.IsSaturday() && !c.IsSunday()
}
// IsWeekend reports whether is weekend.
// 是否是周末
func (c Carbon) IsWeekend() bool {
if c.IsInvalid() {
return false
}
return c.IsSaturday() || c.IsSunday()
}
// IsYesterday reports whether is yesterday.
// 是否是昨天
func (c Carbon) IsYesterday() bool {
if c.IsInvalid() {
return false
}
return c.ToDateString() == Yesterday().ToDateString()
}
// IsToday reports whether is today.
// 是否是今天
func (c Carbon) IsToday() bool {
if c.IsInvalid() {
return false
}
return c.ToDateString() == Now().ToDateString()
}
// IsTomorrow reports whether is tomorrow.
// 是否是明天
func (c Carbon) IsTomorrow() bool {
if c.IsInvalid() {
return false
}
return c.ToDateString() == Tomorrow().ToDateString()
}
// IsSameCentury reports whether is same century.
// 是否是同一世纪
func (c Carbon) IsSameCentury(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Century() == t.Century()
}
// IsSameDecade reports whether is same decade.
// 是否是同一年代
func (c Carbon) IsSameDecade(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Decade() == t.Decade()
}
// IsSameYear reports whether is same year.
// 是否是同一年
func (c Carbon) IsSameYear(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Year() == t.Year()
}
// IsSameQuarter reports whether is same quarter.
// 是否是同一季节
func (c Carbon) IsSameQuarter(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Quarter() == t.Quarter()
}
// IsSameMonth reports whether is same month.
// 是否是同一月
func (c Carbon) IsSameMonth(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Format("Ym") == t.Format("Ym")
}
// IsSameDay reports whether is same day.
// 是否是同一天
func (c Carbon) IsSameDay(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Format("Ymd") == t.Format("Ymd")
}
// IsSameHour reports whether is same hour.
// 是否是同一小时
func (c Carbon) IsSameHour(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Format("YmdH") == t.Format("YmdH")
}
// IsSameMinute reports whether is same minute.
// 是否是同一分钟
func (c Carbon) IsSameMinute(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Format("YmdHi") == t.Format("YmdHi")
}
// IsSameSecond reports whether is same second.
// 是否是同一秒
func (c Carbon) IsSameSecond(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Format("YmdHis") == t.Format("YmdHis")
}
// Compare compares by an operator.
// 时间比较
func (c Carbon) Compare(operator string, t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
switch operator {
case "=":
return c.Eq(t)
case "<>", "!=":
return !c.Eq(t)
case ">":
return c.Gt(t)
case ">=":
return c.Gte(t)
case "<":
return c.Lt(t)
case "<=":
return c.Lte(t)
}
return false
}
// Gt reports whether greater than.
// 是否大于
func (c Carbon) Gt(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.time.After(t.time)
}
// Lt reports whether less than.
// 是否小于
func (c Carbon) Lt(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.time.Before(t.time)
}
// Eq reports whether equal.
// 是否等于
func (c Carbon) Eq(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.time.Equal(t.time)
}
// Ne reports whether not equal.
// 是否不等于
func (c Carbon) Ne(t Carbon) bool {
return !c.Eq(t)
}
// Gte reports whether greater than or equal.
// 是否大于等于
func (c Carbon) Gte(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Gt(t) || c.Eq(t)
}
// Lte reports whether less than or equal.
// 是否小于等于
func (c Carbon) Lte(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Lt(t) || c.Eq(t)
}
// Between reports whether between two times, excluded the start and end time.
// 是否在两个时间之间(不包括这两个时间)
func (c Carbon) Between(start Carbon, end Carbon) bool {
if c.IsInvalid() || start.IsInvalid() || end.IsInvalid() {
return false
}
if c.Gt(start) && c.Lt(end) {
return true
}
return false
}
// BetweenIncludedStart reports whether between two times, included the start time.
// 是否在两个时间之间(包括开始时间)
func (c Carbon) BetweenIncludedStart(start Carbon, end Carbon) bool {
if c.IsInvalid() || start.IsInvalid() || end.IsInvalid() {
return false
}
if c.Gte(start) && c.Lt(end) {
return true
}
return false
}
// BetweenIncludedEnd reports whether between two times, included the end time.
// 是否在两个时间之间(包括结束时间)
func (c Carbon) BetweenIncludedEnd(start Carbon, end Carbon) bool {
if c.IsInvalid() || start.IsInvalid() || end.IsInvalid() {
return false
}
if c.Gt(start) && c.Lte(end) {
return true
}
return false
}
// BetweenIncludedBoth reports whether between two times, included the start and end time.
// 是否在两个时间之间(包括这两个时间)
func (c Carbon) BetweenIncludedBoth(start Carbon, end Carbon) bool {
if c.IsInvalid() || start.IsInvalid() || end.IsInvalid() {
return false
}
if c.Gte(start) && c.Lte(end) {
return true
}
return false
}