From a14aa0c1de5d05d000cc3cc467b07d59fd22cea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Lima?= Date: Mon, 1 Apr 2024 23:27:53 +0100 Subject: [PATCH] hotfix: make week handle timezone changes --- uni/lib/model/utils/time/week.dart | 34 +++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/uni/lib/model/utils/time/week.dart b/uni/lib/model/utils/time/week.dart index 02f8efe26..5af8f83d2 100644 --- a/uni/lib/model/utils/time/week.dart +++ b/uni/lib/model/utils/time/week.dart @@ -1,3 +1,13 @@ +extension _ExactAddition on DateTime { + DateTime addExact(Duration duration) { + return copyWith(isUtc: true).add(duration).copyWith(isUtc: isUtc); + } + + DateTime subtractExact(Duration duration) { + return copyWith(isUtc: true).subtract(duration).copyWith(isUtc: isUtc); + } +} + /// A [Week] represents a period of 7 days. class Week implements Comparable { /// Creates a [Week] that starts the given [start] **date** (not datetime). @@ -12,7 +22,7 @@ class Week implements Comparable { microsecond: 0, ); - final end = startAtMidnight.add(const Duration(days: 7)); + final end = startAtMidnight.addExact(const Duration(days: 7)); return Week._internal(startAtMidnight, end); } @@ -32,20 +42,20 @@ class Week implements Comparable { /// Returns the [Week] that starts at the end of this [Week]. Week next() { - return Week._internal(end, end.add(const Duration(days: 7))); + return Week._internal(end, end.addExact(const Duration(days: 7))); } /// Returns the [Week] that ends at the start of this [Week]. Week previous() { - return Week._internal(start.subtract(const Duration(days: 7)), start); + return Week._internal(start.subtractExact(const Duration(days: 7)), start); } /// Returns the [Week] that is [duration] before this week. Week subtract(Duration duration) { final normalizedDuration = Duration(days: duration.inDays); return Week._internal( - start.subtract(normalizedDuration), - end.subtract(normalizedDuration), + start.subtractExact(normalizedDuration), + end.subtractExact(normalizedDuration), ); } @@ -53,8 +63,8 @@ class Week implements Comparable { Week add(Duration duration) { final normalizedDuration = Duration(days: duration.inDays); return Week._internal( - start.add(normalizedDuration), - end.add(normalizedDuration), + start.addExact(normalizedDuration), + end.addExact(normalizedDuration), ); } @@ -69,8 +79,8 @@ class Week implements Comparable { final offsetInDays = (weekday - start.weekday) % 7; return Week._internal( - start.add(Duration(days: offsetInDays)), - end.add(Duration(days: offsetInDays)), + start.addExact(Duration(days: offsetInDays)), + end.addExact(Duration(days: offsetInDays)), ); } @@ -84,8 +94,8 @@ class Week implements Comparable { final offsetInDays = (end.weekday - weekday) % 7; return Week._internal( - start.subtract(Duration(days: offsetInDays)), - end.subtract(Duration(days: offsetInDays)), + start.subtractExact(Duration(days: offsetInDays)), + end.subtractExact(Duration(days: offsetInDays)), ); } @@ -93,7 +103,7 @@ class Week implements Comparable { /// /// The values for [weekday] are according to [DateTime.weekday]. DateTime getWeekday(int weekday) { - return start.add(Duration(days: (weekday - start.weekday) % 7)); + return start.addExact(Duration(days: (weekday - start.weekday) % 7)); } Iterable get weekdays {