pub struct DateTimeFormatter { /* private fields */ }Expand description
Formats Unix timestamps with strftime-style patterns, localized and
without allocation.
| Token | Output |
|---|---|
%Y, %y | Year, with and without century |
%m, %d, %e | Month, day, space-padded day |
%H, %I, %M, %S | Hour (24 and 12-hour), minute, second |
%p | Localized AM / PM marker |
%B, %OB, %b / %h | Localized month name, standalone, abbreviated |
%A, %a | Localized weekday name, abbreviated |
%u, %w | Weekday number from Monday (1-7), from Sunday (0-6) |
%x, %X, %c | Localized date, time, date and time |
%z | UTC 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
impl DateTimeFormatter
Sourcepub const fn new(locale: DateTimeLocale) -> Self
pub const fn new(locale: DateTimeLocale) -> Self
Creates a formatter for the given names and patterns, in UTC.
Sourcepub const fn with_locale(self, locale: DateTimeLocale) -> Self
pub const fn with_locale(self, locale: DateTimeLocale) -> Self
Uses the names and patterns of another locale.
Sourcepub const fn with_utc_offset(self, seconds: i32) -> Self
pub const fn with_utc_offset(self, seconds: i32) -> Self
Shifts timestamps by a fixed UTC offset, in seconds, also printed by %z.
Sourcepub const fn locale(&self) -> &DateTimeLocale
pub const fn locale(&self) -> &DateTimeLocale
Returns the names and patterns used by this formatter.
Sourcepub const fn utc_offset(&self) -> i32
pub const fn utc_offset(&self) -> i32
Returns the UTC offset of this formatter, in seconds.
Sourcepub fn write<W: Write>(
&self,
writer: &mut W,
unix_timestamp: i64,
pattern: &str,
) -> Result
pub fn write<W: Write>( &self, writer: &mut W, unix_timestamp: i64, pattern: &str, ) -> Result
Writes a Unix timestamp formatted with a pattern.
Sourcepub const fn display<'a>(
&'a self,
unix_timestamp: i64,
pattern: &'a str,
) -> DateTimeDisplay<'a>
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");Trait Implementations§
Source§impl Clone for DateTimeFormatter
impl Clone for DateTimeFormatter
impl Copy for DateTimeFormatter
Source§impl Debug for DateTimeFormatter
impl Debug for DateTimeFormatter
impl Eq for DateTimeFormatter
Source§impl PartialEq for DateTimeFormatter
impl PartialEq for DateTimeFormatter
impl StructuralPartialEq for DateTimeFormatter
Auto Trait Implementations§
impl Forget for DateTimeFormatter
impl Freeze for DateTimeFormatter
impl Move for DateTimeFormatter
impl RefUnwindSafe for DateTimeFormatter
impl Send for DateTimeFormatter
impl Sync for DateTimeFormatter
impl Unpin for DateTimeFormatter
impl UnsafeUnpin for DateTimeFormatterwhere
&'static str: UnsafeUnpin,
impl UnwindSafe for DateTimeFormatterwhere
&'static str: UnwindSafe,
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