-
Notifications
You must be signed in to change notification settings - Fork 36
/
dart_date.dart
1132 lines (917 loc) · 39.2 KB
/
dart_date.dart
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
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago_lib;
class Interval {
late final DateTime _start;
late final Duration _duration;
Interval(DateTime start, DateTime end) {
if (start.isAfter(end)) {
throw RangeError('Invalid Range');
}
_start = start;
_duration = end.difference(start);
}
Duration get duration => _duration;
DateTime get start => _start;
DateTime get end => _start.add(_duration);
Interval setStart(DateTime val) => Interval(val, end);
Interval setEnd(DateTime val) => Interval(start, val);
Interval setDuration(Duration val) => Interval(start, start.add(duration));
bool includes(DateTime date) =>
(date.isAfter(start) || date.isAtSameMomentAs(start)) &&
(date.isBefore(end) || date.isAtSameMomentAs(end));
bool contains(Interval interval) =>
includes(interval.start) && includes(interval.end);
bool cross(Interval other) => includes(other.start) || includes(other.end);
bool equals(Interval other) =>
start.isAtSameMomentAs(other.start) && end.isAtSameMomentAs(other.end);
Interval union(Interval other) {
if (cross(other)) {
if (end.isAfter(other.start) || end.isAtSameMomentAs(other.start)) {
return Interval(start, other.end);
} else if (other.end.isAfter(start) ||
other.end.isAtSameMomentAs(start)) {
return Interval(other.start, end);
} else {
throw RangeError('Error this: $this; other: $other');
}
} else {
throw RangeError('Intervals don\'t cross');
}
}
Interval intersection(Interval other) {
if (!cross(other)) {
if (other.contains(this)) {
return this;
}
throw RangeError('Intervals don\'t cross');
}
final intersectionStart = Date.max(start, other.start);
final intersectionEnd = Date.min(end, other.end);
return Interval(intersectionStart, intersectionEnd);
}
Interval? difference(Interval other) {
if (other == this) {
return null;
} else if (this <= other) {
// | this | | other |
if (end.isBefore(other.start)) {
return this;
} else {
return Interval(start, other.start);
}
} else if (this >= other) {
// | other | | this |
if (other.end.isBefore(start)) {
return this;
} else {
return Interval(other.end, end);
}
} else {
throw RangeError('Error this: $this; other: $other');
}
}
List<Interval?> symetricDiffetence(Interval other) {
final list = <Interval?>[null, null];
try {
list[0] = difference(other);
} catch (e) {
list[0] = null;
}
try {
list[1] = other.difference(this);
} catch (e) {
list[1] = null;
}
return list;
}
// Operators
bool operator <(Interval other) => start.isBefore(other.start);
bool operator <=(Interval other) =>
start.isBefore(other.start) || start.isAtSameMomentAs(other.start);
bool operator >(Interval other) => end.isAfter(other.end);
bool operator >=(Interval other) =>
end.isAfter(other.end) || end.isAtSameMomentAs(other.end);
@override
String toString() => '<${start} | ${end} | ${duration} >';
}
const int MILLISECONDS_IN_WEEK = 604800000;
extension Date on DateTime {
/// Number of seconds since epoch time / A.K.A Unix timestamp
///
/// The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of
/// seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting
/// leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).
/// Literally speaking the epoch is Unix time 0 (midnight 1/1/1970).
static DateTime fromSecondsSinceEpoch(
int secondsSinceEpoch, {
bool isUtc = false,
}) =>
DateTime.fromMillisecondsSinceEpoch(
secondsSinceEpoch * 1000,
isUtc: isUtc,
);
/// Transforms a date that follows a pattern from a [String] representation to a [DateTime] object
static DateTime parse(
String dateString, {
String? pattern,
String locale = 'en_US',
bool isUTC = false,
}) {
initializeDateFormatting();
return pattern == null
? DateTime.parse(dateString)
: DateFormat(pattern, locale).parse(dateString, isUTC);
}
/// Create a [Date] object from a Unix timestamp
static DateTime unix(int seconds) => fromSecondsSinceEpoch(seconds);
/// Tomorrow at same hour / minute / second than now
static DateTime get tomorrow => DateTime.now().nextDay;
/// Yesterday at same hour / minute / second than now
static DateTime get yesterday => DateTime.now().previousDay;
/// Current date (Same as [Date.now])
static DateTime get today => DateTime.now();
/// Get [Date] object as UTC of current object.
DateTime get toUTC => toUtc();
/// Get [Date] object in LocalTime of current object.
DateTime get toLocalTime => toLocal();
DateTime get clone => DateTime.fromMicrosecondsSinceEpoch(
microsecondsSinceEpoch,
isUtc: isUtc,
);
// /// Add a [Duration] to this date
// DateTime add(Duration duration) {
// return add(duration);
// }
/// Substract a [Duration] to this date
DateTime subtract(Duration duration) => add(Duration.zero - duration);
/// Get the difference between this data and other date as a [Duration]
Duration diff(DateTime other) => difference(other);
/// Add a certain amount of days to this date
DateTime addDays(int amount, [bool ignoreDaylightSavings = false]) =>
ignoreDaylightSavings
? DateTime(year, month, day + amount, hour, minute, second,
millisecond, microsecond)
: add(Duration(days: amount));
/// Add a certain amount of hours to this date
DateTime addHours(int amount, [bool ignoreDaylightSavings = false]) =>
ignoreDaylightSavings
? DateTime(year, month, day, hour + amount, minute, second,
millisecond, microsecond)
: add(Duration(hours: amount));
// TODO: this
// Date addISOYears(int amount) {
// return this;
// }
/// Add a certain amount of milliseconds to this date
DateTime addMilliseconds(int amount) => add(Duration(milliseconds: amount));
/// Add a certain amount of microseconds to this date
DateTime addMicroseconds(int amount) => add(Duration(microseconds: amount));
/// Add a certain amount of minutes to this date
DateTime addMinutes(int amount, [bool ignoreDaylightSavings = false]) =>
ignoreDaylightSavings
? DateTime(year, month, day, hour, minute + amount, second,
millisecond, microsecond)
: add(Duration(minutes: amount));
/// Add a certain amount of months to this date
DateTime addMonths(int amount) => clone.setMonth(month + amount);
/// Add a certain amount of quarters to this date
DateTime addQuarters(int amount) => addMonths(amount * 3);
/// Add a certain amount of seconds to this date
DateTime addSeconds(int amount, [bool ignoreDaylightSavings = false]) =>
ignoreDaylightSavings
? DateTime(year, month, day, hour, minute, second + amount,
millisecond, microsecond)
: add(Duration(seconds: amount));
/// Add a certain amount of weeks to this date
DateTime addWeeks(int amount) => addDays(amount * 7);
/// Add a certain amount of years to this date
DateTime addYears(int amount) => clone.setYear(year + amount);
/// Know if two ranges of dates overlaps
static bool areRangesOverlapping(
DateTime initialRangeStartDate,
DateTime initialRangeEndDate,
DateTime comparedRangeStartDate,
DateTime comparedRangeEndDate,
) {
if (initialRangeStartDate.isAfter(initialRangeEndDate)) {
throw RangeError('Not valid initial range');
}
if (comparedRangeStartDate.isAfter(comparedRangeEndDate)) {
throw RangeError('Not valid compareRange range');
}
final initial = Interval(initialRangeStartDate, initialRangeEndDate);
final compared = Interval(comparedRangeStartDate, comparedRangeEndDate);
return initial.cross(compared) || compared.cross(initial);
}
/// Get index of the closest day to current one, returns null if empty [Iterable] is passed as argument
int? closestIndexTo(Iterable<DateTime> datesArray) {
final differences = datesArray.map((date) {
return date.difference(this).abs();
});
if (datesArray.isEmpty) {
return null;
}
var index = 0;
for (var i = 0; i < differences.length; i++) {
if (differences.elementAt(i) < differences.elementAt(index)) {
index = i;
}
}
return index;
}
/// Get closest day to current one, returns null if empty [Iterable] is passed as argument
DateTime? closestTo(Iterable<DateTime> datesArray) {
if (datesArray.isEmpty) {
return null;
}
final index = closestIndexTo(datesArray);
if (index == null) {
return null;
}
return datesArray.elementAt(index);
}
/// Compares this Date object to [other],
/// returning zero if the values are equal.
/// Returns a negative value if this Date [isBefore] [other]. It returns 0
/// if it [isAtSameMomentAs] [other], and returns a positive value otherwise
/// (when this [isAfter] [other]).
int compare(DateTime other) => compareTo(other);
/// Returns true if left [isBefore] than right
static DateTime min(DateTime left, DateTime right) =>
(left < right) ? left : right;
/// Returns true if left [isAfter] than right
static DateTime max(DateTime left, DateTime right) =>
(left < right) ? right : left;
/// Compare the two dates and return 1 if the first date [isAfter] the second,
/// -1 if the first date [isBefore] the second or 0 first date [isEqual] the second.
static int compareAsc(DateTime dateLeft, DateTime dateRight) {
if (dateLeft.isAfter(dateRight)) {
return 1;
} else if (dateLeft.isBefore(dateRight)) {
return -1;
} else {
return 0;
}
}
/// Compare the two dates and return -1 if the first date [isAfter] the second,
/// 1 if the first date [isBefore] the second or 0 first date [isEqual] the second.
static int compareDesc(DateTime dateLeft, DateTime dateRight) =>
(-1) * compareAsc(dateLeft, dateRight);
// int differenceInCalendarDays(dateLeft, dateRight)
// int differenceInCalendarISOWeeks(dateLeft, dateRight)
// int differenceInCalendarISOYears(dateLeft, dateRight)
// int differenceInCalendarMonths(dateLeft, dateRight)
// int differenceInCalendarQuarters(dateLeft, dateRight)
// int differenceInCalendarWeeks(dateLeft, dateRight, [options])
// int differenceInCalendarYears(dateLeft, dateRight)
// int differenceInISOYears(dateLeft, dateRight)
/// Difference in microseconds between this date and other
int differenceInMicroseconds(DateTime other) => diff(other).inMicroseconds;
/// Difference in milliseconds between this date and other
int differenceInMilliseconds(DateTime other) => diff(other).inMilliseconds;
/// Difference in minutes between this date and other
int differenceInMinutes(DateTime other) => diff(other).inMinutes;
/// Difference in seconds between this date and other
int differenceInSeconds(DateTime other) => diff(other).inSeconds;
/// Difference in hours between this date and other
int differenceInHours(DateTime other) => diff(other).inHours;
/// Difference in days between this date and other
int differenceInDays(DateTime other) => diff(other).inDays;
// int differenceInMonths(dateLeft, dateRight)
// int differenceInQuarters(dateLeft, dateRight)
// int differenceInWeeks(dateLeft, dateRight)
// int differenceInYears(dateLeft, dateRight)
/// Formats provided [date] to a fuzzy time like 'a moment ago' (use timeago package to change locales)
///
/// - If [locale] is passed will look for message for that locale, if you want
/// to add or override locales use [setLocaleMessages]. Defaults to 'en'
/// - If [clock] is passed this will be the point of reference for calculating
/// the elapsed time. Defaults to DateTime.now()
/// - If [allowFromNow] is passed, format will use the From prefix, ie. a date
/// 5 minutes from now in 'en' locale will display as '5 minutes from now'
/// If locales was not loaded previously en would be used use timeago.setLocaleMessages to set them
String timeago({String? locale, DateTime? clock, bool? allowFromNow}) =>
timeago_lib.format(
this,
locale: locale,
clock: clock,
allowFromNow: allowFromNow ?? false,
);
/// Return an Iterable of dates which is inclusive to [this] but exclusive to [date]
Iterable<DateTime> eachDay(
DateTime date, {
bool ignoreDaylightSavings = false,
}) sync* {
if (isSameDay(date)) {
yield date.startOfDay;
return;
}
final daysDiff = differenceInDays(date);
for (var i = 0; i != daysDiff; i += daysDiff.sign) {
yield this.addDays(-i, ignoreDaylightSavings);
}
}
/// Return the end of a day for this date. The result will be in the local timezone.
DateTime get endOfDay => clone.setHour(23, 59, 59, 999, 999);
/// Return the end of the hour for this date. The result will be in the local timezone.
DateTime get endOfHour => clone.setMinute(59, 59, 999, 999);
/// Return the end of ISO week for this date. The result will be in the local timezone.
DateTime get endOfISOWeek => startOfISOWeek.addDays(6).endOfDay;
// DateTime endOfISOYear()
/// Return the end of the minute for this date. The result will be in the local timezone.
DateTime get endOfMinute => clone.setSecond(59, 999, 999);
/// Return the end of the month for this date. The result will be in the local timezone.
DateTime get endOfMonth => DateTime(year, month + 1).subMicroseconds(1);
// Date endOfQuarter()
/// Return the end of the second for this date. The result will be in the local timezone.
DateTime get endOfSecond => clone.setMillisecond(999, 999);
/// Return the end of today. The result will be in the local timezone.
static DateTime get endOfToday => DateTime.now().endOfDay;
/// Return the end of tomorrow. The result will be in the local timezone.
static DateTime get endOfTomorrow => DateTime.now().nextDay.endOfDay;
/// Return the end of yesterday. The result will be in the local timezone.
static DateTime get endOfYesterday => DateTime.now().previousDay.endOfDay;
/// Return the end of the week for this date. The result will be in the local timezone.
DateTime get endOfWeek => nextWeek.startOfWeek.subMicroseconds(1);
/// Return the end of the year for this date. The result will be in the local timezone.
DateTime get endOfYear => clone.setYear(year, DateTime.december).endOfMonth;
/// Get the day of the month of the given date.
/// The day of the month 1..31.
int get getDate => day;
/// Get the day of the week of the given date.
int get getDay => weekday;
/// Days since year started. The result will be in the local timezone.
int get getDayOfYear => diff(startOfYear).inDays + 1;
/// Days since month started. The result will be in the local timezone.
int get getDaysInMonth => endOfMonth.diff(startOfMonth).inDays + 1;
/// Number of days in current year
int get getDaysInYear => endOfYear.diff(startOfYear).inDays + 1;
/// Get the hours of the given date.
/// The hour of the day, expressed as in a 24-hour clock 0..23.
int get getHours => hour;
// int getISODay(date)
// int getISOYear(date)
/// Get the milliseconds of the given date.
/// The millisecond 0...999.
int get getMilliseconds => millisecond;
/// Get the microseconds of the given date.
/// The microsecond 0...999.
int get getMicroseconds => microsecond;
/// Get the milliseconds since the 'Unix epoch' 1970-01-01T00:00:00Z (UTC).
int get getMillisecondsSinceEpoch => millisecondsSinceEpoch;
/// Get the microseconds since the 'Unix epoch' 1970-01-01T00:00:00Z (UTC).
int get getMicrosecondsSinceEpoch => microsecondsSinceEpoch;
/// Get the minutes of the given date.
/// The minute 0...59.
int get getMinutes => minute;
/// Get the month of the given date.
/// The month 1..12.
int get getMonth => month;
// int getOverlappingDaysInRanges(initialRangeStartDate, initialRangeEndDate, comparedRangeStartDate, comparedRangeEndDate)
// int getQuarter(date)
/// Get the seconds of the given date.
/// The second 0...59.
int get getSeconds => second;
/// get the numer of milliseconds since epoch
int get timestamp => millisecondsSinceEpoch;
/// get the numer of milliseconds since epoch
int get getTime => millisecondsSinceEpoch;
/// The year
int get getYear => year;
/// The time zone name.
/// This value is provided by the operating system and may be an abbreviation or a full name.
/// In the browser or on Unix-like systems commonly returns abbreviations, such as 'CET' or 'CEST'.
/// On Windows returns the full name, for example 'Pacific Standard Time'.
String get getTimeZoneName => timeZoneName;
/// The time zone offset, which is the difference between local time and UTC.
/// The offset is positive for time zones east of UTC.
/// Note, that JavaScript, Python and C return the difference between UTC and local time.
/// Java, C# and Ruby return the difference between local time and UTC.
Duration get getTimeZoneOffset => timeZoneOffset;
/// The day of the week monday..sunday.
/// In accordance with ISO 8601 a week starts with Monday, which has the value 1.
int get getWeekday => weekday;
/// Get the week index
int get getWeek => addDays(1).getISOWeek;
/// Get the ISO week index
int get getISOWeek {
final woy = ((_ordinalDate - weekday + 10) ~/ 7);
// If the week number equals zero, it means that the given date belongs to the preceding (week-based) year.
if (woy == 0) {
// The 28th of December is always in the last week of the year
return DateTime(year - 1, 12, 28).getISOWeek;
}
// If the week number equals 53, one must check that the date is not actually in week 1 of the following year
if (woy == 53 &&
DateTime(year, 1, 1).weekday != DateTime.thursday &&
DateTime(year, 12, 31).weekday != DateTime.thursday) {
return 1;
}
return woy;
}
int get _ordinalDate {
const offsets = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
return offsets[month - 1] + day + (isLeapYear && month > 2 ? 1 : 0);
}
/// Get the local week-numbering year
int get getWeekYear {
final startOfNextYear = DateTime(year + 1).startOfWeek;
if (millisecondsSinceEpoch >= startOfNextYear.millisecondsSinceEpoch) {
return year + 1;
} else {
final startOfThisYear = DateTime(year).startOfWeek;
if (millisecondsSinceEpoch >= startOfThisYear.millisecondsSinceEpoch) {
return year;
} else {
return year - 1;
}
}
}
/// Return true if other [isEqual] or [isAfter] to this date
bool isSameOrAfter(DateTime other) => this == other || isAfter(other);
/// Return true if other [isEqual] or [isBefore] to this date
bool isSameOrBefore(DateTime other) => this == other || isBefore(other);
/// Check if a Object if a [DateTime], use for validation purposes
static bool isDate(argument) => argument is DateTime;
/// Check if a date is [equals] to other
bool isEqual(other) => equals(other);
/// Return true if this date day is monday
bool get isMonday => weekday == DateTime.monday;
/// Return true if this date day is tuesday
bool get isTuesday => weekday == DateTime.tuesday;
/// Return true if this date day is wednesday
bool get isWednesday => weekday == DateTime.wednesday;
/// Return true if this date day is thursday
bool get isThursday => weekday == DateTime.thursday;
/// Return true if this date day is friday
bool get isFriday => weekday == DateTime.friday;
/// Return true if this date day is saturday
bool get isSaturday => weekday == DateTime.saturday;
/// Return true if this date day is sunday
bool get isSunday => weekday == DateTime.sunday;
/// Is the given date the first day of a month?
bool get isFirstDayOfMonth => isSameDay(startOfMonth);
/// Return true if this date [isAfter] [Date.now]
bool get isFuture => isAfter(DateTime.now());
/// Is the given date the last day of a month?
bool get isLastDayOfMonth =>
isSameDay(nextMonth.startOfMonth.subHours(12).startOfDay);
/// Is the given date in the leap year?
bool get isLeapYear => year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
/// Return true if this date [isBefore] [Date.now]
bool get isPast => isBefore(DateTime.now());
/// Check if this date is in the same day than other
bool isSameDay(DateTime other) => startOfDay == other.startOfDay;
/// Check if this date is in the same hour than other
bool isSameHour(DateTime other) => startOfHour == other.startOfHour;
// bool isSameISOWeek(dateLeft, dateRight)
// bool isSameISOYear(dateLeft, dateRight)
/// Check if this date is in the same minute than other
bool isSameMinute(DateTime other) => startOfMinute == other.startOfMinute;
/// Check if this date is in the same month than other
bool isSameMonth(DateTime other) => startOfMonth == other.startOfMonth;
// bool isSameQuarter(dateLeft, dateRight)
/// Check if this date is in the same second than other
bool isSameSecond(DateTime other) =>
secondsSinceEpoch == other.secondsSinceEpoch;
// bool isSameWeek(dateLeft, dateRight, [options])
/// Check if this date is in the same year than other
bool isSameYear(DateTime other) => year == other.year;
/// Check if this date is in the same hour than [DateTime.now]
bool get isThisHour => startOfHour == today.startOfHour;
// bool isThisISOWeek()
// bool isThisISOYear()
/// Check if this date is in the same minute than [DateTime.now]
bool get isThisMinute => startOfMinute == today.startOfMinute;
/// Check if this date is in the same month than [DateTime.now]
bool get isThisMonth => isSameMonth(today);
// bool isThisQuarter()
/// Check if this date is in the same second than [DateTime.now]
bool get isThisSecond => isSameSecond(today);
// bool isThisWeek(, [options])
/// Check if this date is in the same year than [DateTime.now]
bool get isThisYear => isSameYear(today);
// bool isValid()
/// Check if this date is in the same day than [DateTime.today]
bool get isToday => isSameDay(today);
/// Check if this date is in the same day than [DateTime.tomorrow]
bool get isTomorrow => isSameDay(tomorrow);
/// Check if this date is in the same day than [DateTime.yesterday]
bool get isYesterday => isSameDay(yesterday);
/// Return true if this [DateTime] is set as UTC.
bool get isUTC => isUtc;
/// Return true if this [DateTime] is a saturday or a sunday
bool get isWeekend =>
weekday == DateTime.saturday || weekday == DateTime.sunday;
/// Checks if a [DateTime] is within a Rage (two dates that makes an [Interval])
bool isWithinRange(DateTime startDate, DateTime endDate) =>
Interval(startDate, endDate).includes(this);
/// Checks if a [DateTime] is within an [Interval]
bool isWithinInterval(Interval interval) => interval.includes(this);
// DateTime lastDayOfISOWeek(date)
// DateTime lastDayOfISOYear(date)
// DateTime lastDayOfMonth(date)
// DateTime lastDayOfQuarter(date)
// DateTime lastDayOfWeek(date, [options])
// DateTime lastDayOfYear(date)
// static DateTime max(Iterable<DateTime>)
// static DateTime min(Iterable<DateTime>)
// static DateTime parse(any)
// DateTime setDate(date, dayOfMonth)
// DateTime setDayOfYear(date, dayOfYear)
// DateTime setISODay(date, day)
// DateTime setISOWeek(date, isoWeek)
// DateTime setISOYear(date, isoYear)
/// Change [year] of this date
///
/// set [month] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [day] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [hour] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [minute] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [second] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [millisecond] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [microsecond] if you want to change it as well
DateTime setYear(
int year, [
int? month,
int? day,
int? hour,
int? minute,
int? second,
int? millisecond,
int? microsecond,
]) =>
DateTime(
year,
month ?? this.month,
day ?? this.day,
hour ?? this.hour,
minute ?? this.minute,
second ?? this.second,
millisecond ?? this.millisecond,
microsecond ?? this.microsecond,
);
/// Change [month] of this date
///
/// set [day] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [hour] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [minute] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [second] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [millisecond] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [microsecond] if you want to change it as well
DateTime setMonth(
int month, [
int? day,
int? hour,
int? minute,
int? second,
int? millisecond,
int? microsecond,
]) =>
DateTime(
year,
month,
day ?? this.day,
hour ?? this.hour,
minute ?? this.minute,
second ?? this.second,
millisecond ?? this.millisecond,
microsecond ?? this.microsecond,
);
/// Change [day] of this date
///
/// set [hour] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [minute] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [second] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [millisecond] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [microsecond] if you want to change it as well
DateTime setDay(
int day, [
int? hour,
int? minute,
int? second,
int? millisecond,
int? microsecond,
]) =>
DateTime(
year,
month,
day,
hour ?? this.hour,
minute ?? this.minute,
second ?? this.second,
millisecond ?? this.millisecond,
microsecond ?? this.microsecond,
);
/// Change [weekday] of this date
///
/// set [weekStartsOn] if you want to use any other day as the start of the week
DateTime setWeekDay(int weekday, [int weekStartsOn = DateTime.sunday]) {
const daysPerWeek = DateTime.daysPerWeek;
final currentDay = this.weekday;
final reminder = weekday % daysPerWeek;
final dayIndex = (reminder + daysPerWeek) % daysPerWeek;
final delta = daysPerWeek - weekStartsOn;
final diff = weekday < 0 || weekday > 6
? weekday - ((currentDay + delta) % daysPerWeek)
: ((dayIndex + delta) % daysPerWeek) -
((currentDay + delta) % daysPerWeek);
return addDays(diff);
}
/// Change [hour] of this date
///
/// set [minute] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [second] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [millisecond] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [microsecond] if you want to change it as well
DateTime setHour(
int hour, [
int? minute,
int? second,
int? millisecond,
int? microsecond,
]) =>
DateTime(
year,
month,
day,
hour,
minute ?? this.minute,
second ?? this.second,
millisecond ?? this.millisecond,
microsecond ?? this.microsecond,
);
/// Change [minute] of this date
///
/// set [second] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [millisecond] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [microsecond] if you want to change it as well
DateTime setMinute(
int minute, [
int? second,
int? millisecond,
int? microsecond,
]) =>
DateTime(
year,
month,
day,
hour,
minute,
second ?? this.second,
millisecond ?? this.millisecond,
microsecond ?? this.microsecond,
);
/// Change [second] of this date
///
/// set [millisecond] if you want to change it as well, to skip an change other optional field set it as [null]
/// set [microsecond] if you want to change it as well
DateTime setSecond(
int second, [
int? millisecond,
int? microsecond,
]) =>
DateTime(
year,
month,
day,
hour,
minute,
second,
millisecond ?? this.millisecond,
microsecond ?? this.microsecond,
);
/// Change [millisecond] of this date
///
/// set [microsecond] if you want to change it as well
DateTime setMillisecond(
int millisecond, [
int? microsecond,
]) =>
DateTime(
year,
month,
day,
hour,
minute,
second,
millisecond,
microsecond ?? this.microsecond,
);
/// Change [microsecond] of this date
DateTime setMicrosecond(
int microsecond,
) =>
DateTime(
year,
month,
day,
hour,
minute,
second,
millisecond,
microsecond,
);
// DateTime setQuarter(quarter)
/// Get a [DateTime] representing start of Day of this [DateTime] in local time.
DateTime get startOfDay => clone.setHour(0, 0, 0, 0, 0);
/// Get a [DateTime] representing start of Hour of this [DateTime] in local time.
DateTime get startOfHour => clone.setMinute(0, 0, 0, 0);
/// Get a [DateTime] representing start of week (ISO week) of this [DateTime] in local time.
DateTime get startOfISOWeek => subDays(weekday - 1).startOfDay;
// DateTime startOfISOYear()
/// Get a [DateTime] representing start of minute of this [DateTime] in local time.
DateTime get startOfMinute => clone.setSecond(0, 0, 0);
/// Get a [DateTime] representing start of month of this [DateTime] in local time.
DateTime get startOfMonth => clone.setDay(1, 0, 0, 0, 0, 0);
// DateTime startOfQuarter()
/// Get a [DateTime] representing start of second of this [DateTime] in local time.
DateTime get startOfSecond => clone.setMillisecond(0, 0);
/// Get a [DateTime] representing start of today of [DateTime.today] in local time.
static DateTime get startOfToday => today.startOfDay;
/// Get a [DateTime] representing start of week of this [DateTime] in local time.
DateTime get startOfWeek =>
weekday == DateTime.sunday ? startOfDay : subDays(weekday).startOfDay;
/// Get a [DateTime] representing start of year of this [DateTime] in local time.
DateTime get startOfYear =>
clone.setMonth(DateTime.january, 1, 0, 0, 0, 0, 0);
/// Get the start of a local week-numbering year
DateTime get startOfWeekYear => startOfYear.startOfWeek;
/// Get the start of a local week-numbering year
DateTime get startOfISOWeekYear => startOfYear.startOfISOWeek;
/// Get the number of weeks in an ISO week-numbering year
int get getISOWeeksInYear {
return DateTime(year, 12, 28).getISOWeek;
}
/// Subtracts a [Duration] from this [DateTime]
DateTime sub(Duration duration) => add(Duration.zero - duration);
/// Subtracts an amout of hours from this [DateTime]
DateTime subHours(int amount) => addHours(-amount);
/// Subtracts an amout of days from this [DateTime]
DateTime subDays(int amount) => addDays(-amount);
/// Subtracts an amout of milliseconds from this [DateTime]
DateTime subMilliseconds(amount) => addMilliseconds(-amount);
/// Subtracts an amout of microseconds from this [DateTime]
DateTime subMicroseconds(amount) => addMicroseconds(-amount);
// DateTime subISOYears(amount)
/// Subtracts an amout of minutes from this [DateTime]
DateTime subMinutes(amount) => addMinutes(-amount);
/// Subtracts an amout of months from this [DateTime]
DateTime subMonths(amount) => addMonths(-amount);
// DateTime subQuarters(amount)
/// Subtracts an amout of seconds from this [DateTime]
DateTime subSeconds(amount) => addSeconds(-amount);
// DateTime subWeeks(amount)
/// Subtracts an amout of years from this [DateTime]
DateTime subYears(amount) => addYears(-amount);
// Check if two dates are [equals]
bool equals(DateTime other) => isAtSameMomentAs(other);
bool operator <(DateTime other) => isBefore(other);
bool operator <=(DateTime other) =>
isBefore(other) || isAtSameMomentAs(other);
bool operator >(DateTime other) => isAfter(other);
bool operator >=(DateTime other) => isAfter(other) || isAtSameMomentAs(other);
String toHumanString() => format('E MMM d y H:m:s');
/// The day after
/// The day after this [DateTime]
DateTime get nextDay => addDays(1);
/// The day previous this [DateTime]
DateTime get previousDay => addDays(-1);
/// The month after this [DateTime]
DateTime get nextMonth => clone.setMonth(month + 1);
/// The month previous this [DateTime]
DateTime get previousMonth => clone.setMonth(month - 1);
/// The year after this [DateTime]
DateTime get nextYear => clone.setYear(year + 1);
/// The year previous this [DateTime]
DateTime get previousYear => clone.setYear(year - 1);
/// The week after this [DateTime]
DateTime get nextWeek => addDays(7);
/// The week previous this [DateTime]
DateTime get previousWeek => subDays(7);
/// Number of seconds since epoch time
///