Line data Source code
1 : import 'package:flutter/material.dart';
2 :
3 : ///
4 : ///
5 : ///
6 : extension FollyDateTimeExtension on DateTime {
7 : static const Duration oneDay = Duration(days: 1);
8 : static const Duration week = Duration(days: 7);
9 :
10 : ///
11 : ///
12 : ///
13 2 : TimeOfDay get time => TimeOfDay.fromDateTime(this);
14 :
15 : ///
16 : ///
17 : ///
18 1 : DateTime mergeStartSeconds({
19 : int second = 0,
20 : int millisecond = 0,
21 : int microsecond = 0,
22 : }) =>
23 1 : copyWith(
24 : second: second,
25 : millisecond: millisecond,
26 : microsecond: microsecond,
27 : );
28 :
29 : ///
30 : ///
31 : ///
32 12 : DateTime mergeStart({
33 : TimeOfDay time = const TimeOfDay(hour: 0, minute: 0),
34 : int second = 0,
35 : int millisecond = 0,
36 : int microsecond = 0,
37 : }) =>
38 12 : copyWith(
39 12 : hour: time.hour,
40 12 : minute: time.minute,
41 : second: second,
42 : millisecond: millisecond,
43 : microsecond: microsecond,
44 : );
45 :
46 : ///
47 : ///
48 : ///
49 10 : DateTime get startOfDay => mergeStart();
50 :
51 : ///
52 : ///
53 : ///
54 1 : DateTime mergeEndSeconds({
55 : int second = 59,
56 : int millisecond = 999,
57 : int microsecond = 0,
58 : }) =>
59 1 : copyWith(
60 : second: second,
61 : millisecond: millisecond,
62 : microsecond: microsecond,
63 : );
64 :
65 : ///
66 : ///
67 : ///
68 6 : DateTime mergeEnd({
69 : TimeOfDay time = const TimeOfDay(hour: 23, minute: 59),
70 : int second = 59,
71 : int millisecond = 999,
72 : int microsecond = 0,
73 : }) =>
74 6 : copyWith(
75 6 : hour: time.hour,
76 6 : minute: time.minute,
77 : second: second,
78 : millisecond: millisecond,
79 : microsecond: microsecond,
80 : );
81 :
82 : ///
83 : ///
84 : ///
85 10 : DateTime get endOfDay => mergeEnd();
86 :
87 : ///
88 : ///
89 : ///
90 5 : int get daysInMonth {
91 10 : if (month == DateTime.february) {
92 45 : return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)
93 : ? 29
94 : : 28;
95 : }
96 10 : List<int> days = <int>[31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
97 :
98 15 : return days[month - 1];
99 : }
100 :
101 : ///
102 : ///
103 : ///
104 9 : DateTime get monthFirstDay => copyWith(day: 1).startOfDay;
105 :
106 : ///
107 : ///
108 : ///
109 12 : DateTime get monthLastDay => copyWith(day: daysInMonth).endOfDay;
110 :
111 : ///
112 : ///
113 : ///
114 3 : DateTime get prevMonthFirstDay => prevMonthLastDay.monthFirstDay;
115 :
116 : ///
117 : ///
118 : ///
119 8 : DateTime get prevMonthLastDay => monthFirstDay.subtract(oneDay).endOfDay;
120 :
121 : ///
122 : ///
123 : ///
124 8 : DateTime get nextMonthFirstDay => monthLastDay.add(oneDay).startOfDay;
125 :
126 : ///
127 : ///
128 : ///
129 3 : DateTime get nextMonthLastDay => nextMonthFirstDay.monthLastDay;
130 :
131 : ///
132 : ///
133 : ///
134 6 : DateTime weekFirstDay([int firstDay = DateTime.sunday]) => subtract(
135 3 : Duration(
136 18 : days: weekday - firstDay + (weekday - firstDay < 0 ? 7 : 0),
137 : ),
138 3 : ).mergeStart();
139 :
140 : ///
141 : ///
142 : ///
143 6 : DateTime weekLastDay([int lastDay = DateTime.saturday]) => add(
144 3 : Duration(
145 18 : days: lastDay - weekday + (lastDay - weekday < 0 ? 7 : 0),
146 : ),
147 3 : ).mergeStart();
148 :
149 : ///
150 : ///
151 : ///
152 1 : DateTime prevWeekFirstDay([int firstDay = DateTime.sunday]) =>
153 2 : weekFirstDay(firstDay).subtract(week);
154 :
155 : ///
156 : ///
157 : ///
158 1 : DateTime prevWeekLastDay([int lastDay = DateTime.saturday]) =>
159 2 : weekLastDay(lastDay).subtract(week);
160 :
161 : ///
162 : ///
163 : ///
164 1 : DateTime nextWeekFirstDay([int firstDay = DateTime.sunday]) =>
165 2 : weekFirstDay(firstDay).add(week);
166 :
167 : ///
168 : ///
169 : ///
170 1 : DateTime nextWeekLastDay([int lastDay = DateTime.saturday]) =>
171 2 : weekLastDay(lastDay).add(week);
172 :
173 : ///
174 : ///
175 : ///
176 0 : DateTime get yearFirstDay => copyWith(month: 1, day: 1).startOfDay;
177 :
178 : ///
179 : ///
180 : ///
181 0 : DateTime get yearLastDay => copyWith(month: 12, day: 31).endOfDay;
182 : }
|