-
Notifications
You must be signed in to change notification settings - Fork 0
/
timekit.go
714 lines (599 loc) · 17.4 KB
/
timekit.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// Copyright 2021 echotrue
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// package `timkit` is a time toolkit for Golang reference PHP's library `Carbon`
package timkit
import (
"strings"
"sync"
"time"
)
const (
secondsPerMinute = 60
minutesPerHour = 60
hoursPerDay = 24
daysPerWeek = 7
monthsPerQuarter = 3
monthsPerYear = 12
yearsPerCenturies = 100
yearsPerDecade = 10
weeksPerLongYear = 53
daysInLeapYear = 366
daysInNormalYear = 365
secondsInWeek = 691200
)
// String formats for dates
const (
DefaultFormat = "2006-01-02 15:04:05"
DateFormat = "2006-01-02"
TimeFormat = "15:04:05"
)
// The TimeKit type represents a time instance
// Provides a simple API extension for Time
type TimeKit struct {
time.Time
format string
weekendDays []time.Weekday
weekStartAt time.Weekday
weekEndAt time.Weekday
lock sync.Mutex
}
// NewTimeKit return a pointer to a new NewTimeKit instance
func NewTimeKit(t time.Time) *TimeKit {
return NewOptions(OptionSetTime(t))
}
type Option func(t *TimeKit)
func NewOptions(opt ...Option) *TimeKit {
t := TimeKit{
Time: time.Now(),
format: DefaultFormat,
weekendDays: []time.Weekday{time.Saturday, time.Sunday},
weekStartAt: time.Monday,
weekEndAt: time.Sunday,
}
for _, o := range opt {
o(&t)
}
return &t
}
// Now return a new TimeKit instance for current time in local
func Now() *TimeKit {
return NewTimeKit(time.Now())
}
// NowWithLocation return a new TimeKit instance for current time in given location
func NowWithLocation(loc string) (*TimeKit, error) {
l, err := time.LoadLocation(loc)
if err != nil {
return nil, err
}
return NewTimeKit(Now().In(l)), nil
}
// Parse return a new TimeKit instance from a string
// layout reference `DefaultFormat` or time.ANSIC
// value reference `2020-12-23 11:13:11`
// location default `UTC` .
func Parse(layout, value, location string) (*TimeKit, error) {
l, err := time.LoadLocation(location)
if err != nil {
return nil, err
}
t, err := time.ParseInLocation(layout, value, l)
if err != nil {
return nil, err
}
return NewTimeKit(t), nil
}
// CreateFromTimestamp return a new TimeKit instance from a timestamp
// If the location is invalid , it return an error
func CreateFromTimestamp(timestamp int64, location string) (*TimeKit, error) {
l, err := time.LoadLocation(location)
if err != nil {
return nil, err
}
return createFromTimestamp(timestamp, l), nil
}
func createFromTimestamp(timestamp int64, l *time.Location) *TimeKit {
return NewTimeKit(time.Unix(timestamp, 0).In(l))
}
// OptionSetFormat format the time with this format string
func OptionSetFormat(format string) Option {
return func(t *TimeKit) {
t.format = format
}
}
// OptionSetWeekStartAt set the start of week
func OptionSetWeekStartAt(s time.Weekday) Option {
return func(t *TimeKit) {
t.weekStartAt = s
}
}
// OptionSetWeekEndAt set end of week
func OptionSetWeekEndAt(e time.Weekday) Option {
return func(t *TimeKit) {
t.weekStartAt = e
}
}
// OptionSetTime set the time by given sec
func OptionSetTime(datetime time.Time) Option {
return func(t *TimeKit) {
t.Time = datetime
}
}
// SetFormat format the time with this format string
func (tk *TimeKit) SetFormat(format string) {
tk.lock.Lock()
defer tk.lock.Unlock()
tk.format = format
}
// SetTime set the time by given sec
func (tk *TimeKit) SetTime(t time.Time) {
tk.lock.Lock()
defer tk.lock.Unlock()
tk.Time = t
}
// SetWeekStartsAt set the start of week
func (tk *TimeKit) SetWeekStartsAt(start time.Weekday) {
tk.lock.Lock()
defer tk.lock.Unlock()
tk.weekStartAt = start
}
// SetWeekEndsAt set end of week
func (tk *TimeKit) SetWeekEndsAt(end time.Weekday) {
tk.lock.Lock()
defer tk.lock.Unlock()
tk.weekEndAt = end
}
// SetTimestamp set the time by given sec todo delete
func (tk *TimeKit) SetTimestamp(sec int64) {
tk.SetTime(time.Unix(sec, 0).In(tk.Location()))
}
// AddCenturies add centuries from the current time
func (tk *TimeKit) AddCenturies(centuries int) *TimeKit {
tk.SetTime(tk.AddDate(yearsPerCenturies*centuries, 0, 0))
return tk
}
// AddCentury add a century from the current time
func (tk *TimeKit) AddCentury() *TimeKit {
return tk.AddCenturies(1)
}
// SubCenturies remove centuries from the current time
func (tk *TimeKit) SubCenturies(centuries int) *TimeKit {
return tk.AddCenturies(-centuries)
}
// SubCentury remove a century from the current time
func (tk *TimeKit) SubCentury() *TimeKit {
return tk.SubCenturies(1)
}
// AddDays add days from the current time
func (tk *TimeKit) AddDays(d int) *TimeKit {
tk.SetTime(tk.AddDate(0, 0, d))
return tk
}
// AddDays add a day from the current time
func (tk *TimeKit) AddDay() *TimeKit {
return tk.AddDays(1)
}
// SubDays remove days from the current time
func (tk *TimeKit) SubDays(d int) *TimeKit {
return tk.AddDays(-d)
}
// SubDay remove a day from the current time
func (tk *TimeKit) SubDay() *TimeKit {
return tk.SubDays(1)
}
// AddHours add hours from the current time
func (tk *TimeKit) AddHours(h int) *TimeKit {
d := time.Duration(h) * time.Hour
tk.SetTime(tk.Add(d))
return tk
}
// AddHour add an hour from the current time
func (tk *TimeKit) AddHour() *TimeKit {
return tk.AddHours(1)
}
// SubHours remove hours from current time
func (tk *TimeKit) SubHours(h int) *TimeKit {
return tk.AddHours(-h)
}
// SubHour remove an hours from the current time
func (tk *TimeKit) SubHour() *TimeKit {
return tk.SubHours(1)
}
// AddMinutes add minutes from current time
func (tk *TimeKit) AddMinutes(m int) *TimeKit {
d := time.Duration(m) * time.Minute
tk.SetTime(tk.Add(d))
return tk
}
// AddMinute add a minute from current time
func (tk *TimeKit) AddMinute() *TimeKit {
return tk.AddMinutes(1)
}
// SubMinutes remove minutes from current time
func (tk *TimeKit) SubMinutes(m int) *TimeKit {
return tk.AddMinutes(-m)
}
// SubMinute remove a minute from current time
func (tk *TimeKit) SubMinute() *TimeKit {
return tk.SubMinutes(1)
}
// AddMonths add months from current time
func (tk *TimeKit) AddMonths(m int) *TimeKit {
tk.SetTime(tk.AddDate(0, m, 0))
return tk
}
// AddMonth add a month from current time
func (tk *TimeKit) AddMonth() *TimeKit {
return tk.AddMonths(1)
}
// SubMonths remove months from current time
func (tk *TimeKit) SubMonths(m int) *TimeKit {
return tk.AddMonths(-m)
}
// SubMonth remove a month from current time
func (tk *TimeKit) SubMonth() *TimeKit {
return tk.SubMonths(1)
}
// AddMonthsNoOverflow add months from current time .
// Not overflowing in case the days of new month is less than the current one
func (tk *TimeKit) AddMonthsNoOverflow(m int) *TimeKit {
newDate := tk.AddDate(0, m, 0)
if tk.Day() != newDate.Day() {
newDate = tk.LastDayOfPreMonth()
}
tk.SetTime(newDate)
return tk
}
// AddMonthNoOverflow add a month with no overflow from current time
func (tk *TimeKit) AddMonthNoOverflow() *TimeKit {
return tk.AddMonthsNoOverflow(1)
}
// SubMonthsNoOverflow remove months with no overflow from current time
func (tk *TimeKit) SubMonthsNoOverflow(m int) *TimeKit {
return tk.AddMonthsNoOverflow(-m)
}
// SubMonthNoOverflow remove a month with no overflow from current time
func (tk *TimeKit) SubMonthNoOverflow() *TimeKit {
return tk.SubMonthsNoOverflow(1)
}
// AddQuarters add quarters from current time
func (tk *TimeKit) AddQuarters(q int) *TimeKit {
tk.SetTime(tk.AddDate(0, monthsPerQuarter*q, 0))
return tk
}
// AddQuarter add a quarter from current time
func (tk *TimeKit) AddQuarter() *TimeKit {
return tk.AddQuarters(1)
}
// SubQuarters remove quarters from current time
func (tk *TimeKit) SubQuarters(q int) *TimeKit {
return tk.AddQuarters(-q)
}
// SubQuarter remove a quarter from current time
func (tk *TimeKit) SubQuarter() *TimeKit {
return tk.SubQuarters(1)
}
// AddSeconds add seconds from current time
func (tk *TimeKit) AddSeconds(s int) *TimeKit {
d := time.Duration(s) * time.Second
tk.SetTime(tk.Add(d))
return tk
}
// AddSecond add a second from current time
func (tk *TimeKit) AddSecond() *TimeKit {
return tk.AddSeconds(1)
}
// SubSeconds remove seconds from current time
func (tk *TimeKit) SubSeconds(s int) *TimeKit {
return tk.AddSeconds(-s)
}
// SubSecond remove a second from current time
func (tk *TimeKit) SubSecond() *TimeKit {
return tk.SubSeconds(1)
}
// AddWeeks add weeks from current time
func (tk *TimeKit) AddWeeks(w int) *TimeKit {
tk.SetTime(tk.AddDate(0, 0, daysPerWeek*w))
return tk
}
// AddWeek add a week from current time
func (tk *TimeKit) AddWeek() *TimeKit {
return tk.AddWeeks(1)
}
// SubWeeks remove weeks from current time
func (tk *TimeKit) SubWeeks(w int) *TimeKit {
return tk.AddWeeks(-w)
}
// SubWeek remove a week from current time
func (tk *TimeKit) SubWeek() *TimeKit {
return tk.SubWeeks(1)
}
// AddWeekdays add weekdays from current time
func (tk *TimeKit) AddWeekdays(wd int) *TimeKit {
step := 1
if wd < 0 {
wd, step = -wd, -step
}
for wd > 0 {
tk.AddDays(step)
if tk.IsWeekday() {
wd--
}
}
return tk
}
// AddWeekday add a weekday fro current time
func (tk *TimeKit) AddWeekday() *TimeKit {
return tk.AddWeekdays(1)
}
// SubWeekdays remove weekdays from current time
func (tk *TimeKit) SubWeekdays(wd int) *TimeKit {
return tk.AddWeekdays(-wd)
}
// SubWeekday remove a weekday fro current time
func (tk *TimeKit) SubWeekday() *TimeKit {
return tk.SubWeekdays(1)
}
// AddYears add years from current time
func (tk *TimeKit) AddYears(y int) *TimeKit {
tk.SetTime(tk.AddDate(y, 0, 0))
return tk
}
// AddYear add year from current time
func (tk *TimeKit) AddYear() *TimeKit {
return tk.AddYears(1)
}
// SubYears remove years from current time
func (tk *TimeKit) SubYears(y int) *TimeKit {
return tk.AddYears(-y)
}
// SubYear remove year from current time
func (tk *TimeKit) SubYear() *TimeKit {
return tk.SubYears(1)
}
// DiffInSeconds return the difference in seconds
func (tk *TimeKit) DiffInSeconds(t *TimeKit, abs bool) int64 {
if t == nil {
t = createFromTimestamp(time.Now().Unix(), tk.Location())
}
diff := t.Timestamp() - tk.Timestamp()
return absoluteValue(abs, diff)
}
// DiffInMinutes return the difference in minutes
func (tk *TimeKit) DiffInMinutes(t *TimeKit, abs bool) int64 {
return tk.DiffInSeconds(t, abs) / secondsPerMinute
}
// DiffInHours return the difference in hours
func (tk *TimeKit) DiffInHours(t *TimeKit, abs bool) int64 {
return tk.DiffInMinutes(t, abs) / minutesPerHour
}
// DiffInDays return the difference in days
func (tk *TimeKit) DiffInDays(t *TimeKit, abs bool) int64 {
return tk.DiffInHours(t, abs) / hoursPerDay
}
// DiffInMonths return the difference in months
func (tk *TimeKit) DiffInMonths(t *TimeKit, abs bool) int64 {
if t == nil {
t = createFromTimestamp(time.Now().Unix(), tk.Location())
}
tkCopy := tk.Copy()
tCopy := t.Copy()
if tkCopy.Location() != tCopy.Location() {
tkCopy.Time = tkCopy.In(time.UTC)
tCopy.Time = tCopy.In(time.UTC)
}
return countMonthDifference(tkCopy.Time, tCopy.Time, abs)
}
// DiffDurationInString return the duration in string
func (tk *TimeKit) DiffDurationInString(t *TimeKit) string {
if t == nil {
t = createFromTimestamp(time.Now().Unix(), tk.Location())
}
return strings.Replace(tk.Sub(t.Time).String(), "-", "", 1)
}
func countMonthDifference(t1, t2 time.Time, abs bool) int64 {
y1 := t1.Year()
y2 := t2.Year()
m1 := int(t1.Month())
m2 := int(t2.Month())
d1 := t1.Day()
d2 := t2.Day()
yearInterval := y1 - y2
if m1 < m2 || m1 == m2 && d1 < d2 {
yearInterval--
}
monthInterval := (m1 + 12) - m2
if d1 < d2 {
monthInterval--
}
monthInterval %= 12
return absoluteValue(abs, int64(yearInterval*12+monthInterval))
}
type Filter func(*TimeKit) bool
// swap swap params
func swap(a, b *TimeKit) (*TimeKit, *TimeKit) {
return b, a
}
// DiffFiltered return the difference by Unit `duration` and using a filter
//
func (tk *TimeKit) DiffFiltered(t *TimeKit, duration time.Duration, f Filter, abs bool) int64 {
if t == nil {
t = createFromTimestamp(time.Now().Unix(), tk.Location())
}
var diffNumber int64
step := int64(duration.Seconds())
start, end := tk.Copy(), t.Copy()
inverse := false
if start.After(end.Time) {
start, end = swap(start, end)
inverse = true
}
for start.DiffInSeconds(end, true)/step > 0 {
if f(end) {
diffNumber++
}
end.SetTime(end.Add(-duration))
}
if inverse {
diffNumber = -diffNumber
}
return absoluteValue(abs, diffNumber)
}
// DiffInDaysFiltered return difference in days using filter
func (tk *TimeKit) DiffInDaysFiltered(t *TimeKit, f Filter, abs bool) int64 {
return tk.DiffFiltered(t, time.Hour*hoursPerDay, f, abs)
}
// DiffInHoursFiltered return difference in hours using filter
func (tk *TimeKit) DiffInHoursFiltered(t *TimeKit, f Filter, abs bool) int64 {
return tk.DiffFiltered(t, time.Hour, f, abs)
}
// StartOfCentury return the datetime of start of the century
func (tk *TimeKit) StartOfCentury() *TimeKit {
year := tk.Year() - tk.Year()%yearsPerCenturies
t := time.Date(year, time.January, 1, 0, 0, 0, 0, tk.Location())
tk.SetTime(t)
return tk
}
// EndOfCentury return the datetime of end of the century
func (tk *TimeKit) EndOfCentury() *TimeKit {
year := tk.Year() - 1 - tk.Year()%yearsPerCenturies + yearsPerCenturies
t := time.Date(year, time.December, 31, 23, 59, 59, 0, tk.Location())
tk.SetTime(t)
return tk
}
// StartOfYear return datetime of start of the year
func (tk *TimeKit) StartOfYear() *TimeKit {
tk.SetTime(time.Date(tk.Year(), time.January, 1, 0, 0, 0, 0, tk.Location()))
return tk
}
// EndOfYear return datetime of start of the year
func (tk *TimeKit) EndOfYear() *TimeKit {
tk.SetTime(time.Date(tk.Year(), time.December, 31, 23, 59, 59, 0, tk.Location()))
return tk
}
// StartOfQuarter return datetime of start of the quarter
func (tk *TimeKit) StartOfQuarter() *TimeKit {
m := time.Month((tk.Quarter()-1)*monthsPerQuarter + 1)
tk.SetTime(time.Date(tk.Year(), m, 1, 0, 0, 0, 0, tk.Location()))
return tk
}
// EndOfQuarter return datetime of end of the quarter
func (tk *TimeKit) EndOfQuarter() *TimeKit {
m := tk.Quarter() * monthsPerQuarter
tk.SetTime(time.Date(tk.Year(), time.Month(m), 1, 23, 59, 59, 0, tk.Location()).AddDate(0, 1, -1))
return tk
}
// StartOfMonth return datetime of start of the month
func (tk *TimeKit) StartOfMonth() *TimeKit {
tk.SetTime(time.Date(tk.Year(), tk.Month(), 1, 0, 0, 0, 0, tk.Location()))
return tk
}
// EndOfMonth return datetime of end of the month
func (tk *TimeKit) EndOfMonth() *TimeKit {
tk.SetTime(time.Date(tk.Year(), tk.Month(), 1, 0, 0, 0, 0, tk.Location()).AddDate(0, 1, -1))
return tk
}
// StartOfWeek return datetime of start of the Week
func (tk *TimeKit) StartOfWeek() *TimeKit {
t := time.Date(tk.Year(), tk.Month(), tk.Day(), 0, 0, 0, 0, tk.Location())
for t.Weekday() != tk.weekStartAt {
t = t.AddDate(0, 0, -1)
}
tk.SetTime(t)
return tk
}
// EndOfWeek return datetime of end of the Week
func (tk *TimeKit) EndOfWeek() *TimeKit {
t := time.Date(tk.Year(), tk.Month(), tk.Day(), 23, 59, 59, 0, tk.Location())
for t.Weekday() != tk.weekEndAt {
t = t.AddDate(0, 0, 1)
}
tk.SetTime(t)
return tk
}
// StartOfDay return datetime of start of the Day
func (tk *TimeKit) StartOfDay() *TimeKit {
t := time.Date(tk.Year(), tk.Month(), tk.Day(), 0, 0, 0, 0, tk.Location())
tk.SetTime(t)
return tk
}
// EndOfDay return datetime of end of the Day
func (tk *TimeKit) EndOfDay() *TimeKit {
t := time.Date(tk.Year(), tk.Month(), tk.Day(), 23, 59, 59, 0, tk.Location())
tk.SetTime(t)
return tk
}
// LastDayOfPreMonth return the last day of the previous month
func (tk *TimeKit) LastDayOfPreMonth() time.Time {
return tk.AddDate(0, 0, -tk.Day())
}
// Timestamp returns the current time's seconds since Jan 1 1970 (Unix time).
func (tk *TimeKit) Timestamp() int64 {
return tk.Unix()
}
// Copy return a new TimeKit instance of current instance
func (tk *TimeKit) Copy() *TimeKit {
return createFromTimestamp(tk.Time.Unix(), tk.Location())
}
// IsWeekend whether the current time is a weekend day
func (tk *TimeKit) IsWeekend() bool {
d := tk.Weekday()
for _, v := range tk.weekendDays {
if d == v {
return true
}
}
return false
}
// IsWeekday whether the current time is a weekday
func (tk *TimeKit) IsWeekday() bool {
return !tk.IsWeekend()
}
// WeekendDays return the weekend days of the week
func (tk *TimeKit) WeekendDays() []time.Weekday {
return tk.weekendDays
}
func (tk *TimeKit) Quarter() int {
m := tk.Month()
switch {
case m < 4:
return 1
case m >= 4 && m < 7:
return 2
case m >= 7 && m < 10:
return 3
}
return 4
}
// String gets the time string using the previously set format
func (tk *TimeKit) String() string {
return tk.Format(tk.format)
}
// DateTimeString get the date string
func (tk *TimeKit) DateTimeString() string {
return tk.Format(DefaultFormat)
}
// DateString get the date string
func (tk *TimeKit) DateString() string {
return tk.Format(DateFormat)
}
// TimeString get the time string
func (tk *TimeKit) TimeString() string {
return tk.Format(TimeFormat)
}
// absolute return the abs value if need
func absoluteValue(abs bool, value int64) int64 {
if abs && value < 0 {
return -value
}
return value
}