-
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathgetter.go
More file actions
executable file
·413 lines (366 loc) · 8.97 KB
/
getter.go
File metadata and controls
executable file
·413 lines (366 loc) · 8.97 KB
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
package carbon
import (
"time"
)
// StdTime gets standard time.Time.
func (c *Carbon) StdTime() StdTime {
if c.IsInvalid() {
return StdTime{}
}
if c.loc == nil {
return c.time
}
return c.time.In(c.loc)
}
// DaysInYear gets total days in year like 365.
func (c *Carbon) DaysInYear() int {
if c.IsInvalid() {
return 0
}
if c.IsLeapYear() {
return DaysPerLeapYear
}
return DaysPerNormalYear
}
// DaysInMonth gets total days in month like 30.
func (c *Carbon) DaysInMonth() int {
if c.IsInvalid() {
return 0
}
return c.EndOfMonth().StdTime().Day()
}
// MonthOfYear gets month of year like 12.
func (c *Carbon) MonthOfYear() int {
if c.IsInvalid() {
return 0
}
return int(c.StdTime().Month())
}
// DayOfYear gets day of year like 365.
func (c *Carbon) DayOfYear() int {
if c.IsInvalid() {
return 0
}
return c.StdTime().YearDay()
}
// DayOfMonth gets day of month like 30.
func (c *Carbon) DayOfMonth() int {
if c.IsInvalid() {
return 0
}
return c.StdTime().Day()
}
// DayOfWeek gets day of week like 6, start from 1.
func (c *Carbon) DayOfWeek() int {
if c.IsInvalid() {
return 0
}
return (int(c.StdTime().Weekday())+DaysPerWeek-int(c.weekStartsAt))%DaysPerWeek + 1
}
// WeekOfYear gets week of year like 1.
//
// refer to https://en.wikipedia.org/wiki/ISO_8601#Week_dates.
func (c *Carbon) WeekOfYear() int {
if c.IsInvalid() {
return 0
}
_, week := c.StdTime().ISOWeek()
return week
}
// WeekOfMonth gets week of month like 1.
func (c *Carbon) WeekOfMonth() int {
if c.IsInvalid() {
return 0
}
days := c.Day() + c.StartOfMonth().DayOfWeek() - 1
if days%DaysPerWeek == 0 {
return days / DaysPerWeek
}
return days/DaysPerWeek + 1
}
// DateTime gets current year, month, day, hour, minute, and second like 2020, 8, 5, 13, 14, 15.
func (c *Carbon) DateTime() (year, month, day, hour, minute, second int) {
if c.IsInvalid() {
return
}
year, month, day = c.Date()
hour, minute, second = c.Time()
return year, month, day, hour, minute, second
}
// DateTimeMilli gets current year, month, day, hour, minute, second and millisecond like 2020, 8, 5, 13, 14, 15, 999.
func (c *Carbon) DateTimeMilli() (year, month, day, hour, minute, second, millisecond int) {
if c.IsInvalid() {
return
}
year, month, day, hour, minute, second = c.DateTime()
return year, month, day, hour, minute, second, c.Millisecond()
}
// DateTimeMicro gets current year, month, day, hour, minute, second and microsecond like 2020, 8, 5, 13, 14, 15, 999999.
func (c *Carbon) DateTimeMicro() (year, month, day, hour, minute, second, microsecond int) {
if c.IsInvalid() {
return
}
year, month, day, hour, minute, second = c.DateTime()
return year, month, day, hour, minute, second, c.Microsecond()
}
// DateTimeNano gets current year, month, day, hour, minute, second and nanosecond like 2020, 8, 5, 13, 14, 15, 999999999.
func (c *Carbon) DateTimeNano() (year, month, day, hour, minute, second, nanosecond int) {
if c.IsInvalid() {
return
}
year, month, day, hour, minute, second = c.DateTime()
return year, month, day, hour, minute, second, c.Nanosecond()
}
// Date gets current year, month, and day like 2020, 8, 5.
func (c *Carbon) Date() (year, month, day int) {
if c.IsInvalid() {
return
}
var tm time.Month
year, tm, day = c.StdTime().Date()
return year, int(tm), day
}
// DateMilli gets current year, month, day and millisecond like 2020, 8, 5, 999.
func (c *Carbon) DateMilli() (year, month, day, millisecond int) {
if c.IsInvalid() {
return
}
year, month, day = c.Date()
return year, month, day, c.Millisecond()
}
// DateMicro gets current year, month, day and microsecond like 2020, 8, 5, 999999.
func (c *Carbon) DateMicro() (year, month, day, microsecond int) {
if c.IsInvalid() {
return
}
year, month, day = c.Date()
return year, month, day, c.Microsecond()
}
// DateNano gets current year, month, day and nanosecond like 2020, 8, 5, 999999999.
func (c *Carbon) DateNano() (year, month, day, nanosecond int) {
if c.IsInvalid() {
return
}
year, month, day = c.Date()
return year, month, day, c.Nanosecond()
}
// Time gets current hour, minute, and second like 13, 14, 15.
func (c *Carbon) Time() (hour, minute, second int) {
if c.IsInvalid() {
return
}
return c.StdTime().Clock()
}
// TimeMilli gets current hour, minute, second and millisecond like 13, 14, 15, 999.
func (c *Carbon) TimeMilli() (hour, minute, second, millisecond int) {
if c.IsInvalid() {
return
}
hour, minute, second = c.Time()
return hour, minute, second, c.Millisecond()
}
// TimeMicro gets current hour, minute, second and microsecond like 13, 14, 15, 999999.
func (c *Carbon) TimeMicro() (hour, minute, second, microsecond int) {
if c.IsInvalid() {
return
}
hour, minute, second = c.Time()
return hour, minute, second, c.Microsecond()
}
// TimeNano gets current hour, minute, second and nanosecond like 13, 14, 15, 999999999.
func (c *Carbon) TimeNano() (hour, minute, second, nanosecond int) {
if c.IsInvalid() {
return
}
hour, minute, second = c.Time()
return hour, minute, second, c.Nanosecond()
}
// Century gets current century like 21.
func (c *Carbon) Century() int {
if c.IsInvalid() {
return 0
}
return c.Year()/YearsPerCentury + 1
}
// Decade gets current decade like 20.
func (c *Carbon) Decade() int {
if c.IsInvalid() {
return 0
}
return c.Year() % YearsPerCentury / YearsPerDecade * YearsPerDecade
}
// Year gets current year like 2020.
func (c *Carbon) Year() int {
if c.IsInvalid() {
return 0
}
return c.StdTime().Year()
}
// Quarter gets current quarter like 3.
func (c *Carbon) Quarter() (quarter int) {
if c.IsInvalid() {
return
}
month := c.Month()
switch {
case month >= 10:
quarter = 4
case month >= 7:
quarter = 3
case month >= 4:
quarter = 2
case month >= 1:
quarter = 1
}
return
}
// Month gets current month like 8.
func (c *Carbon) Month() int {
return c.MonthOfYear()
}
// Week gets current week like 6, start from 0.
func (c *Carbon) Week() int {
if c.IsInvalid() {
return -1
}
return c.DayOfWeek() - 1
}
// Day gets current day like 5.
func (c *Carbon) Day() int {
return c.DayOfMonth()
}
// Hour gets current hour like 13.
func (c *Carbon) Hour() int {
if c.IsInvalid() {
return 0
}
return c.StdTime().Hour()
}
// Minute gets current minute like 14.
func (c *Carbon) Minute() int {
if c.IsInvalid() {
return 0
}
return c.StdTime().Minute()
}
// Second gets current second like 9.
func (c *Carbon) Second() int {
if c.IsInvalid() {
return 0
}
return c.StdTime().Second()
}
// Millisecond gets current millisecond like 999.
func (c *Carbon) Millisecond() int {
if c.IsInvalid() {
return 0
}
return c.StdTime().Nanosecond() / 1e6
}
// Microsecond gets current microsecond like 999999.
func (c *Carbon) Microsecond() int {
if c.IsInvalid() {
return 0
}
return c.StdTime().Nanosecond() / 1e3
}
// Nanosecond gets current nanosecond like 999999999.
func (c *Carbon) Nanosecond() int {
if c.IsInvalid() {
return 0
}
return c.StdTime().Nanosecond()
}
// Timestamp gets timestamp with second precision like 1596604455.
func (c *Carbon) Timestamp() int64 {
if c.IsInvalid() {
return 0
}
return c.StdTime().Unix()
}
// TimestampMilli gets timestamp with millisecond precision like 1596604455000.
func (c *Carbon) TimestampMilli() int64 {
if c.IsInvalid() {
return 0
}
return c.StdTime().UnixMilli()
}
// TimestampMicro gets timestamp with microsecond precision like 1596604455000000.
func (c *Carbon) TimestampMicro() int64 {
if c.IsInvalid() {
return 0
}
return c.StdTime().UnixMicro()
}
// TimestampNano gets timestamp with nanosecond precision like 1596604455000000000.
func (c *Carbon) TimestampNano() int64 {
if c.IsInvalid() {
return 0
}
return c.StdTime().UnixNano()
}
// Timezone gets timezone location like "Asia/Shanghai".
func (c *Carbon) Timezone() string {
if c.IsInvalid() {
return ""
}
return c.loc.String()
}
// ZoneName gets timezone name like "CST".
func (c *Carbon) ZoneName() string {
if c.IsInvalid() {
return ""
}
name, _ := c.StdTime().Zone()
return name
}
// ZoneOffset gets timezone offset seconds from the UTC timezone like 28800.
func (c *Carbon) ZoneOffset() int {
if c.IsInvalid() {
return 0
}
_, offset := c.StdTime().Zone()
return offset
}
// Locale gets locale name like "zh-CN".
func (c *Carbon) Locale() string {
if c.IsInvalid() {
return ""
}
if c.lang == nil {
return ""
}
return c.lang.locale
}
// WeekStartsAt returns start day of the week.
func (c *Carbon) WeekStartsAt() Weekday {
if c.IsInvalid() {
return 0
}
return c.weekStartsAt
}
// WeekEndsAt returns end day of the week.
func (c *Carbon) WeekEndsAt() Weekday {
if c.IsInvalid() {
return 0
}
return Weekday((int(c.weekStartsAt) + DaysPerWeek - 1) % DaysPerWeek)
}
// CurrentLayout returns the layout used for parsing the time string.
func (c *Carbon) CurrentLayout() string {
if c.IsInvalid() {
return ""
}
return c.currentLayout
}
// Age gets age like 18.
func (c *Carbon) Age() int {
if c.IsInvalid() {
return 0
}
now := Now().SetLocation(c.loc)
if c.Gte(now) {
return 0
}
return int(c.DiffInYears(now))
}