Struct RelativeTimeParser

Source
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>

Source

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))
);
Source

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))
);
Source

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))
);
Source

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)))
);

Trait Implementations§

Source§

impl<'a> Debug for RelativeTimeParser<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for RelativeTimeParser<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a> PartialEq for RelativeTimeParser<'a>

Source§

fn eq(&self, other: &RelativeTimeParser<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Eq for RelativeTimeParser<'a>

Source§

impl<'a> StructuralPartialEq for RelativeTimeParser<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for RelativeTimeParser<'a>

§

impl<'a> RefUnwindSafe for RelativeTimeParser<'a>

§

impl<'a> Send for RelativeTimeParser<'a>

§

impl<'a> Sync for RelativeTimeParser<'a>

§

impl<'a> Unpin for RelativeTimeParser<'a>

§

impl<'a> UnwindSafe for RelativeTimeParser<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.