Skip to main content

DateTimeFormatter

Struct DateTimeFormatter 

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

Formats Unix timestamps with strftime-style patterns, localized and without allocation.

TokenOutput
%Y, %yYear, with and without century
%m, %d, %eMonth, day, space-padded day
%H, %I, %M, %SHour (24 and 12-hour), minute, second
%pLocalized AM / PM marker
%B, %OB, %b / %hLocalized month name, standalone, abbreviated
%A, %aLocalized weekday name, abbreviated
%u, %wWeekday number from Monday (1-7), from Sunday (0-6)
%x, %X, %cLocalized date, time, date and time
%zUTC offset (+0200)
%%%
use embedded_i18n::{date_time_locale, DateTimeFormatter};

let formatter = DateTimeFormatter::new(date_time_locale("fr")).with_utc_offset(2 * 3600);

let mut output = String::new();
formatter.write(&mut output, 1_791_884_800, "%A %d %B %Y, %H:%M %z")?;
assert_eq!(output, "mardi 13 octobre 2026, 11:46 +0200");

Implementations§

Source§

impl DateTimeFormatter

Source

pub const fn new(locale: DateTimeLocale) -> Self

Creates a formatter for the given names and patterns, in UTC.

Source

pub const fn with_locale(self, locale: DateTimeLocale) -> Self

Uses the names and patterns of another locale.

Source

pub const fn with_utc_offset(self, seconds: i32) -> Self

Shifts timestamps by a fixed UTC offset, in seconds, also printed by %z.

Source

pub const fn locale(&self) -> &DateTimeLocale

Returns the names and patterns used by this formatter.

Source

pub const fn utc_offset(&self) -> i32

Returns the UTC offset of this formatter, in seconds.

Source

pub fn write<W: Write>( &self, writer: &mut W, unix_timestamp: i64, pattern: &str, ) -> Result

Writes a Unix timestamp formatted with a pattern.

Source

pub const fn display<'a>( &'a self, unix_timestamp: i64, pattern: &'a str, ) -> DateTimeDisplay<'a>

Returns a value implementing fmt::Display, to format a timestamp with write! or format_args! without allocation.

use embedded_i18n::{DateTimeFormatter, date_time_locale};

let formatter = DateTimeFormatter::new(date_time_locale("en"));
let text = format!("Updated {}", formatter.display(0, "%Y-%m-%d"));
assert_eq!(text, "Updated 1970-01-01");
Source

pub fn format(&self, unix_timestamp: i64, pattern: &str) -> String

Formats a Unix timestamp into a String. Available with the alloc feature.

Trait Implementations§

Source§

impl Clone for DateTimeFormatter

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for DateTimeFormatter

Source§

impl Debug for DateTimeFormatter

Source§

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

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

impl Eq for DateTimeFormatter

Source§

impl PartialEq for DateTimeFormatter

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for DateTimeFormatter

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.