devela::_dep::jiff::fmt::strtime

Function parse

pub fn parse(
    format: impl AsRef<[u8]>,
    input: impl AsRef<[u8]>,
) -> Result<BrokenDownTime, Error> 
Available on crate features dep_jiff and alloc only.
Expand description

Parse the given input according to the given format string.

See the module documentation for details on what’s supported.

This routine is the same as BrokenDownTime::parse, but may be more convenient to call.

§Errors

This returns an error when parsing failed. This might happen because the format string itself was invalid, or because the input didn’t match the format string.

§Example

This example shows how to parse something resembling a RFC 2822 datetime:

use jiff::{civil::date, fmt::strtime, tz};

let zdt = strtime::parse(
    "%a, %d %b %Y %T %z",
    "Mon, 15 Jul 2024 16:24:59 -0400",
)?.to_zoned()?;

let tz = tz::offset(-4).to_time_zone();
assert_eq!(zdt, date(2024, 7, 15).at(16, 24, 59, 0).to_zoned(tz)?);

Of course, one should prefer using the fmt::rfc2822 module, which contains a dedicated RFC 2822 parser. For example, the above format string does not part all valid RFC 2822 datetimes, since, e.g., the leading weekday is optional and so are the seconds in the time, but strptime-like APIs have no way of expressing such requirements.

§Example: parse RFC 3339 timestamp with fractional seconds

use jiff::{civil::date, fmt::strtime};

let zdt = strtime::parse(
    "%Y-%m-%dT%H:%M:%S%.f%:z",
    "2024-07-15T16:24:59.123456789-04:00",
)?.to_zoned()?;
assert_eq!(
    zdt,
    date(2024, 7, 15).at(16, 24, 59, 123_456_789).intz("America/New_York")?,
);