Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(date_ranger): exclude endDate instant in includes #180

Merged
merged 1 commit into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/src/model/date/date_ranger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ mixin DateRanger {
return false;
}

/// Whether [dateTime] is included in this [DateRanger].
/// Whether [dateTime] is included in this [DateRanger], including the
/// [startDate] but excluding the [endDate] instant: `[startDate, endDate)`.
///
/// Examples:
/// ```dart
Expand All @@ -55,9 +56,14 @@ mixin DateRanger {
bool includes(DateTime dateTime) {
if (hasInfiniteStart && hasInfiniteEnd) return true;
if (hasInfiniteStart) return endDate!.isAfter(dateTime);
if (hasInfiniteEnd) return startDate!.isBefore(dateTime);
if (hasInfiniteEnd) {
return startDate!.isAtSameMomentAs(dateTime) ||
startDate!.isBefore(dateTime);
}

return startDate!.isBefore(dateTime) && endDate!.isAfter(dateTime);
return (startDate!.isAtSameMomentAs(dateTime) ||
startDate!.isBefore(dateTime)) &&
endDate!.isAfter(dateTime);
}

/// Whether this [DateRanger] overlaps with another [DateRanger].
Expand Down
18 changes: 9 additions & 9 deletions test/model/date/date_ranger_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,21 @@ void main() {
test(
'should return true when the DateTime is included in this DateRanger',
() {
final dateTime = DateTime(2022, 12, 4, 11, 30);
final startDateTime = DateTime(2022, 12, 1, 9, 30);
final endDateTime = DateTime(2022, 12, 31, 21, 30);
final dateRange = DateRange(
startDate: DateTime(2022, 12, 1, 9, 30),
endDate: DateTime(2022, 12, 31, 21, 30),
startDate: startDateTime,
endDate: endDateTime,
);
expect(dateRange.includes(startDateTime), isTrue);
expect(dateRange.includes(endDateTime), isFalse);
final dateTime = DateTime(2022, 12, 4, 11, 30);
expect(dateRange.includes(dateTime), isTrue);

final startDateRange = DateRange(
startDate: DateTime(2022, 12, 1, 9, 30),
);
final startDateRange = DateRange(startDate: startDateTime);
expect(startDateRange.includes(dateTime), isTrue);

final endDateRange = DateRange(
endDate: DateTime(2022, 12, 31, 21, 30),
);
final endDateRange = DateRange(endDate: endDateTime);
expect(endDateRange.includes(dateTime), isTrue);
},
);
Expand Down