parse static method
Parses a string representation of a date range.
The input
string should be in one of the following formats:
- [date,date] (inclusive start and end)
- [date,date) (inclusive start, exclusive end)
- (date,date] (exclusive start, inclusive end)
- (date,date) (exclusive start and end)
- (-infinity,infinity) (open range)
- (-infinity,date] or (-infinity,date) (open start, inclusive/exclusive end)
- [date,infinity) or (date,infinity) (inclusive/exclusive start, open end)
The startInclusive
and endInclusive
parameters can be used to override the inclusivity of the start and end dates.
If not provided, the inclusivity is determined from the input string.
Example:
DateRange.parse("[2023-01-01,2023-01-31]"); // Inclusive start and end
DateRange.parse("[2023-01-01,2023-01-31)", startInclusive: false); // Exclusive start, exclusive end
DateRange.parse("(-infinity,2023-01-31]"); // Open start, inclusive end
DateRange.parse("[2023-01-01,infinity)"); // Inclusive start, open end
DateRange.parse("(-infinity,infinity)"); // Open range
Returns a DateRange object representing the parsed date range, or null if the input string is invalid.
Implementation
static DateRange? parse(String? input, {bool? startInclusive, bool? endInclusive}) {
final range = DiscreteRange._parse<DateTime, DateTime>(input,
regexInfInf: regexInfInf,
regexInfVal: regexInfVal,
regexValInf: regexValInf,
regexValVal: regexValVal,
parser: (val) => DateTime.parse("${val}T00:00:00Z"),
ctor: () => DateRange._(),
startInclusive: startInclusive,
endInclusive: endInclusive) as DateRange?;
return range;
}