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

hotfix: make week handle timezone changes #1217

Merged
merged 1 commit into from
Apr 1, 2024
Merged
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
34 changes: 22 additions & 12 deletions uni/lib/model/utils/time/week.dart
Original file line number Diff line number Diff line change
@@ -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<Week> {
/// Creates a [Week] that starts the given [start] **date** (not datetime).
Expand All @@ -12,7 +22,7 @@ class Week implements Comparable<Week> {
microsecond: 0,
);

final end = startAtMidnight.add(const Duration(days: 7));
final end = startAtMidnight.addExact(const Duration(days: 7));

return Week._internal(startAtMidnight, end);
}
Expand All @@ -32,29 +42,29 @@ class Week implements Comparable<Week> {

/// 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),
);
}

/// Returns the [Week] that is [duration] after this week.
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),
);
}

Expand All @@ -69,8 +79,8 @@ class Week implements Comparable<Week> {
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)),
);
}

Expand All @@ -84,16 +94,16 @@ class Week implements Comparable<Week> {
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)),
);
}

/// Returns the [DateTime] at the start of the given [weekday].
///
/// 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<DateTime> get weekdays {
Expand Down
Loading