TimeZoneParser

Struct TimeZoneParser 

Source
pub struct TimeZoneParser<'a, T: EncodingType> { /* private fields */ }
Expand description

A parser for time zone offset and IANA identifier strings.

Enabled with the timezone Cargo feature.

Implementations§

Source§

impl<'a> TimeZoneParser<'a, Utf8>

Source

pub fn from_str(source: &'a str) -> Self

Creates a new TimeZoneParser from a source &str.

Source

pub fn from_utf8(source: &'a [u8]) -> Self

Creates a new TimeZoneParser from a slice of utf-8 bytes.

Source§

impl<'a> TimeZoneParser<'a, Utf16>

Source

pub fn from_utf16(source: &'a [u16]) -> Self

Creates a new TimeZoneParser from a slice of utf-16 bytes.

Source§

impl<'a, T: EncodingType> TimeZoneParser<'a, T>

Source

pub fn new(source: &'a [T::CodeUnit]) -> Self

Creates a new TimeZoneParser for the provided encoding.

Source

pub fn parse_identifier(&mut self) -> ParserResult<TimeZoneRecord<'a, T>>

Parse a time zone identifier that can be either an IANA identifer name or minute precision offset.

§IANA identifier example
use ixdtf::{parsers::TimeZoneParser, records::TimeZoneRecord};

let identifier = "Europe/London";
let record = TimeZoneParser::from_str(identifier)
    .parse_identifier()
    .unwrap();
assert_eq!(record, TimeZoneRecord::Name(identifier.as_bytes()))
§Minute precision offset example
use ixdtf::{
    parsers::TimeZoneParser,
    records::{MinutePrecisionOffset, Sign, TimeZoneRecord},
};

let identifier = "+00:00";
let offset = match TimeZoneParser::from_str(identifier).parse_identifier() {
    Ok(TimeZoneRecord::Offset(o)) => o,
    _ => unreachable!(),
};

assert_eq!(offset.sign, Sign::Positive);
assert_eq!(offset.hour, 0);
assert_eq!(offset.minute, 0);
§Errors

It is an error to provide a full precision offset as a time zone identifier.

NOTE: To parse either a full or minute precision, use Self::parse_offset.

use ixdtf::{parsers::TimeZoneParser, ParseError};

let identifier = "+00:00:00";
let err = TimeZoneParser::from_str(identifier)
    .parse_identifier()
    .unwrap_err();
assert_eq!(err, ParseError::InvalidMinutePrecisionOffset);

let identifier = "+00:00.1";
let err = TimeZoneParser::from_str(identifier)
    .parse_identifier()
    .unwrap_err();
assert_eq!(err, ParseError::InvalidEnd);
Source

pub fn parse_offset(&mut self) -> ParserResult<UtcOffsetRecord>

Parse a UTC offset from the provided source.

This method can parse both a minute precision and full precision offset.

§Minute precision offset example
use ixdtf::{parsers::TimeZoneParser, records::Sign};

let offset_src = "-05:00";
let parse_result =
    TimeZoneParser::from_str(offset_src).parse_offset().unwrap();
assert_eq!(parse_result.sign(), Sign::Negative);
assert_eq!(parse_result.hour(), 5);
assert_eq!(parse_result.minute(), 0);
assert_eq!(parse_result.second(), None);
assert_eq!(parse_result.fraction(), None);
§Full precision offset example
use ixdtf::{parsers::TimeZoneParser, records::Sign};

let offset_src = "-05:00:30.123456789";
let parse_result =
    TimeZoneParser::from_str(offset_src).parse_offset().unwrap();
assert_eq!(parse_result.sign(), Sign::Negative);
assert_eq!(parse_result.hour(), 5);
assert_eq!(parse_result.minute(), 0);
assert_eq!(parse_result.second(), Some(30));
let fraction = parse_result.fraction().unwrap();
assert_eq!(fraction.to_nanoseconds(), Some(123456789));
Source

pub fn parse_iana_identifier(&mut self) -> ParserResult<&'a [T::CodeUnit]>

Parse an IANA identifier name.

use ixdtf::{parsers::TimeZoneParser, records::Sign};

let iana_identifier = "America/Chicago";
let parse_result = TimeZoneParser::from_str(iana_identifier)
    .parse_iana_identifier()
    .unwrap();
assert_eq!(parse_result, iana_identifier.as_bytes());

let iana_identifier = "Europe/Berlin";
let parse_result = TimeZoneParser::from_str(iana_identifier)
    .parse_iana_identifier()
    .unwrap();
assert_eq!(parse_result, iana_identifier.as_bytes());

Trait Implementations§

Source§

impl<'a, T: Debug + EncodingType> Debug for TimeZoneParser<'a, T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for TimeZoneParser<'a, T>

§

impl<'a, T> RefUnwindSafe for TimeZoneParser<'a, T>
where <T as EncodingType>::CodeUnit: RefUnwindSafe,

§

impl<'a, T> Send for TimeZoneParser<'a, T>
where <T as EncodingType>::CodeUnit: Sync,

§

impl<'a, T> Sync for TimeZoneParser<'a, T>
where <T as EncodingType>::CodeUnit: Sync,

§

impl<'a, T> Unpin for TimeZoneParser<'a, T>

§

impl<'a, T> UnwindSafe for TimeZoneParser<'a, T>
where <T as EncodingType>::CodeUnit: RefUnwindSafe,

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.