-
Notifications
You must be signed in to change notification settings - Fork 102
Add and use a TimeRange class to represent before/after timestamp.
#4719
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
Merged
auto-submit
merged 1 commit into
flutter:main
from
matanlurey:firestore-filter-TimeRange
May 22, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // Copyright 2019 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
|
|
||
| /// A definition of a time range, which can be specific or indefinite. | ||
| @immutable | ||
| sealed class TimeRange { | ||
| const TimeRange(); | ||
|
|
||
| /// Matches any time. | ||
| static const TimeRange indefinite = IndefiniteTimeRange(); | ||
|
|
||
| /// Creates a range matching time between [start] and [end]. | ||
| /// | ||
| /// If [exclusive] is set, the specific moments described in [start] and [end] | ||
| /// are excluded. | ||
| /// | ||
| /// [end] must be after [start]. | ||
| factory TimeRange.between( | ||
| DateTime start, // | ||
| DateTime end, { | ||
| bool exclusive, | ||
| }) = SpecificTimeRange.between; | ||
|
|
||
| /// Creates a range matching time before [end]. | ||
| /// | ||
| /// If [exclusive] is set, the specific moment described in [end] is excluded. | ||
| factory TimeRange.before( | ||
| DateTime end, { // | ||
| bool exclusive, | ||
| }) = SpecificTimeRange.before; | ||
|
|
||
| /// Creates a range matching time after [start]. | ||
| /// | ||
| /// If [exclusive] is set, the specific moment described in [start] is | ||
| /// excluded. | ||
| factory TimeRange.after( | ||
| DateTime start, { // | ||
| bool exclusive, | ||
| }) = SpecificTimeRange.after; | ||
|
|
||
| @override | ||
| @nonVirtual | ||
| bool operator ==(Object other) { | ||
| return other is TimeRange && start == other.start && end == other.end; | ||
| } | ||
|
|
||
| @override | ||
| @nonVirtual | ||
| int get hashCode => Object.hash(start, end); | ||
|
|
||
| /// A starting point in time. | ||
| /// | ||
| /// If `null`, the time range is infinite in the past. | ||
| DateTime? get start; | ||
|
|
||
| /// An ending point in time. | ||
| /// | ||
| /// If `null`, the time range is infinite in the future. | ||
| DateTime? get end; | ||
|
|
||
| /// Returns whether [time] is within `this` range. | ||
| bool contains(DateTime time); | ||
|
|
||
| @override | ||
| String toString() { | ||
| if (start == null && end == null) { | ||
| return 'TimeRange.indefinite'; | ||
| } | ||
| if (start == null) { | ||
| return 'TimeRange.before($end)'; | ||
| } | ||
| if (end == null) { | ||
| return 'TimeRange.after($start)'; | ||
| } | ||
| return 'TimeRange.between($start, $end)'; | ||
| } | ||
| } | ||
|
|
||
| /// No specific start or end time. | ||
| final class IndefiniteTimeRange extends TimeRange { | ||
| @literal | ||
| const IndefiniteTimeRange(); | ||
|
|
||
| @override | ||
| Null get start => null; | ||
|
|
||
| @override | ||
| Null get end => null; | ||
|
|
||
| @override | ||
| bool contains(DateTime time) => true; | ||
| } | ||
|
|
||
| /// A specific time range, which includes at _least_ [start] or [end], or both. | ||
| final class SpecificTimeRange extends TimeRange { | ||
| /// Creates a time range with a specific [end] time. | ||
| /// | ||
| /// If [exclusive] is set, the specific moment described in [end] is excluded. | ||
| SpecificTimeRange.before( | ||
| DateTime this.end, { // | ||
| this.exclusive = false, | ||
| }) : start = null; | ||
|
|
||
| /// Creates a time range with a specific [start] time. | ||
| /// | ||
| /// If [exclusive] is set, the specific moment described in [start] is | ||
| /// excluded. | ||
| SpecificTimeRange.after( | ||
| DateTime this.start, { // | ||
| this.exclusive = false, | ||
| }) : end = null; | ||
|
|
||
| /// Creates a time range with a specific [start] and [end] time. | ||
| /// | ||
| /// If [exclusive] is set, the specific moment described in [start] and [end] | ||
| /// are excluded. | ||
| /// | ||
| /// [start] must be before [end]. | ||
| SpecificTimeRange.between( | ||
| DateTime this.start, // | ||
| DateTime this.end, { | ||
| this.exclusive = false, | ||
| }) { | ||
| if (start!.isAfter(end!)) { | ||
| throw ArgumentError.value( | ||
| start, | ||
| 'start', | ||
| 'Start time must be before end time.', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// Whether [start], or [end], if specified, should exclude that instant. | ||
| final bool exclusive; | ||
|
|
||
| /// Whether [start], or [end], if specified, should include that instant. | ||
| bool get inclusive => !exclusive; | ||
|
|
||
| @override | ||
| final DateTime? start; | ||
|
|
||
| @override | ||
| final DateTime? end; | ||
|
|
||
| @override | ||
| bool contains(DateTime time) { | ||
| if (start case final start? when start.isAfter(time)) { | ||
| return false; | ||
| } | ||
| if (end case final end? when end.isBefore(time)) { | ||
| return false; | ||
| } | ||
| if (exclusive) { | ||
| return time != start && time != end; | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this either:
(start, end)or[start, end]and never
(start, end]or[start, end)