Skip to content

Commit

Permalink
Merge 3352cc5 into 5cda40d
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Feb 14, 2023
2 parents 5cda40d + 3352cc5 commit e93e4c0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
44 changes: 43 additions & 1 deletion lib/src/model/date/date_ranger.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:cabin_booking/utils/date_time_extension.dart';
import 'package:cabin_booking/utils/time_of_day_extension.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -66,7 +68,7 @@ mixin DateRanger {
endDate!.isAfter(dateTime);
}

/// Whether this [DateRanger] overlaps with another [DateRanger].
/// Whether this [DateRanger] overlaps with [other].
///
/// Example:
/// ```dart
Expand Down Expand Up @@ -95,6 +97,46 @@ mixin DateRanger {
endDate!.isAfter(other.startDate!);
}

/// Returns the overlapping [Duration] with [other]. The operation is
/// commutative, returning the same result in both directions.
///
/// Example:
/// ```dart
/// final dateRange1 = DateRange(
/// startDate: DateTime(2022, 12, 4, 9, 15),
/// endDate: DateTime(2022, 12, 4, 12, 15),
/// );
/// final dateRange2 = DateRange(
/// startDate: DateTime(2022, 12, 4, 10, 15),
/// endDate: DateTime(2022, 12, 4, 11, 15),
/// );
///
/// final duration1 = dateRange1.overlappingDurationWith(dateRange2);
/// assert(duration1 == const Duration(hours: 1));
///
/// final duration2 = dateRange2.overlappingDurationWith(dateRange1);
/// assert(duration1 == duration2);
/// ```
Duration overlappingDurationWith(DateRanger other) {
if (hasInfiniteStart && hasInfiniteEnd ||
other.hasInfiniteStart && other.hasInfiniteEnd) return Duration.zero;
if (hasInfiniteStart && !other.hasInfiniteStart) {
return endDate!.difference(other.startDate!);
}
if (hasInfiniteEnd && !other.hasInfiniteEnd) {
return startDate!.difference(other.endDate!);
}
if (startDate!.isBefore(other.endDate!) &&
endDate!.isAfter(other.startDate!)) {
return Duration(
milliseconds:
min<int>(duration.inMilliseconds, other.duration.inMilliseconds),
);
}

return Duration.zero;
}

/// Whether this [DateRanger] has [startDate] and [endDate] values.
///
/// Examples:
Expand Down
24 changes: 24 additions & 0 deletions test/model/date/date_ranger_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,30 @@ void main() {
);
});

group('.overlappingDurationWith()', () {
test(
'should return the overlapping duration with another DateRanger',
() {
final dateRange1 = DateRange(
startDate: DateTime(2022, 12, 4, 9, 15),
endDate: DateTime(2022, 12, 4, 12, 15),
);
final dateRange2 = DateRange(
startDate: DateTime(2022, 12, 4, 10, 15),
endDate: DateTime(2022, 12, 4, 11, 15),
);
expect(
dateRange1.overlappingDurationWith(dateRange2),
const Duration(hours: 1),
);
expect(
dateRange1.overlappingDurationWith(dateRange2),
dateRange2.overlappingDurationWith(dateRange1),
);
},
);
});

group('.isFinite', () {
test('should return true when this DateRanger is finite', () {
final dateRange = DateRange(
Expand Down

0 comments on commit e93e4c0

Please sign in to comment.