pub struct StrPTimeFmt { /* private fields */ }Expand description
A pre-validated, reusable date/time format string.
- Format is validated once at construction (
newreturnsResult). - Format bytes are copied into an owned fixed-size buffer.
- Only ASCII formats are accepted.
§See also
Implementations§
Source§impl StrPTimeFmt
impl StrPTimeFmt
pub const MAX_FORMAT_LEN: usize = 256
Sourcepub fn new(fmt: &str) -> Result<Self, DtErr>
pub fn new(fmt: &str) -> Result<Self, DtErr>
Creates a new validated format.
- Validates syntax and supported directives.
- Requires the format to be valid ASCII and ≤ 256 bytes.
- Returns a
DtErron any failure.
§Errors
DtErrKind::InvalidLenif the format string is longer than 256 bytes.DtErrKind::InvalidInputif the format string is not valid ASCII.DtErrKind::TruncatedDirectiveif a%appears at the end of the format with no directive character following it.DtErrKind::UnexpectedEndif a%is followed only by flags, width digits, or colons, with no directive character after them.DtErrKind::ExpectedFractionalif a%.sequence is not followed by a directive character.DtErrKind::InvalidFractionalif a%.sequence is followed by a character other thanforN.DtErrKind::UnsupportedItemif the format contains%c,%r,%x,%X, or%Z.DtErrKind::UnknownItemif the format contains any other unrecognized%directive.
§Examples
use deep_time::{Dt, Lang, StrPTimeFmt};
let fmt = Dt::parse_fmt("%F %T").unwrap();
// parse a datetime
let dt = fmt.to_dt("2025-05-23 14:30:00", false, false, false).unwrap();
// change a datetimes format
let s = fmt.to_str("2000-01-01 12:00:00", "%d %m %Y %H:%M:%S", false, false, false, Lang::En).unwrap();
assert_eq!(s, "01 01 2000 12:00:00");Sourcepub fn to_dt(
&self,
s: &str,
inp_can_end_before_fmt: bool,
fmt_can_end_before_inp: bool,
allow_partial_date: bool,
) -> Result<Dt, DtErr>
pub fn to_dt( &self, s: &str, inp_can_end_before_fmt: bool, fmt_can_end_before_inp: bool, allow_partial_date: bool, ) -> Result<Dt, DtErr>
Parses a date/time string using this pre-validated format.
The four boolean flags control lenient parsing behavior — see
Dt::from_str for full documentation.
§Parameters
s: The input string to parse.inp_can_end_before_fmt: Allow input to end before format is fully consumed.fmt_can_end_before_inp: Allow format to end before input is fully consumed.allow_partial_date: Default missing month/day to1instead of erroring.
§Errors
DtErrKind::InvalidBytesifas_str()fails to convert the stored format back to&str.- Any error returned by
Parts::from_strfollowed byParts::to_dt(see the error documentation onDt::from_strfor the complete list).
§Examples
use deep_time::{Dt, StrPTimeFmt};
let fmt = Dt::parse_fmt("%F %T").unwrap();
let dt = fmt.to_dt("2025-05-23 14:30:00", false, false, false).unwrap();Sourcepub fn to_str(
&self,
s: &str,
output_fmt: &str,
inp_can_end_before_fmt: bool,
fmt_can_end_before_inp: bool,
allow_partial_date: bool,
lang: Lang,
) -> Result<String, DtErr>
pub fn to_str( &self, s: &str, output_fmt: &str, inp_can_end_before_fmt: bool, fmt_can_end_before_inp: bool, allow_partial_date: bool, lang: Lang, ) -> Result<String, DtErr>
Formats a Dt into a string using this pre-validated format and a given
output format.
Effectively parses a str with the contained format, then outputs a
String with a new given format.
Requires the alloc feature.
§Parameters
s: datetime inputstr.output_fmt: The new format to output the datetime as.- The remaining three flags are passed through to the internal
to_dtcall.
§Examples
use deep_time::{Dt, Lang, StrPTimeFmt};
let fmt = Dt::parse_fmt("%Y-%m-%dT%H:%M:%S").unwrap();
let s = fmt.to_str("2000-01-01T12:00:00", "%d %m %Y %H:%M:%S", false, false, false, Lang::En).unwrap();
assert_eq!(s, "01 01 2000 12:00:00");Sourcepub fn to_str_lite(
&self,
s: &str,
output_fmt: &str,
inp_can_end_before_fmt: bool,
fmt_can_end_before_inp: bool,
allow_partial_date: bool,
lang: Lang,
) -> Result<LiteStr<STRTIME_SIZE>, DtErr>
pub fn to_str_lite( &self, s: &str, output_fmt: &str, inp_can_end_before_fmt: bool, fmt_can_end_before_inp: bool, allow_partial_date: bool, lang: Lang, ) -> Result<LiteStr<STRTIME_SIZE>, DtErr>
Formats a Dt into a LiteStr using this pre-validated format and a given
output format.
Effectively parses a str with the contained format, then outputs a
LiteStr with a new given format.
§Parameters
s: datetime inputstr.output_fmt: The new format to output the datetime as.- The remaining three flags are passed through to the internal
to_dtcall.
§Examples
use deep_time::{Dt, Lang, StrPTimeFmt};
let fmt = Dt::parse_fmt("%Y-%m-%dT%H:%M:%S").unwrap();
let s = fmt.to_str_lite("2000-01-01T12:00:00", "%d %m %Y %H:%M:%S", false, false, false, Lang::En).unwrap();
assert_eq!(s.as_str(), "01 01 2000 12:00:00");Trait Implementations§
Source§impl Clone for StrPTimeFmt
impl Clone for StrPTimeFmt
Source§fn clone(&self) -> StrPTimeFmt
fn clone(&self) -> StrPTimeFmt
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreimpl Copy for StrPTimeFmt
Source§impl Debug for StrPTimeFmt
impl Debug for StrPTimeFmt
Auto Trait Implementations§
impl Freeze for StrPTimeFmt
impl RefUnwindSafe for StrPTimeFmt
impl Send for StrPTimeFmt
impl Sync for StrPTimeFmt
impl Unpin for StrPTimeFmt
impl UnsafeUnpin for StrPTimeFmt
impl UnwindSafe for StrPTimeFmt
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more