Skip to content

Commit

Permalink
refactor(time_of_day_extension): improve comparison implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Sep 18, 2021
1 parent 617c4a6 commit cfe1aba
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/utils/time_of_day_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ extension TimeOfDayExtension on TimeOfDay {

static int compare(TimeOfDay a, TimeOfDay b) => a.compareTo(b);

int get inMinutes => hour * TimeOfDay.minutesPerHour + minute;

int compareTo(TimeOfDay? other) {
if (other == null) return 1;
if (other == this) return 0;
return inMinutes.compareTo(other.inMinutes);
}

Duration durationBetween(TimeOfDay other) => Duration(
hours: other.hour - hour,
minutes: other.minute - minute,
);

String format24Hour() => '${hour.padLeft2}:${minute.padLeft2}';

int compareTo(TimeOfDay other) =>
(hour - other.hour) * 60 + minute - other.minute;
}

0 comments on commit cfe1aba

Please sign in to comment.