forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparer.go
executable file
·378 lines (335 loc) · 7.5 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
package carbon
import (
"time"
)
// IsZero whether is zero time.
// 是否是零值时间
func (c Carbon) IsZero() bool {
return c.time.IsZero()
}
// IsInvalid whether is invalid time.
// 是否是无效时间
func (c Carbon) IsInvalid() bool {
if c.Error != nil || c.IsZero() {
return true
}
return false
}
// IsNow whether is now time.
// 是否是当前时间
func (c Carbon) IsNow() bool {
if c.IsInvalid() {
return false
}
return c.Timestamp() == c.Now().Timestamp()
}
// IsFuture whether is future time.
// 是否是未来时间
func (c Carbon) IsFuture() bool {
if c.IsInvalid() {
return false
}
return c.Timestamp() > c.Now().Timestamp()
}
// IsPast whether is past time.
// 是否是过去时间
func (c Carbon) IsPast() bool {
if c.IsInvalid() {
return false
}
return c.Timestamp() < c.Now().Timestamp()
}
// IsLeapYear whether is a leap year.
// 是否是闰年
func (c Carbon) IsLeapYear() bool {
if c.IsInvalid() {
return false
}
year := c.time.In(c.loc).Year()
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
return true
}
return false
}
// IsLongYear 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(), time.December, 31, 0, 0, 0, 0, c.loc).ISOWeek()
return w == weeksPerLongYear
}
// IsJanuary whether is January.
// 是否是一月
func (c Carbon) IsJanuary() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Month() == time.January
}
// IsFebruary whether is February.
// 是否是二月
func (c Carbon) IsFebruary() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Month() == time.February
}
// IsMarch whether is March.
// 是否是三月
func (c Carbon) IsMarch() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Month() == time.March
}
// IsApril whether is April.
// 是否是四月
func (c Carbon) IsApril() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Month() == time.April
}
// IsMay whether is May.
// 是否是五月
func (c Carbon) IsMay() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Month() == time.May
}
// IsJune whether is June.
// 是否是六月
func (c Carbon) IsJune() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Month() == time.June
}
// IsJuly whether is July.
// 是否是七月
func (c Carbon) IsJuly() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Month() == time.July
}
// IsAugust whether is August.
// 是否是八月
func (c Carbon) IsAugust() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Month() == time.August
}
// IsSeptember whether is September.
// 是否是九月
func (c Carbon) IsSeptember() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Month() == time.September
}
// IsOctober whether is October.
// 是否是十月
func (c Carbon) IsOctober() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Month() == time.October
}
// IsNovember whether is November.
// 是否是十一月
func (c Carbon) IsNovember() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Month() == time.November
}
// IsDecember whether is December.
// 是否是十二月
func (c Carbon) IsDecember() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Month() == time.December
}
// IsMonday whether is Monday.
// 是否是周一
func (c Carbon) IsMonday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Monday
}
// IsTuesday whether is Tuesday.
// 是否是周二
func (c Carbon) IsTuesday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Tuesday
}
// IsWednesday whether is Wednesday.
// 是否是周三
func (c Carbon) IsWednesday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Wednesday
}
// IsThursday whether is Thursday.
// 是否是周四
func (c Carbon) IsThursday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Thursday
}
// IsFriday whether is Friday.
// 是否是周五
func (c Carbon) IsFriday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Friday
}
// IsSaturday whether is Saturday.
// 是否是周六
func (c Carbon) IsSaturday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Saturday
}
// IsSunday whether is Sunday.
// 是否是周日
func (c Carbon) IsSunday() bool {
if c.IsInvalid() {
return false
}
return c.time.In(c.loc).Weekday() == time.Sunday
}
// IsWeekday whether is weekday.
// 是否是工作日
func (c Carbon) IsWeekday() bool {
if c.IsInvalid() {
return false
}
return !c.IsSaturday() && !c.IsSunday()
}
// IsWeekend whether is weekend.
// 是否是周末
func (c Carbon) IsWeekend() bool {
if c.IsInvalid() {
return false
}
return c.IsSaturday() || c.IsSunday()
}
// IsYesterday whether is yesterday.
// 是否是昨天
func (c Carbon) IsYesterday() bool {
if c.IsInvalid() {
return false
}
return c.ToDateString() == Now().SubDay().ToDateString()
}
// IsToday whether is today.
// 是否是今天
func (c Carbon) IsToday() bool {
if c.IsInvalid() {
return false
}
return c.ToDateString() == c.Now().ToDateString()
}
// IsTomorrow whether is tomorrow.
// 是否是明天
func (c Carbon) IsTomorrow() bool {
if c.IsInvalid() {
return false
}
return c.ToDateString() == Now().AddDay().ToDateString()
}
// Compare comparison by a operator.
// 时间比较
func (c Carbon) Compare(operator string, t Carbon) bool {
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 whether greater than.
// 是否大于
func (c Carbon) Gt(t Carbon) bool {
return c.time.After(t.time)
}
// Lt whether less than.
// 是否小于
func (c Carbon) Lt(t Carbon) bool {
return c.time.Before(t.time)
}
// Eq whether equal.
// 是否等于
func (c Carbon) Eq(t Carbon) bool {
return c.time.Equal(t.time)
}
// Ne whether not equal.
// 是否不等于
func (c Carbon) Ne(t Carbon) bool {
return !c.Eq(t)
}
// Gte whether greater than or equal.
// 是否大于等于
func (c Carbon) Gte(t Carbon) bool {
return c.Gt(t) || c.Eq(t)
}
// Lte whether less than or equal.
// 是否小于等于
func (c Carbon) Lte(t Carbon) bool {
return c.Lt(t) || c.Eq(t)
}
// Between whether between two times, excluded the start and end time.
// 是否在两个时间之间(不包括这两个时间)
func (c Carbon) Between(start Carbon, end Carbon) bool {
if c.Gt(start) && c.Lt(end) {
return true
}
return false
}
// BetweenIncludedStart whether between two times, included the start time.
// 是否在两个时间之间(包括开始时间)
func (c Carbon) BetweenIncludedStart(start Carbon, end Carbon) bool {
if c.Gte(start) && c.Lt(end) {
return true
}
return false
}
// BetweenIncludedEnd whether between two times, included the end time.
// 是否在两个时间之间(包括结束时间)
func (c Carbon) BetweenIncludedEnd(start Carbon, end Carbon) bool {
if c.Gt(start) && c.Lte(end) {
return true
}
return false
}
// BetweenIncludedBoth whether between two times, included the start and end time.
// 是否在两个时间之间(包括这两个时间)
func (c Carbon) BetweenIncludedBoth(start Carbon, end Carbon) bool {
if c.Gte(start) && c.Lte(end) {
return true
}
return false
}