diff --git a/lib/utils/time_utils.dart b/lib/utils/time_utils.dart index 7ca2dd036..644a1d9b0 100644 --- a/lib/utils/time_utils.dart +++ b/lib/utils/time_utils.dart @@ -1,17 +1,3 @@ -bool isAtSameYearAs(DateTime d1, DateTime d2) => d1 != null && d2 != null && d1.year == d2.year; - -bool isAtSameMonthAs(DateTime d1, DateTime d2) => isAtSameYearAs(d1, d2) && d1.month == d2.month; - -bool isAtSameDayAs(DateTime d1, DateTime d2) => isAtSameMonthAs(d1, d2) && d1.day == d2.day; - -bool isToday(DateTime d) => isAtSameDayAs(d, DateTime.now()); - -bool isYesterday(DateTime d) => isAtSameDayAs(d, DateTime.now().subtract(const Duration(days: 1))); - -bool isThisMonth(DateTime d) => isAtSameMonthAs(d, DateTime.now()); - -bool isThisYear(DateTime d) => isAtSameYearAs(d, DateTime.now()); - String formatDuration(Duration d) { String twoDigits(int n) { if (n >= 10) return '$n'; @@ -24,3 +10,19 @@ String formatDuration(Duration d) { String twoDigitMinutes = twoDigits(d.inMinutes.remainder(Duration.minutesPerHour)); return '${d.inHours}:$twoDigitMinutes:$twoDigitSeconds'; } + +extension ExtraDateTime on DateTime { + bool isAtSameYearAs(DateTime other) => this != null && other != null && this.year == other.year; + + bool isAtSameMonthAs(DateTime other) => isAtSameYearAs(other) && this.month == other.month; + + bool isAtSameDayAs(DateTime other) => isAtSameMonthAs(other) && this.day == other.day; + + bool get isToday => isAtSameDayAs(DateTime.now()); + + bool get isYesterday => isAtSameDayAs(DateTime.now().subtract(const Duration(days: 1))); + + bool get isThisMonth => isAtSameMonthAs(DateTime.now()); + + bool get isThisYear => isAtSameYearAs(DateTime.now()); +} diff --git a/lib/widgets/album/sections.dart b/lib/widgets/album/sections.dart index bca2b8c71..a14ffb227 100644 --- a/lib/widgets/album/sections.dart +++ b/lib/widgets/album/sections.dart @@ -14,9 +14,9 @@ class DaySectionHeader extends StatelessWidget { static DateFormat ymd = DateFormat.yMMMMd(); static String _formatDate(DateTime date) { - if (isToday(date)) return 'Today'; - if (isYesterday(date)) return 'Yesterday'; - if (isThisYear(date)) return md.format(date); + if (date.isToday) return 'Today'; + if (date.isYesterday) return 'Yesterday'; + if (date.isThisYear) return md.format(date); return ymd.format(date); } @@ -37,8 +37,8 @@ class MonthSectionHeader extends StatelessWidget { static DateFormat ym = DateFormat.yMMMM(); static String _formatDate(DateTime date) { - if (isThisMonth(date)) return 'This month'; - if (isThisYear(date)) return m.format(date); + if (date.isThisMonth) return 'This month'; + if (date.isThisYear) return m.format(date); return ym.format(date); }