pub struct RelativeTimeParser<'a> { /* private fields */ }
Expand description
The main gnu relative time parser
Note this parser can be created as const at compile time.
§Examples
use fundu_gnu::{Duration, RelativeTimeParser};
const PARSER: RelativeTimeParser = RelativeTimeParser::new();
let parser = &PARSER;
assert_eq!(parser.parse("1hour"), Ok(Duration::positive(60 * 60, 0)));
assert_eq!(parser.parse("minute"), Ok(Duration::positive(60, 0)));
assert_eq!(
parser.parse("2 hours"),
Ok(Duration::positive(2 * 60 * 60, 0))
);
assert_eq!(parser.parse("second"), Ok(Duration::positive(1, 0)));
assert_eq!(parser.parse("-3minutes"), Ok(Duration::negative(3 * 60, 0)));
assert_eq!(
parser.parse("3 mins ago"),
Ok(Duration::negative(3 * 60, 0))
);
assert_eq!(
parser.parse("999sec +1day"),
Ok(Duration::positive(86_400 + 999, 0))
);
assert_eq!(
parser.parse("55secs500week"),
Ok(Duration::positive(55 + 500 * 7 * 24 * 60 * 60, 0))
);
assert_eq!(
parser.parse("300mins20secs 5hour"),
Ok(Duration::positive(300 * 60 + 20 + 5 * 60 * 60, 0))
);
assert_eq!(
parser.parse("123456789"),
Ok(Duration::positive(123_456_789, 0))
);
assert_eq!(
parser.parse("42fortnight"),
Ok(Duration::positive(42 * 2 * 7 * 24 * 60 * 60, 0))
);
assert_eq!(
parser.parse("yesterday"),
Ok(Duration::negative(24 * 60 * 60, 0))
);
assert_eq!(parser.parse("now"), Ok(Duration::positive(0, 0)));
assert_eq!(
parser.parse("today -10seconds"),
Ok(Duration::negative(10, 0))
);
Implementations§
Source§impl<'a> RelativeTimeParser<'a>
impl<'a> RelativeTimeParser<'a>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Create a new RelativeTimeParser
§Examples
use fundu_gnu::{Duration, RelativeTimeParser};
let parser = RelativeTimeParser::new();
assert_eq!(
parser.parse("2hours"),
Ok(Duration::positive(2 * 60 * 60, 0))
);
assert_eq!(parser.parse("123"), Ok(Duration::positive(123, 0)));
assert_eq!(
parser.parse("3min +10sec"),
Ok(Duration::positive(3 * 60 + 10, 0))
);
Sourcepub fn parse(&self, source: &str) -> Result<Duration, ParseError>
pub fn parse(&self, source: &str) -> Result<Duration, ParseError>
Parse the source
string into a Duration
relative to the date and time of now
Any leading and trailing whitespace is ignored. The parser saturates at the maximum of
Duration::MAX
.
§Errors
Returns a ParseError
if an error during the parsing process occurred
§Examples
use fundu_gnu::{Duration, RelativeTimeParser};
let parser = RelativeTimeParser::new();
assert_eq!(
parser.parse("2hours"),
Ok(Duration::positive(2 * 60 * 60, 0))
);
assert_eq!(parser.parse("12 seconds"), Ok(Duration::positive(12, 0)));
assert_eq!(
parser.parse("123456789"),
Ok(Duration::positive(123_456_789, 0))
);
assert_eq!(
parser.parse("yesterday"),
Ok(Duration::negative(24 * 60 * 60, 0))
);
Sourcepub fn parse_with_date(
&self,
source: &str,
date: Option<DateTime>,
) -> Result<Duration, ParseError>
pub fn parse_with_date( &self, source: &str, date: Option<DateTime>, ) -> Result<Duration, ParseError>
Parse the source
string into a Duration
relative to the optionally given date
If the date
is None
, then the system time of now
is assumed. Time units of year
and
month
are parsed fuzzy since years and months are not all of equal length. Any leading and
trailing whitespace is ignored. The parser saturates at the maximum of Duration::MAX
.
§Errors
Returns a ParseError
if an error during the parsing process occurred or the calculation
of the calculation of the given date
plus the duration of the source
string overflows.
§Examples
use fundu_gnu::{DateTime, Duration, RelativeTimeParser};
let parser = RelativeTimeParser::new();
assert_eq!(
parser.parse_with_date("2hours", None),
Ok(Duration::positive(2 * 60 * 60, 0))
);
let date_time = DateTime::from_gregorian_date_time(1970, 2, 1, 0, 0, 0, 0);
assert_eq!(
parser.parse_with_date("+1month", Some(date_time)),
Ok(Duration::positive(28 * 86400, 0))
);
assert_eq!(
parser.parse_with_date("+1year", Some(date_time)),
Ok(Duration::positive(365 * 86400, 0))
);
// 1972 is a leap year
let date_time = DateTime::from_gregorian_date_time(1972, 2, 1, 0, 0, 0, 0);
assert_eq!(
parser.parse_with_date("+1month", Some(date_time)),
Ok(Duration::positive(29 * 86400, 0))
);
assert_eq!(
parser.parse_with_date("+1year", Some(date_time)),
Ok(Duration::positive(366 * 86400, 0))
);
Sourcepub fn parse_fuzzy(
&self,
source: &str,
) -> Result<(i64, i64, Duration), ParseError>
pub fn parse_fuzzy( &self, source: &str, ) -> Result<(i64, i64, Duration), ParseError>
Parse the source
string extracting year
and month
time units from the Duration
Unlike RelativeTimeParser::parse
and RelativeTimeParser::parse_with_date
this method
won’t interpret the parsed year
and month
time units but simply returns the values
parsed from the source
string.
The returned tuple (years
, months
, Duration
) contains in the first component the
amount parsed years
as i64
, in the second component the parsed months
as i64
and in
the last component the rest of the parsed time units accumulated as Duration
.
§Errors
Returns a ParseError
if an error during the parsing process occurred.
§Examples
use fundu_gnu::{Duration, RelativeTimeParser};
let parser = RelativeTimeParser::new();
assert_eq!(
parser.parse_fuzzy("2hours"),
Ok((0, 0, Duration::positive(2 * 60 * 60, 0)))
);
assert_eq!(
parser.parse_fuzzy("2hours +123month -10years"),
Ok((-10, 123, Duration::positive(2 * 60 * 60, 0)))
);