Skip to main content

Display

Struct Display 

Source
pub struct Display<'f> { /* private fields */ }
Expand description

A “lazy” implementation of std::fmt::Display for strftime.

Values of this type are created by the strftime methods on the various datetime types in this crate. For example, Zoned::strftime.

A Display captures the information needed from the datetime and waits to do the actual formatting when this type’s std::fmt::Display trait implementation is actually used.

§Errors and panics

This trait implementation configures formatting to use lenient mode. This avoids panics occurring when using APIs like Timestamp::strftime with unsupported or invalid formatting directives. Without lenient mode, whenever formatting would fail, this would surface as a panic when converting this display implementation to a string.

§Example

This example shows how to format a zoned datetime using Zoned::strftime:

use jiff::civil::date;

let zdt = date(2024, 7, 15).at(16, 24, 59, 0).in_tz("America/New_York")?;
let string = zdt.strftime("%a, %-d %b %Y %T %z").to_string();
assert_eq!(string, "Mon, 15 Jul 2024 16:24:59 -0400");

Or use it directly when writing to something:

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

let zdt = date(2024, 7, 15).at(16, 24, 59, 0).in_tz("America/New_York")?;

let string = format!("the date is: {}", zdt.strftime("%-m/%-d/%-Y"));
assert_eq!(string, "the date is: 7/15/2024");

§Example: errors are silently ignored

If the formatting string is malformed in some way, then it is silently ignored. For example, when using an invalid formatting directive:

use jiff::Zoned;

let zdt = Zoned::UNIX_EPOCH;
let string = zdt.strftime("%Y %").to_string();
assert_eq!(string, "1970 %");

If one wants to surface errors from a formatting string, use a lower level API:

use jiff::Zoned;

let zdt = Zoned::UNIX_EPOCH;
assert_eq!(
    jiff::fmt::strtime::format("%Y %", &zdt).unwrap_err().to_string(),
    "strftime formatting failed: invalid format string, \
     expected byte after `%`, but found end of format string",
);

Trait Implementations§

Source§

impl<'f> Debug for Display<'f>

Source§

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

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

impl<'f> Display for Display<'f>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'f> Freeze for Display<'f>

§

impl<'f> RefUnwindSafe for Display<'f>

§

impl<'f> Send for Display<'f>

§

impl<'f> Sync for Display<'f>

§

impl<'f> Unpin for Display<'f>

§

impl<'f> UnsafeUnpin for Display<'f>

§

impl<'f> UnwindSafe for Display<'f>

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.