Expand description
Module implementing parsing of human-readable text into a DateRange.
§Examples
use timelog::date::{DateRange, RangeParser};
use timelog::Result;
let parser = RangeParser::default();
let range = parser.parse_from_str("last week")?;
print!("Dates: {} up to {}", range.start(), range.end());§Description
The RangeParser converts a description of relative or absolute dates into a
range of dates expressed as a half-open range including the start date, but excluding
the end date.
§Date Range Descriptors
A date range descriptor can be supplied to the parser either as a &str containing the descriptor or as an iterator returning separate words of the descriptor.
The parser can handle date range descriptors of the following forms:
A single date in one of the following forms:
- yyyy-mm-dd
- today
- yesterday
- a day of the week: sunday, monday, tuesday, wednesday, … etc.
All but the first of the above strings parse to dates relative to the base date of the parser. “Today” is the current date. “Yesterday” is the day before the current date. The weekdays are the last instance of that day before today.
This will result in a range spanning one day.
A pair of dates in the forms above.
This pair results in a range that covers the two supplied dates. If the dates are in order, the start is the first date and end is the day after the second date. If the dates are out of order, the parse will return an empty range.
A range description in one of the following forms:
- a month name: january, february, march, … etc.
- a short (3 char) month name: jan, feb, mar, … etc.
- a relative timeframe: (this|last) (week|month|year)
- the string “ytd”
If the parser is given a month name, the range will cover the whole month with that name before today.
If the parser is given “this” followed by “week”, “month”, or “year”, the resultant range covers:
- week: from the last Sunday to Saturday of this week,
- month: from the first day of the current month to last day of the month,
- year: from the January 1 of the current year to the last day of the year.
If the parser is given “last” followed by “week”, “month”, or “year”, the resultant range covers:
- week: from the two Sundays ago to last Saturday,
- month: from the first of the month before this one to the last day of that month,
- year: from January 1 of the year before the current one to December 31 of the same year.
If the parser receives the string “ytd”, the range will be from January 1 of the current year up to today.
Structs§
- Date
Parser - Struct implementing the parser to generate a
Datefrom a date description. - Range
Parser - Struct implementing the parser to generate a
DateRangefrom a date range description.