forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traveler.go
executable file
·434 lines (371 loc) · 10.4 KB
/
traveler.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
package carbon
import (
"time"
)
// AddDuration add one duration
// 按照持续时长字符串增加时间,支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
func (c Carbon) AddDuration(duration string) Carbon {
if c.IsInvalid() {
return c
}
td, err := parseByDuration(duration)
c.Time = c.Time.In(c.Loc).Add(td)
c.Error = err
return c
}
// SubDuration subtraction one duration
// 按照持续时长字符串减少时间,支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
func (c Carbon) SubDuration(duration string) Carbon {
return c.AddDuration("-" + duration)
}
// AddCenturies add some centuries
// N个世纪后
func (c Carbon) AddCenturies(centuries int) Carbon {
return c.AddYears(centuries * YearsPerCentury)
}
// AddCenturiesNoOverflow add some centuries without overflowing month
// N个世纪后(月份不溢出)
func (c Carbon) AddCenturiesNoOverflow(centuries int) Carbon {
return c.AddYearsNoOverflow(centuries * YearsPerCentury)
}
// AddCentury add one century
// 1个世纪后
func (c Carbon) AddCentury() Carbon {
return c.AddCenturies(1)
}
// AddCenturyNoOverflow add one century without overflowing month
// 1个世纪后(月份不溢出)
func (c Carbon) AddCenturyNoOverflow() Carbon {
return c.AddCenturiesNoOverflow(1)
}
// SubCenturies subtraction some centuries
// N个世纪前
func (c Carbon) SubCenturies(centuries int) Carbon {
return c.SubYears(centuries * YearsPerCentury)
}
// SubCenturiesNoOverflow subtraction some centuries without overflowing month
// N个世纪前(月份不溢出)
func (c Carbon) SubCenturiesNoOverflow(centuries int) Carbon {
return c.SubYearsNoOverflow(centuries * YearsPerCentury)
}
// SubCentury subtraction one century
// 1个世纪前
func (c Carbon) SubCentury() Carbon {
return c.SubCenturies(1)
}
// SubCenturyNoOverflow subtraction one century without overflowing month
// 1个世纪前(月份不溢出)
func (c Carbon) SubCenturyNoOverflow() Carbon {
return c.SubCenturiesNoOverflow(1)
}
// AddDecades add some decades
// N个年代后
func (c Carbon) AddDecades(decades int) Carbon {
return c.AddYears(decades * YearsPerDecade)
}
// AddDecadesNoOverflow add some decades without overflowing month
// N个年代后(月份不溢出)
func (c Carbon) AddDecadesNoOverflow(decades int) Carbon {
return c.AddYearsNoOverflow(decades * YearsPerDecade)
}
// AddDecade add one decade
// 1个年代后
func (c Carbon) AddDecade() Carbon {
return c.AddDecades(1)
}
// AddDecadeNoOverflow add one decade without overflowing month
// 1个年代后(月份不溢出)
func (c Carbon) AddDecadeNoOverflow() Carbon {
return c.AddDecadesNoOverflow(1)
}
// SubDecades subtraction some decades
// N个年代后
func (c Carbon) SubDecades(decades int) Carbon {
return c.SubYears(decades * YearsPerDecade)
}
// SubDecadesNoOverflow subtraction some decades without overflowing month
// N个年代后(月份不溢出)
func (c Carbon) SubDecadesNoOverflow(decades int) Carbon {
return c.SubYearsNoOverflow(decades * YearsPerDecade)
}
// SubDecade subtraction one decade
// 1个年代后
func (c Carbon) SubDecade() Carbon {
return c.SubDecades(1)
}
// SubDecadeNoOverflow subtraction one decade without overflowing month
// 1个年代后(月份不溢出)
func (c Carbon) SubDecadeNoOverflow() Carbon {
return c.SubDecadesNoOverflow(1)
}
// AddYears add some years
// N年后
func (c Carbon) AddYears(years int) Carbon {
if c.IsInvalid() {
return c
}
c.Time = c.Time.In(c.Loc).AddDate(years, 0, 0)
return c
}
// AddYearsNoOverflow add some years without overflowing month
// N年后(月份不溢出)
func (c Carbon) AddYearsNoOverflow(years int) Carbon {
if c.IsInvalid() {
return c
}
// 获取N年后本月的最后一天
last := time.Date(c.Year()+years, time.Month(c.Month()), 1, c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Loc).AddDate(0, 1, -1)
day := c.Day()
if c.Day() > last.Day() {
day = last.Day()
}
c.Time = time.Date(last.Year(), last.Month(), day, c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Loc)
return c
}
// AddYear add one year
// 1年后
func (c Carbon) AddYear() Carbon {
return c.AddYears(1)
}
// AddYearNoOverflow add one year without overflowing month
// 1年后(月份不溢出)
func (c Carbon) AddYearNoOverflow() Carbon {
return c.AddYearsNoOverflow(1)
}
// SubYears subtraction some years
// N年前
func (c Carbon) SubYears(years int) Carbon {
if c.IsInvalid() {
return c
}
return c.AddYears(-years)
}
// SubYearsNoOverflow subtraction some years without overflowing month
// N年前(月份不溢出)
func (c Carbon) SubYearsNoOverflow(years int) Carbon {
return c.AddYearsNoOverflow(-years)
}
// SubYear subtraction one year
// 1年前
func (c Carbon) SubYear() Carbon {
return c.SubYears(1)
}
// SubYearNoOverflow subtraction one year without overflowing month
// 1年前(月份不溢出)
func (c Carbon) SubYearNoOverflow() Carbon {
return c.SubYearsNoOverflow(1)
}
// AddQuarters add some quarters
// N个季度后
func (c Carbon) AddQuarters(quarters int) Carbon {
return c.AddMonths(quarters * MonthsPerQuarter)
}
// AddQuartersNoOverflow add quarters without overflowing month
// N个季度后(月份不溢出)
func (c Carbon) AddQuartersNoOverflow(quarters int) Carbon {
return c.AddMonthsNoOverflow(quarters * MonthsPerQuarter)
}
// AddQuarter add one quarter
// 1个季度后
func (c Carbon) AddQuarter() Carbon {
return c.AddQuarters(1)
}
// AddQuarterNoOverflow add one quarter without overflowing month
// 1个季度后(月份不溢出)
func (c Carbon) AddQuarterNoOverflow() Carbon {
return c.AddQuartersNoOverflow(1)
}
// SubQuarters subtraction some quarters
// N个季度前
func (c Carbon) SubQuarters(quarters int) Carbon {
return c.AddQuarters(-quarters)
}
// SubQuartersNoOverflow subtraction some quarters without overflowing month
// N个季度前(月份不溢出)
func (c Carbon) SubQuartersNoOverflow(quarters int) Carbon {
return c.AddMonthsNoOverflow(-quarters * MonthsPerQuarter)
}
// SubQuarter subtraction one quarter
// 1个季度前
func (c Carbon) SubQuarter() Carbon {
return c.SubQuarters(1)
}
// SubQuarterNoOverflow subtraction one quarter without overflowing month
// 1个季度前(月份不溢出)
func (c Carbon) SubQuarterNoOverflow() Carbon {
return c.SubQuartersNoOverflow(1)
}
// AddMonths add some months
// N个月后
func (c Carbon) AddMonths(months int) Carbon {
if c.IsInvalid() {
return c
}
c.Time = c.Time.In(c.Loc).AddDate(0, months, 0)
return c
}
// AddMonthsNoOverflow add some months without overflowing month
// N个月后(月份不溢出)
func (c Carbon) AddMonthsNoOverflow(months int) Carbon {
if c.IsInvalid() {
return c
}
month := c.Month() + months
// 获取N月后的最后一天
last := time.Date(c.Year(), time.Month(month), 1, c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Loc).AddDate(0, 1, -1)
day := c.Day()
if c.Day() > last.Day() {
day = last.Day()
}
c.Time = time.Date(last.Year(), last.Month(), day, c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Loc)
return c
}
// AddMonth add one month
// 1个月后
func (c Carbon) AddMonth() Carbon {
return c.AddMonths(1)
}
// AddMonthNoOverflow add one month without overflowing month
// 1个月后(月份不溢出)
func (c Carbon) AddMonthNoOverflow() Carbon {
return c.AddMonthsNoOverflow(1)
}
// SubMonths subtraction some months
// N个月前
func (c Carbon) SubMonths(months int) Carbon {
return c.AddMonths(-months)
}
// SubMonthsNoOverflow subtraction some months without overflowing month
// N个月前(月份不溢出)
func (c Carbon) SubMonthsNoOverflow(months int) Carbon {
return c.AddMonthsNoOverflow(-months)
}
// SubMonth subtraction one month
// 1个月前
func (c Carbon) SubMonth() Carbon {
return c.SubMonths(1)
}
// SubMonthNoOverflow subtraction one month without overflowing month
// 1个月前(月份不溢出)
func (c Carbon) SubMonthNoOverflow() Carbon {
return c.SubMonthsNoOverflow(1)
}
// AddWeeks add some weeks
// N周后
func (c Carbon) AddWeeks(weeks int) Carbon {
return c.AddDays(weeks * DaysPerWeek)
}
// AddWeek add one week
// 1周后
func (c Carbon) AddWeek() Carbon {
return c.AddWeeks(1)
}
// SubWeeks subtraction some weeks
// N周前
func (c Carbon) SubWeeks(weeks int) Carbon {
return c.SubDays(weeks * DaysPerWeek)
}
// SubWeek subtraction one week
// 1周前
func (c Carbon) SubWeek() Carbon {
return c.SubWeeks(1)
}
// AddDays add some days
// N天后
func (c Carbon) AddDays(days int) Carbon {
if c.IsInvalid() {
return c
}
c.Time = c.Time.In(c.Loc).AddDate(0, 0, days)
return c
}
// AddDay add one day
// 1天后
func (c Carbon) AddDay() Carbon {
return c.AddDays(1)
}
// SubDays subtraction some days
// N天前
func (c Carbon) SubDays(days int) Carbon {
return c.AddDays(-days)
}
// SubDay subtraction one day
// 1天前
func (c Carbon) SubDay() Carbon {
return c.SubDays(1)
}
// AddHours add some hours
// N小时后
func (c Carbon) AddHours(hours int) Carbon {
if c.IsInvalid() {
return c
}
td := time.Duration(hours) * time.Hour
c.Time = c.Time.In(c.Loc).Add(td)
return c
}
// AddHour add one hour
// 1小时后
func (c Carbon) AddHour() Carbon {
return c.AddHours(1)
}
// SubHours subtraction some hours
// N小时前
func (c Carbon) SubHours(hours int) Carbon {
return c.AddHours(-hours)
}
// SubHour subtraction one hour
// 1小时前
func (c Carbon) SubHour() Carbon {
return c.SubHours(1)
}
// AddMinutes add some minutes
// N分钟后
func (c Carbon) AddMinutes(minutes int) Carbon {
if c.IsInvalid() {
return c
}
td := time.Duration(minutes) * time.Minute
c.Time = c.Time.Add(td)
return c
}
// AddMinute add one minute
// 1分钟后
func (c Carbon) AddMinute() Carbon {
return c.AddMinutes(1)
}
// SubMinutes subtraction some minutes
// N分钟前
func (c Carbon) SubMinutes(minutes int) Carbon {
return c.AddMinutes(-minutes)
}
// SubMinute subtraction one minute
// 1分钟前
func (c Carbon) SubMinute() Carbon {
return c.SubMinutes(1)
}
// AddSeconds add some seconds
// N秒钟后
func (c Carbon) AddSeconds(seconds int) Carbon {
if c.IsInvalid() {
return c
}
td := time.Duration(seconds) * time.Second
c.Time = c.Time.Add(td)
return c
}
// AddSecond add one second
// 1秒钟后
func (c Carbon) AddSecond() Carbon {
return c.AddSeconds(1)
}
// SubSeconds subtraction some seconds
// N秒钟前
func (c Carbon) SubSeconds(seconds int) Carbon {
return c.AddSeconds(-seconds)
}
// SubSecond subtraction one second
// 1秒钟前
func (c Carbon) SubSecond() Carbon {
return c.SubSeconds(1)
}