-
Notifications
You must be signed in to change notification settings - Fork 1
/
is.go
291 lines (233 loc) · 5.44 KB
/
is.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
package datebin
import (
"fmt"
"time"
)
// 是否是零值时间
// if the time is Zero time ?
func (this Datebin) IsZero() bool {
return this.time.IsZero()
}
// 是否是无效时间
// if the time is Invalid time ?
func (this Datebin) IsInvalid() bool {
if this.Error() != nil || this.IsZero() {
return true
}
return false
}
// 是否是夏令时
// if the time is DST timezone ?
// IsDST reports whether the time in the configured location is in Daylight Savings Time.
func (this Datebin) IsDST() bool {
if this.IsInvalid() {
return false
}
return this.time.IsDST()
}
// 是否是 Utc 时区
// if the time is Utc timezone ?
func (this Datebin) IsUTC() bool {
if this.IsInvalid() {
return false
}
return this.GetTimezone() == UTC
}
// 是否是本地时区
// if the time is Local timezone ?
func (this Datebin) IsLocal() bool {
if this.IsInvalid() {
return false
}
return this.GetTimezone() == this.Now("Local").GetTimezone()
}
// 是否是上午
// if the time is AM ?
func (this Datebin) IsAM() bool {
if this.IsInvalid() {
return false
}
return this.Hour() < 12
}
// 是否是下午
// if the time is PM ?
func (this Datebin) IsPM() bool {
if this.IsInvalid() {
return false
}
return this.Hour() >= 12
}
// 是否是当前时间
// if the time is Now time ?
func (this Datebin) IsNow() bool {
if this.IsInvalid() {
return false
}
return this.Timestamp() == this.Now().Timestamp()
}
// 是否是未来时间
// if the time is Future time ?
func (this Datebin) IsFuture() bool {
if this.IsInvalid() {
return false
}
return this.Timestamp() > this.Now().Timestamp()
}
// 是否是过去时间
// if the time is Past time ?
func (this Datebin) IsPast() bool {
if this.IsInvalid() {
return false
}
return this.Timestamp() < this.Now().Timestamp()
}
// 是否是闰年
// if the time is LeapYear ?
func (this Datebin) IsLeapYear() bool {
if this.IsInvalid() {
return false
}
year := this.time.In(this.loc).Year()
if year%4 == 0 && (year%100 != 0 || year%400 == 0) {
return true
}
return false
}
// 是否是长年
// if the time is LongYear ?
func (this Datebin) IsLongYear() bool {
if this.IsInvalid() {
return false
}
_, w := time.Date(this.Year(), time.December, 31, 0, 0, 0, 0, this.loc).ISOWeek()
return w == weeksPerLongYear
}
// 是否是今天
// if the time is Today ?
func (this Datebin) IsToday() bool {
if this.IsInvalid() {
return false
}
return this.ToDateString() == this.Now().ToDateString()
}
// 是否是昨天
// if the time is Yesterday ?
func (this Datebin) IsYesterday() bool {
if this.IsInvalid() {
return false
}
return this.ToDateString() == this.Now().Offset("day", -1).ToDateString()
}
// 是否是明天
// if the time is Tomorrow ?
func (this Datebin) IsTomorrow() bool {
if this.IsInvalid() {
return false
}
return this.ToDateString() == this.Now().Offset("day", +1).ToDateString()
}
// 是否是当年
// if the time is Current Year ?
func (this Datebin) IsCurrentYear() bool {
if this.IsInvalid() {
return false
}
return this.Year() == this.Now().Year()
}
// 是否是当月
// if the time is Current Month ?
func (this Datebin) IsCurrentMonth() bool {
if this.IsInvalid() {
return false
}
return this.Month() == this.Now().Month()
}
// 时间是否是当前最近的一周
// if the time is LatelyWeek ?
func (this Datebin) IsLatelyWeek() bool {
if this.IsInvalid() {
return false
}
secondsPerWeek := float64(SecondsPerWeek)
difference := this.Now().ToStdTime().Sub(this.ToStdTime())
if difference.Seconds() > 0 && difference.Seconds() < secondsPerWeek {
return true
}
return false
}
// 时间是否是当前最近的一个月
// if the time is Lately Month ?
func (this Datebin) IsLatelyMonth() bool {
if this.IsInvalid() {
return false
}
now := this.Now()
if (now.Month() == 1 && this.Month() == 12) ||
(now.Month() == 12 && this.Month() == 1) {
return true
}
monthDifference := now.Month() - this.Month()
if absFormat(int64(monthDifference)) != 1 {
return false
}
return true
}
// 是否是当前月最后一天
// if the time is Month's Last day ?
func (this Datebin) IsLastOfMonth() bool {
if this.IsInvalid() {
return false
}
return this.DayOfMonth() == this.DaysInMonth()
}
// 是否当天开始
// if the time is start of day ?
func (this Datebin) IsStartOfDay() bool {
if this.IsInvalid() {
return false
}
return this.Format("H:i:s") == "00:00:00"
}
// 是否当天开始带微妙
// if the time is start of day with microsecond ?
func (this Datebin) IsStartOfDayWithMicrosecond() bool {
if this.IsInvalid() {
return false
}
return this.Format("H:i:s") == "00:00:00" &&
this.Microsecond() == 0
}
// 是否当天结束
// if the time is day end time ?
func (this Datebin) IsEndOfDay() bool {
if this.IsInvalid() {
return false
}
return this.Format("H:i:s") == "23:59:59"
}
// 是否当天结束带微妙
// if the time is end of day with microsecond ?
func (this Datebin) IsEndOfDayWithMicrosecond() bool {
if this.IsInvalid() {
return false
}
return this.Format("H:i:s") == "23:59:59" &&
this.Microsecond() == 999999
}
// 是否是半夜
// if the time is midnight time ?
func (this Datebin) IsMidnight() bool {
return this.IsStartOfDay()
}
// 是否是中午
// if the time is midday time ?
func (this Datebin) IsMidday(midDay ...string) bool {
if this.IsInvalid() {
return false
}
midDayAt := "12"
if len(midDay) > 0 {
midDayAt = midDay[0]
}
return this.Format("H:i:s") == fmt.Sprintf("%s:00:00", midDayAt)
}