Skip to content

Commit d82cf27

Browse files
committed
adding endOfDay getter
1 parent 97e34f5 commit d82cf27

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/src/extensions.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,19 @@ extension DateTimeTimeExtension on DateTime {
296296
bool get isWeekend => (weekday == DateTime.saturday) || (weekday == DateTime.sunday);
297297

298298
bool get isWorkday => !isWeekend;
299+
300+
/// Returns the last microsecond of the day (23:59:59.999999)
301+
/// ```dart
302+
/// final date = DateTime(2020, 1, 1);
303+
/// date.endOfDay; // 2020-01-01 23:59:59.999999
304+
/// final date = DateTime(2020, 1, 1, 12, 30, 15, 123, 456);
305+
/// date.endOfDay; // 2020-01-01 23:59:59.999999
306+
/// ```
307+
DateTime get endOfDay {
308+
const microsecond = Duration(microseconds: 1);
309+
if (isUtc) return DateTime.utc(year, month, day + 1) - microsecond;
310+
return DateTime(year, month, day + 1) - microsecond;
311+
}
299312
}
300313

301314
extension DurationTimeExtension on Duration {

test/time_test.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,18 @@ void main() {
716716
expect(it.isWorkday, isFalse);
717717
});
718718
});
719-
719+
group('EndOfDay', () {
720+
test('returns the last microsecond of the day', () {
721+
final it = DateTime(2022, DateTime.august, 1, 12, 30, 15, 10, 5);
722+
final expected = DateTime(2022, DateTime.august, 1, 23, 59, 59, 999, 999);
723+
expect(it.endOfDay, expected);
724+
});
725+
test('returns the last microsecond of the day for utc', () {
726+
final it = DateTime.utc(2022, DateTime.august, 1, 12, 30, 15, 10, 5);
727+
final expected = DateTime.utc(2022, DateTime.august, 1, 23, 59, 59, 999, 999);
728+
expect(it.endOfDay, expected);
729+
});
730+
});
720731
group('Shift', () {
721732
group('empty parameters', () {
722733
test('local', () {

0 commit comments

Comments
 (0)