Parser

Struct Parser 

Source
pub struct Parser { /* private fields */ }
Expand description

Parser is responsible for doing the actual work of understanding a time string. The root level parse function is responsible for constructing a default Parser and triggering its behavior.

Implementations§

Source§

impl Parser

Source

pub fn new(info: ParserInfo) -> Self

Create a new Parser instance using the provided ParserInfo.

This method allows you to set up a parser to handle different names for days of the week, months, etc., enabling customization for different languages or extra values.

Source

pub fn parse( &self, timestr: &str, dayfirst: Option<bool>, yearfirst: Option<bool>, fuzzy: bool, fuzzy_with_tokens: bool, default: Option<&NaiveDateTime>, ignoretz: bool, tzinfos: &HashMap<String, i32>, ) -> Result<(NaiveDateTime, Option<FixedOffset>, Option<Vec<String>>), ParseError>

Main method to trigger parsing of a string using the previously-provided parser information. Returns a naive timestamp along with timezone and unused tokens if available.

dayfirst and yearfirst force parser behavior in the event of ambiguous dates. Consider the following scenarios where we parse the string ‘01.02.03’

  • dayfirst=Some(true), yearfirst=None: Results in February 2, 2003
  • dayfirst=None, yearfirst=Some(true): Results in February 3, 2001
  • dayfirst=Some(true), yearfirst=Some(true): Results in March 2, 2001

fuzzy enables fuzzy parsing mode, allowing the parser to skip tokens if they are unrecognized. However, the unused tokens will not be returned unless fuzzy_with_tokens is set as true.

default is the timestamp used to infer missing values, and is midnight of the current day by default. For example, when parsing the text ‘2003’, we will use the current month and day as a default value, leading to a result of ‘March 3, 2003’ if the function was run using a default of March 3rd.

ignoretz forces the parser to ignore timezone information even if it is recognized in the time string

tzinfos is a map of timezone names to the offset seconds. For example, the parser would ignore the ‘EST’ part of the string in ‘10 AM EST’ unless you added a tzinfos map of {"EST": "14400"}. Please note that timezone name support (i.e. “EST”, “BRST”) is not available by default at the moment, they must be added through tzinfos at the moment in order to be resolved.

Trait Implementations§

Source§

impl Default for Parser

Source§

fn default() -> Parser

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

Auto Trait Implementations§

§

impl Freeze for Parser

§

impl RefUnwindSafe for Parser

§

impl Send for Parser

§

impl Sync for Parser

§

impl Unpin for Parser

§

impl UnwindSafe for Parser

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.