hifitime::efmt::format

Struct Format

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

Format allows formatting an Epoch with some custom arrangement of the Epoch items. This provides almost all of the options from the 1989 C standard.

Construct a format with Format::from_str (no allocation needed) where the string contains a succession of tokens (each starting with %) and up to two separators between tokens.

Then this format can then be provided to the Formatter for formatting. This is also no-std.

§Supported tokens

Any token may be followed by ? to make it optional. For integer values, an optional token will only be printed if its value is non-zero. For time scales, only non-UTC time scale will be printed.

§C89 standard tokens

TokenExplanationExampleNotes
%YProleptic Gregorian year, zero-padded to 4 digits2022(1)
%yProleptic Gregorian year after 2000, on two digits23N/A
%mMonth number, zero-padded to 2 digits03 for MarchN/A
%bMonth name in short formMar for MarchN/A
%BMonth name in long formMarchN/A
%dDay number, zero-padded to 2 digits07 for the 7th day of the monthN/A
%jDay of year, zero-padded to 3 digits059 for 29 February 2000N/A
%AWeekday name in long formMondayN/A
%aWeekday name in short formMon for MondayN/A
%HHour number, zero-padded to 2 digits02 for the 2nd hour of the dayN/A
%MMinute number, zero-padded to 2 digits39 for the 39th minutes of the hourN/A
%SSeconds, zero-padded to 2 digits27 for the 27th second of the minuteN/A
%fSub-seconds, zero-padded to 9 digits000000007 for the 7th nanosecond past the second(2)
%wWeekday in decimal form with C89 standard01 for Dynamical barycentric time(3)
%zOffset timezone if the formatter is provided with an epoch.+15:00 For GMT +15 hours and zero minutesN/A
  • (1): Hifitime supports years from -34668 to 34668. If your epoch is larger than +/- 9999 years, the formatting of the years will show all five digits of the year.
  • (2): Hifitime supports exactly nanosecond precision, and this is not lost when formatting.

§Hifitime specific tokens

These are chosen to not conflict with strptime/strfime of the C89 standard.

TokenExplanationExampleNotes
%TTime scale used to represent this dateTDB for Dynamical barycentric time(3)
%JFull day of year as a double59.62325231481524 for 29 February 2000 14:57:29 UTCN/A
  • (3): Hifitime supports many time scales and these should not be lost when formatting. This is a novelty compared to other time management libraries as most do not have any concept of time scales.

§Example

use hifitime::prelude::*;
use hifitime::efmt;
use hifitime::efmt::consts;
use core::str::FromStr;

let bday = Epoch::from_gregorian_utc(2000, 2, 29, 14, 57, 29, 37);

let fmt = efmt::Format::from_str("%Y-%m-%d").unwrap();
assert_eq!(fmt, consts::ISO8601_DATE);

let fmtd_bday = Formatter::new(bday, fmt);
assert_eq!(format!("{fmtd_bday}"), format!("2000-02-29"));

let fmt = Format::from_str("%Y-%m-%dT%H:%M:%S.%f %T").unwrap();
assert_eq!(fmt, consts::ISO8601);

let fmtd_bday = Formatter::new(bday, fmt);
// ISO with the timescale is the default format
assert_eq!(format!("{fmtd_bday}"), format!("{bday}"));

let fmt = Format::from_str("%Y-%j").unwrap();
assert_eq!(fmt, consts::ISO8601_ORDINAL);

let fmt_iso_ord = Formatter::new(bday, consts::ISO8601_ORDINAL);
assert_eq!(format!("{fmt_iso_ord}"), "2000-060");

let fmt = Format::from_str("%A, %d %B %Y %H:%M:%S").unwrap();
assert_eq!(fmt, consts::RFC2822_LONG);

let fmt = Formatter::new(bday, consts::RFC2822_LONG);
assert_eq!(
    format!("{fmt}"),
    format!("Tuesday, 29 February 2000 14:57:29")
);

let fmt = Format::from_str("%a, %d %b %Y %H:%M:%S").unwrap();
assert_eq!(fmt, consts::RFC2822);

let fmt = Formatter::new(bday, consts::RFC2822);
assert_eq!(format!("{fmt}"), format!("Tue, 29 Feb 2000 14:57:29"));

Implementations§

Source§

impl Format

Source

pub fn parse(&self, s_in: &str) -> Result<Epoch, HifitimeError>

Trait Implementations§

Source§

impl Clone for Format

Source§

fn clone(&self) -> Format

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Format

Source§

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

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

impl Default for Format

Source§

fn default() -> Format

Returns the “default value” for a type. Read more
Source§

impl FromStr for Format

Source§

type Err = ParsingError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Format

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Format

Source§

impl StructuralPartialEq for Format

Auto Trait Implementations§

§

impl Freeze for Format

§

impl RefUnwindSafe for Format

§

impl Send for Format

§

impl Sync for Format

§

impl Unpin for Format

§

impl UnwindSafe for Format

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.