Skip to content

Commit

Permalink
adding endOfDay getter
Browse files Browse the repository at this point in the history
  • Loading branch information
FMorschel committed Sep 20, 2024
1 parent 97e34f5 commit d82cf27
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ extension DateTimeTimeExtension on DateTime {
bool get isWeekend => (weekday == DateTime.saturday) || (weekday == DateTime.sunday);

bool get isWorkday => !isWeekend;

/// Returns the last microsecond of the day (23:59:59.999999)
/// ```dart
/// final date = DateTime(2020, 1, 1);
/// date.endOfDay; // 2020-01-01 23:59:59.999999
/// final date = DateTime(2020, 1, 1, 12, 30, 15, 123, 456);
/// date.endOfDay; // 2020-01-01 23:59:59.999999
/// ```
DateTime get endOfDay {
const microsecond = Duration(microseconds: 1);
if (isUtc) return DateTime.utc(year, month, day + 1) - microsecond;
return DateTime(year, month, day + 1) - microsecond;
}
}

extension DurationTimeExtension on Duration {
Expand Down
13 changes: 12 additions & 1 deletion test/time_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,18 @@ void main() {
expect(it.isWorkday, isFalse);
});
});

group('EndOfDay', () {
test('returns the last microsecond of the day', () {
final it = DateTime(2022, DateTime.august, 1, 12, 30, 15, 10, 5);
final expected = DateTime(2022, DateTime.august, 1, 23, 59, 59, 999, 999);
expect(it.endOfDay, expected);
});
test('returns the last microsecond of the day for utc', () {
final it = DateTime.utc(2022, DateTime.august, 1, 12, 30, 15, 10, 5);
final expected = DateTime.utc(2022, DateTime.august, 1, 23, 59, 59, 999, 999);
expect(it.endOfDay, expected);
});
});
group('Shift', () {
group('empty parameters', () {
test('local', () {
Expand Down

0 comments on commit d82cf27

Please sign in to comment.