-
Notifications
You must be signed in to change notification settings - Fork 56
/
carbon.go
1864 lines (1521 loc) · 47 KB
/
carbon.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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 UNIPLACES
// 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 `carbon` is an extension to Go's time library based on PHP's Carbon library
package carbon
import (
"errors"
"math"
"strings"
"time"
)
// Represents the number of elements in a given period
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
)
// Represents the different string formats for dates
const (
DefaultFormat = "2006-01-02 15:04:05"
DateFormat = "2006-01-02"
FormattedDateFormat = "Jan 2, 2006"
TimeFormat = "15:04:05"
HourMinuteFormat = "15:04"
HourFormat = "15"
DayDateTimeFormat = "Mon, Jan 2, 2006 3:04 PM"
CookieFormat = "Monday, 02-Jan-2006 15:04:05 MST"
RFC822Format = "Mon, 02 Jan 06 15:04:05 -0700"
RFC1036Format = "Mon, 02 Jan 06 15:04:05 -0700"
RFC2822Format = "Mon, 02 Jan 2006 15:04:05 -0700"
RFC3339Format = "2006-01-02T15:04:05-07:00"
RSSFormat = "Mon, 02 Jan 2006 15:04:05 -0700"
)
// The Carbon type represents a Time instance.
// Provides a simple API extension for Time.
type Carbon struct {
time.Time
weekStartsAt time.Weekday
weekEndsAt time.Weekday
weekendDays []time.Weekday
stringFormat string
Translator *Translator
}
// Used for testing purposes
var (
isTimeFrozen bool
currentFrozenTime time.Time
)
// NewCarbon returns a pointer to a new Carbon instance
func NewCarbon(t time.Time) *Carbon {
wds := []time.Weekday{
time.Saturday,
time.Sunday,
}
return &Carbon{
Time: t,
weekStartsAt: time.Monday,
weekEndsAt: time.Sunday,
weekendDays: wds,
stringFormat: DefaultFormat,
Translator: translator(),
}
}
// Freeze allows time to be frozen to facilitate testing
func Freeze(time time.Time) {
currentFrozenTime = time
isTimeFrozen = true
}
// UnFreeze returns time to normal operation
func UnFreeze() {
isTimeFrozen = false
}
// IsTimeFrozen allows checking if time has been frozen
func IsTimeFrozen() bool {
return isTimeFrozen
}
// After will be behave like time.After unless time has been frozen
// If time is frozen it will add the expected delay and immediately send the frozen time on the returned channel
func After(d time.Duration) <-chan time.Time {
if isTimeFrozen {
currentFrozenTime = currentFrozenTime.Add(d)
c := make(chan time.Time, 1)
c <- currentFrozenTime
return c
}
return time.After(d)
}
// Tick will be behave like time.Tick unless time has been frozen
// If time is frozen it will tick normally but the date will be based on the frozen date
func Tick(d time.Duration) <-chan time.Time {
if isTimeFrozen {
c := make(chan time.Time, 1)
go func() {
for {
currentFrozenTime = currentFrozenTime.Add(d)
c <- currentFrozenTime
}
}()
return c
}
return time.Tick(d)
}
// Sleep will be behave like time.Sleep unless time has been frozen
// If time is frozen it will add the expected sleep delay and return immediately
func Sleep(d time.Duration) {
if isTimeFrozen && d > 0 {
currentFrozenTime = currentFrozenTime.Add(d)
return
}
time.Sleep(d)
}
// create returns a new carbon pointe. It is a helper function to create new dates
func create(y int, mon time.Month, d, h, m, s, ns int, l *time.Location) *Carbon {
return NewCarbon(time.Date(y, mon, d, h, m, s, ns, l))
}
// Create returns a new pointer to Carbon instance from a specific date and time.
// If the location is invalid, it returns an error instead.
func Create(y int, mon time.Month, d, h, m, s, ns int, location string) (*Carbon, error) {
l, err := time.LoadLocation(location)
if err != nil {
return nil, err
}
return create(y, mon, d, h, m, s, ns, l), nil
}
// CreateFromDate returns a new pointer to a Carbon instance from just a date.
// The time portion is set to now.
// If the location is invalid, it returns an error instead.
func CreateFromDate(y int, mon time.Month, d int, location string) (*Carbon, error) {
h, m, s := Now().Clock()
ns := Now().Nanosecond()
return Create(y, mon, d, h, m, s, ns, location)
}
// CreateFromTime returns a new pointer to a Carbon instance from just a date.
// The time portion is set to now.
// If the locations is invalid, it returns an error instead.
func CreateFromTime(h, m, s, ns int, location string) (*Carbon, error) {
y, mon, d := Now().Date()
return Create(y, mon, d, h, m, s, ns, location)
}
// CreateFromFormat returns a new pointer to a Carbon instance from a specific format.
// If the location is invalid, it returns an error instead.
func CreateFromFormat(layout, value string, location string) (*Carbon, 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 NewCarbon(t), nil
}
// CreateFromTimestamp returns a new pointer to a Carbon instance from a timestamp.
// If the location is invalid, it returns an error instead.
func CreateFromTimestamp(timestamp int64, location string) (*Carbon, error) {
l, err := time.LoadLocation(location)
if err != nil {
return nil, err
}
t := NewCarbon(Now().In(l))
t.SetTimestamp(timestamp)
return t, nil
}
// CreateFromTimestampUTC returns a new pointer to a Carbon instance from an UTC timestamp.
// If the location is invalid, it returns an error instead.
func CreateFromTimestampUTC(timestamp int64) (*Carbon, error) {
return CreateFromTimestamp(timestamp, "UTC")
}
// CreateFromMonthAndYear returns a new pointer to a Carbon instance from a specific month and year.
// If the location is invalid, it returns an error instead.
func CreateFromMonthAndYear(y int, mon time.Month, location string) (*Carbon, error) {
_, _, d := Now().Date()
h, m, s := Now().Clock()
ns := Now().Nanosecond()
return Create(y, mon, d, h, m, s, ns, location)
}
// Parse returns a pointer to a new carbon instance from a string
// If the location is invalid, it returns an error instead.
func Parse(layout, value, location string) (*Carbon, 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 NewCarbon(t), nil
}
// Today returns a pointer to a new carbon instance for today
// If the location is invalid, it returns an error instead.
func Today(location string) (*Carbon, error) {
l, err := time.LoadLocation(location)
if err != nil {
return nil, err
}
return NewCarbon(Now().In(l)), err
}
// Tomorrow returns a pointer to a new carbon instance for tomorrow
// If the location is invalid, it returns an error instead.
func Tomorrow(location string) (*Carbon, error) {
c, err := Today(location)
if err != nil {
return nil, err
}
return c.AddDay(), nil
}
// Yesterday returns a pointer to a new carbon instance for yesterday
// If the location is invalid, it returns an error instead.
func Yesterday(location string) (*Carbon, error) {
c, err := Today(location)
if err != nil {
return nil, err
}
return c.SubDay(), nil
}
// unixTimeInSeconds represents the number of seconds between Year 1 and 1970
const unixTimeInSeconds = 62135596801
const maxNSecs = 999999999
// MaxValue returns a pointer to a new carbon instance for greatest supported date
func MaxValue() *Carbon {
return NewCarbon(time.Unix(math.MaxInt64-unixTimeInSeconds, maxNSecs))
}
// MinValue returns a pointer to a new carbon instance for lowest supported date
func MinValue() *Carbon {
return NewCarbon(time.Unix(math.MinInt64+unixTimeInSeconds, 0))
}
// Now returns a new Carbon instance for right now in current localtime
func Now() *Carbon {
if isTimeFrozen {
return NewCarbon(currentFrozenTime)
}
return NewCarbon(time.Now())
}
// NowInLocation returns a new Carbon instance for right now in given location.
// The location is in IANA Time Zone database, such as "America/New_York".
func NowInLocation(loc string) (*Carbon, error) {
l, err := time.LoadLocation(loc)
if err != nil {
return nil, err
}
return nowIn(l), nil
}
func nowIn(loc *time.Location) *Carbon {
return NewCarbon(Now().In(loc))
}
// Copy returns a new copy of the current Carbon instance
func (c *Carbon) Copy() *Carbon {
return create(c.Year(), c.Month(), c.Day(), c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Location())
}
// WeekStartsAt get the starting day of the week
func (c *Carbon) WeekStartsAt() time.Weekday {
return c.weekStartsAt
}
// WeekEndsAt gets the ending day of the week
func (c *Carbon) WeekEndsAt() time.Weekday {
return c.weekEndsAt
}
// WeekendDays gets the weekend days of the week
func (c *Carbon) WeekendDays() []time.Weekday {
return c.weekendDays
}
// Quarter gets the current quarter
func (c *Carbon) Quarter() int {
month := c.Month()
switch {
case month < 4:
return 1
case month >= 4 && month < 7:
return 2
case month >= 7 && month < 10:
return 3
}
return 4
}
// Age gets the age from the current instance time to now
func (c *Carbon) Age() int {
return int(c.DiffInYears(Now(), true))
}
// DaysInMonth returns the number of days in the month
func (c *Carbon) DaysInMonth() int {
return c.EndOfMonth().Day()
}
// DaysInYear returns the number of days in the year
func (c *Carbon) DaysInYear() int {
if c.IsLeapYear() {
return daysInLeapYear
}
return daysInNormalYear
}
// WeekOfMonth returns the week of the month
func (c *Carbon) WeekOfMonth() int {
w := math.Ceil(float64(c.Day() / daysPerWeek))
return int(w + 1)
}
// WeekOfYear returns the week of the current year.
// This is an alias for time.ISOWeek
func (c *Carbon) WeekOfYear() (int, int) {
return c.ISOWeek()
}
// TimeZone gets the current timezone
func (c *Carbon) TimeZone() string {
return c.Location().String()
}
// Timestamp gets the current time since January 1, 1970 UTC
func (c *Carbon) Timestamp() int64 {
return c.Unix()
}
// String gets the current date using the previously set format
func (c *Carbon) String() string {
return c.Format(c.stringFormat)
}
// AddYears adds a year to the current time.
// Positive values travel forward while negative values travel into the past
func (c *Carbon) AddYears(y int) *Carbon {
return NewCarbon(c.AddDate(y, 0, 0))
}
// AddYear adds a year to the current time
func (c *Carbon) AddYear() *Carbon {
return c.AddYears(1)
}
// AddQuarters adds quarters to the current time.
// Positive values travel forward while negative values travel into the past
func (c *Carbon) AddQuarters(q int) *Carbon {
return NewCarbon(c.AddDate(0, monthsPerQuarter*q, 0))
}
// AddQuarter adds a quarter to the current time
func (c *Carbon) AddQuarter() *Carbon {
return c.AddQuarters(1)
}
// AddCenturies adds centuries to the time.
// Positive values travels forward while negative values travels into the past
func (c *Carbon) AddCenturies(cent int) *Carbon {
return NewCarbon(c.AddDate(yearsPerCenturies*cent, 0, 0))
}
// AddCentury adds a century to the current time
func (c *Carbon) AddCentury() *Carbon {
return c.AddCenturies(1)
}
// AddMonths adds months to the current time.
// Positive value travels forward while negative values travels into the past
func (c *Carbon) AddMonths(m int) *Carbon {
return NewCarbon(c.AddDate(0, m, 0))
}
// AddMonth adds a month to the current time
func (c *Carbon) AddMonth() *Carbon {
return c.AddMonths(1)
}
// AddSeconds adds seconds to the current time.
// Positive values travels forward while negative values travels into the past.
func (c *Carbon) AddSeconds(s int) *Carbon {
d := time.Duration(s) * time.Second
return NewCarbon(c.Add(d))
}
// AddSecond adds a second to the time
func (c *Carbon) AddSecond() *Carbon {
return c.AddSeconds(1)
}
// AddDays adds a day to the current time.
// Positive value travels forward while negative value travels into the past
func (c *Carbon) AddDays(d int) *Carbon {
return NewCarbon(c.AddDate(0, 0, d))
}
// AddDay adds a day to the current time
func (c *Carbon) AddDay() *Carbon {
return c.AddDays(1)
}
// AddWeekdays adds a weekday to the current time.
// Positive value travels forward while negative value travels into the past
func (c *Carbon) AddWeekdays(wd int) *Carbon {
d := 1
if wd < 0 {
wd, d = -wd, -d
}
t := c.Copy()
for wd > 0 {
t = t.AddDays(d)
if t.IsWeekday() {
wd--
}
}
return t
}
// AddWeekday adds a weekday to the current time
func (c *Carbon) AddWeekday() *Carbon {
return c.AddWeekdays(1)
}
// AddWeeks adds a week to the current time.
// Positive value travels forward while negative value travels into the past.
func (c *Carbon) AddWeeks(w int) *Carbon {
return NewCarbon(c.AddDate(0, 0, daysPerWeek*w))
}
// AddWeek adds a week to the current time
func (c *Carbon) AddWeek() *Carbon {
return c.AddWeeks(1)
}
// AddHours adds an hour to the current time.
// Positive value travels forward while negative value travels into the past
func (c *Carbon) AddHours(h int) *Carbon {
d := time.Duration(h) * time.Hour
return NewCarbon(c.Add(d))
}
// AddHour adds an hour to the current time
func (c *Carbon) AddHour() *Carbon {
return c.AddHours(1)
}
// AddMonthsNoOverflow adds a month to the current time, not overflowing in case the
// destination month has less days than the current one.
// Positive value travels forward while negative value travels into the past.
func (c *Carbon) AddMonthsNoOverflow(m int) *Carbon {
addedDate := NewCarbon(c.AddDate(0, m, 0))
if c.Day() != addedDate.Day() {
return addedDate.PreviousMonthLastDay()
}
return addedDate
}
// PreviousMonthLastDay returns the last day of the previous month
func (c *Carbon) PreviousMonthLastDay() *Carbon {
return NewCarbon(c.AddDate(0, 0, -c.Day()))
}
// AddMonthNoOverflow adds a month with no overflow to the current time
func (c *Carbon) AddMonthNoOverflow() *Carbon {
return c.AddMonthsNoOverflow(1)
}
// AddMinutes adds minutes to the current time.
// Positive value travels forward while negative value travels into the past.
func (c *Carbon) AddMinutes(m int) *Carbon {
d := time.Duration(m) * time.Minute
return NewCarbon(c.Add(d))
}
// AddMinute adds a minute to the current time
func (c *Carbon) AddMinute() *Carbon {
return c.AddMinutes(1)
}
// SubYear removes a year from the current time
func (c *Carbon) SubYear() *Carbon {
return c.SubYears(1)
}
// SubYears removes years from current time
func (c *Carbon) SubYears(y int) *Carbon {
return c.AddYears(-1 * y)
}
// SubQuarter removes a quarter from the current time
func (c *Carbon) SubQuarter() *Carbon {
return c.SubQuarters(1)
}
// SubQuarters removes quarters from current time
func (c *Carbon) SubQuarters(q int) *Carbon {
return c.AddQuarters(-q)
}
// SubCentury removes a century from the current time
func (c *Carbon) SubCentury() *Carbon {
return c.SubCenturies(1)
}
// SubCenturies removes centuries from the current time
func (c *Carbon) SubCenturies(cent int) *Carbon {
return c.AddCenturies(-cent)
}
// SubMonth removes a month from the current time
func (c *Carbon) SubMonth() *Carbon {
return c.SubMonths(1)
}
// SubMonths removes months from the current time
func (c *Carbon) SubMonths(m int) *Carbon {
return c.AddMonths(-m)
}
// SubMonthNoOverflow remove a month with no overflow from the current time
func (c *Carbon) SubMonthNoOverflow() *Carbon {
return c.SubMonthsNoOverflow(1)
}
// SubMonthsNoOverflow removes months with no overflow from the current time
func (c *Carbon) SubMonthsNoOverflow(m int) *Carbon {
return c.AddMonthsNoOverflow(-m)
}
// SubDay removes a day from the current instance
func (c *Carbon) SubDay() *Carbon {
return c.SubDays(1)
}
// SubDays removes days from the current time
func (c *Carbon) SubDays(d int) *Carbon {
return c.AddDays(-d)
}
// SubWeekday removes a weekday from the current time
func (c *Carbon) SubWeekday() *Carbon {
return c.SubWeekdays(1)
}
// SubWeekdays removes a weekday from the current time
func (c *Carbon) SubWeekdays(wd int) *Carbon {
return c.AddWeekdays(-wd)
}
// SubWeek removes a week from the current time
func (c *Carbon) SubWeek() *Carbon {
return c.SubWeeks(1)
}
// SubWeeks removes weeks to the current time
func (c *Carbon) SubWeeks(w int) *Carbon {
return c.AddWeeks(-w)
}
// SubHour removes an hour from the current time
func (c *Carbon) SubHour() *Carbon {
return c.SubHours(1)
}
// SubHours removes hours from the current time
func (c *Carbon) SubHours(h int) *Carbon {
return c.AddHours(-h)
}
// SubMinute removes a minute from the current time
func (c *Carbon) SubMinute() *Carbon {
return c.SubMinutes(1)
}
// SubMinutes removes minutes from the current time
func (c *Carbon) SubMinutes(m int) *Carbon {
return c.AddMinutes(-m)
}
// SubSecond removes a second from the current time
func (c *Carbon) SubSecond() *Carbon {
return c.SubSeconds(1)
}
// SubSeconds removes seconds from the current time
func (c *Carbon) SubSeconds(s int) *Carbon {
return c.AddSeconds(-s)
}
// SetYear sets the year of the current time
func (c *Carbon) SetYear(y int) {
c.Time = time.Date(y, c.Month(), c.Day(), c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Location())
}
// SetMonth sets the month of the current time
func (c *Carbon) SetMonth(m time.Month) {
c.Time = time.Date(c.Year(), m, c.Day(), c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Location())
}
// SetDay sets the day of the current time
func (c *Carbon) SetDay(d int) {
c.Time = time.Date(c.Year(), c.Month(), d, c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Location())
}
// SetHour sets the hour of the current time
func (c *Carbon) SetHour(h int) {
c.Time = time.Date(c.Year(), c.Month(), c.Day(), h, c.Minute(), c.Second(), c.Nanosecond(), c.Location())
}
// SetMinute sets the minute of the current time
func (c *Carbon) SetMinute(m int) {
c.Time = time.Date(c.Year(), c.Month(), c.Day(), c.Hour(), m, c.Second(), c.Nanosecond(), c.Location())
}
// SetSecond sets the second of the current time
func (c *Carbon) SetSecond(s int) {
c.Time = time.Date(c.Year(), c.Month(), c.Day(), c.Hour(), c.Minute(), s, c.Nanosecond(), c.Location())
}
// SetDate sets only the date of the current time
func (c *Carbon) SetDate(y int, m time.Month, d int) {
c.Time = time.Date(y, m, d, c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Location())
}
// SetDateTime sets the date and the time
func (c *Carbon) SetDateTime(y int, mon time.Month, d, h, m, s int) {
c.Time = time.Date(y, mon, d, h, m, s, c.Nanosecond(), c.Location())
}
// SetTimeFromTimeString receives a string and sets the current time
// It accepts the following formats: "hh:mm:ss", "hh:mm" and "hh"
func (c *Carbon) SetTimeFromTimeString(timeString string) error {
layouts := []string{
TimeFormat,
HourMinuteFormat,
HourFormat,
}
var t time.Time
var err error
for i, layout := range layouts {
t, err = time.Parse(layout, timeString)
if err == nil {
h, m, s := t.Clock()
switch i {
case 1:
s = c.Second()
case 2:
m, s = c.Minute(), c.Second()
}
c.SetHour(h)
c.SetMinute(m)
c.SetSecond(s)
return nil
}
}
return errors.New("only supports hh:mm:ss, hh:mm and hh formats")
}
// SetWeekEndsAt sets the last day of week
func (c *Carbon) SetWeekEndsAt(wd time.Weekday) {
c.weekEndsAt = wd
}
// SetWeekStartsAt sets the first day of week
func (c *Carbon) SetWeekStartsAt(wd time.Weekday) {
c.weekStartsAt = wd
}
// SetWeekendDays sets the weekend days
func (c *Carbon) SetWeekendDays(wds []time.Weekday) {
c.weekendDays = wds
}
// SetTimestamp sets the current time given a timestamp
func (c *Carbon) SetTimestamp(sec int64) {
c.Time = time.Unix(sec, 0).In(c.Location())
}
// SetTimeZone sets the location from a string
// If the location is invalid, it returns an error instead.
func (c *Carbon) SetTimeZone(name string) error {
loc, err := time.LoadLocation(name)
if err != nil {
return err
}
c.Time = time.Date(c.Year(), c.Month(), c.Day(), c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), loc)
return nil
}
// ResetStringFormat changes the format to the DefaultFormat
func (c *Carbon) ResetStringFormat() {
c.stringFormat = DefaultFormat
}
// SetStringFormat formats the current time with the set format string
func (c *Carbon) SetStringFormat(format string) {
c.stringFormat = format
}
// DateString return the current time in Y-m-d format
func (c *Carbon) DateString() string {
return c.Format(DateFormat)
}
// FormattedDateString returns the current time as a readable date
func (c *Carbon) FormattedDateString() string {
return c.Format(FormattedDateFormat)
}
// TimeString returns the current time in hh:mm:ss format
func (c *Carbon) TimeString() string {
return c.Format(TimeFormat)
}
// DateTimeString returns the current time in Y-m-d hh:mm:ss format
func (c *Carbon) DateTimeString() string {
return c.Format(DefaultFormat)
}
// DayDateTimeString returns the current time with a day, date and time format
func (c *Carbon) DayDateTimeString() string {
return c.Format(DayDateTimeFormat)
}
// AtomString formats the current time to a Atom date format
func (c *Carbon) AtomString() string {
return c.Format(RFC3339Format)
}
// CookieString formats the current time to a Cookie date format
func (c *Carbon) CookieString() string {
return c.Format(CookieFormat)
}
// ISO8601String returns the current time in ISO8601 format
func (c *Carbon) ISO8601String() string {
return c.Format(RFC3339Format)
}
// RFC822String returns the current time in RFC 822 format
func (c *Carbon) RFC822String() string {
return c.Format(RFC822Format)
}
// RFC850String returns the current time in RFC 850 format
func (c *Carbon) RFC850String() string {
return c.Format(time.RFC850)
}
// RFC1036String returns the current time in RFC 1036 format
func (c *Carbon) RFC1036String() string {
return c.Format(RFC1036Format)
}
// RFC1123String returns the current time in RFC 1123 format
func (c *Carbon) RFC1123String() string {
return c.Format(time.RFC1123Z)
}
// RFC2822String returns the current time in RFC 2822 format
func (c *Carbon) RFC2822String() string {
return c.Format(RFC2822Format)
}
// RFC3339String returns the current time in RFC 3339 format
func (c *Carbon) RFC3339String() string {
return c.Format(RFC3339Format)
}
// RSSString returns the current time for RSS format
func (c *Carbon) RSSString() string {
return c.Format(RSSFormat)
}
// W3CString returns the current time for WWW Consortium format
func (c *Carbon) W3CString() string {
return c.Format(RFC3339Format)
}
// IsWeekday determines if the current time is a weekday
func (c *Carbon) IsWeekday() bool {
return !c.IsWeekend()
}
// IsWeekend determines if the current time is a weekend day
func (c *Carbon) IsWeekend() bool {
d := c.Weekday()
for _, wd := range c.WeekendDays() {
if d == wd {
return true
}
}
return false
}
// IsYesterday determines if the current time is yesterday
func (c *Carbon) IsYesterday() bool {
n := Now().SubDay()
return c.IsSameDay(n)
}
// IsToday determines if the current time is today
func (c *Carbon) IsToday() bool {
return c.IsSameDay(Now())
}
// IsTomorrow determines if the current time is tomorrow
func (c *Carbon) IsTomorrow() bool {
n := Now().AddDay()
return c.IsSameDay(n)
}
// IsFuture determines if the current time is in the future, ie. greater (after) than now
func (c *Carbon) IsFuture() bool {
return c.After(Now().Time)
}
// IsPast determines if the current time is in the past, ie. less (before) than now
func (c *Carbon) IsPast() bool {
return c.Before(Now().Time)
}
// IsLeapYear determines if current current time is a leap year
func (c *Carbon) IsLeapYear() bool {
y := c.Year()
if (y%4 == 0 && y%100 != 0) || y%400 == 0 {
return true
}
return false
}
// IsLongYear determines if the instance is a long year
func (c *Carbon) IsLongYear() bool {
carb := create(c.Year(), time.December, 31, 0, 0, 0, 0, c.Location())
_, w := carb.WeekOfYear()
return w == weeksPerLongYear
}
// IsSameAs compares the formatted values of the two dates.
// If passed date is nil, compares against today
func (c *Carbon) IsSameAs(format string, carb *Carbon) bool {
if carb == nil {
return c.Format(DefaultFormat) == Now().Format(DefaultFormat)
}
return c.Format(DefaultFormat) == carb.Format(DefaultFormat)
}
// IsCurrentYear determines if the current time is in the current year
func (c *Carbon) IsCurrentYear() bool {
return c.Year() == Now().Year()
}
// IsSameYear checks if the passed in date is in the same year as the current time year.
// If passed date is nil, compares against today
func (c *Carbon) IsSameYear(carb *Carbon) bool {
if carb == nil {
return c.Year() == nowIn(c.Location()).Year()
}
return c.Year() == carb.Year()
}
// IsCurrentMonth determines if the current time is in the current month
func (c *Carbon) IsCurrentMonth() bool {
return c.Month() == Now().Month()
}
// IsSameMonth checks if the passed in date is in the same month as the current month
// If passed date is nil, compares against today
func (c *Carbon) IsSameMonth(carb *Carbon, sameYear bool) bool {
m := nowIn(c.Location()).Month()
if carb != nil {
m = carb.Month()
}
if sameYear {
return c.IsSameYear(carb) && c.Month() == m
}
return c.Month() == m
}
// IsSameDay checks if the passed in date is the same day as the current day.
// If passed date is nil, compares against today
func (c *Carbon) IsSameDay(carb *Carbon) bool {
n := nowIn(c.Location())
if carb != nil {
n = carb
}
return c.Year() == n.Year() && c.Month() == n.Month() && c.Day() == n.Day()
}
// IsSunday checks if this day is a Sunday.
func (c *Carbon) IsSunday() bool {
return c.Weekday() == time.Sunday
}
// IsMonday checks if this day is a Monday.
func (c *Carbon) IsMonday() bool {
return c.Weekday() == time.Monday
}
// IsTuesday checks if this day is a Tuesday.
func (c *Carbon) IsTuesday() bool {
return c.Weekday() == time.Tuesday
}
// IsWednesday checks if this day is a Wednesday.
func (c *Carbon) IsWednesday() bool {
return c.Weekday() == time.Wednesday
}
// IsThursday checks if this day is a Thursday.
func (c *Carbon) IsThursday() bool {
return c.Weekday() == time.Thursday
}
// IsFriday checks if this day is a Friday.
func (c *Carbon) IsFriday() bool {
return c.Weekday() == time.Friday
}
// IsSaturday checks if this day is a Saturday.
func (c *Carbon) IsSaturday() bool {
return c.Weekday() == time.Saturday
}
// IsLastWeek returns true is the date is within last week
func (c *Carbon) IsLastWeek() bool {