Skip to main content

Module fmt

Module fmt 

Source
Available on crate features fmt only.
Expand description

Module that contains primitives for parsing, formatting, and serializing SystemTime into various formats.

The following formats are available:

  • Iso8601: Parsing and formatting of system time in ISO 8601 format. For example, 2024-08-06T21:30:00Z.

  • Rfc2822: Parsing and formatting of system time in RFC 2822 format. For example, Tue, 6 Aug 2024 14:30:00 -0000.

  • UnixSeconds: Parsing and formatting of system time that is represented as the number of whole seconds since Unix epoch. For example, 0 represents Thu, 1 Jan 1970 00:00:00 -0000.

§Interoperability with SystemTime

Types in this module use the TryFrom trait to convert from SystemTime to the respective format. The conversion is fallible because the SystemTime can be outside the maximum range of the respective format. The conversion back to SystemTime is always infallible.

To retrieve the current system time in the respective format, use the Clock::system_time_as function which retrieves current system time and does the automatic conversion to the output format. This conversion never fails because clock always returns a valid and normalized SystemTime.

§Examples

§Using format types

use tick::fmt::{Iso8601, Rfc2822, UnixSeconds};

// ISO 8601
let time: Iso8601 = "2024-08-06T21:30:00Z".parse()?;
assert_eq!(time.to_string(), "2024-08-06T21:30:00Z");

// RFC 2822
let time: Rfc2822 = "Tue, 06 Aug 2024 14:30:00 GMT".parse()?;
assert_eq!(time.to_string(), "Tue, 06 Aug 2024 14:30:00 GMT");

// Unix seconds
let time: UnixSeconds = "951786000".parse()?;
assert_eq!(time.to_string(), "951786000");

§Using SystemTimeExt

use std::time::{Duration, SystemTime};

use tick::SystemTimeExt;

let time = SystemTime::UNIX_EPOCH + Duration::from_hours(1);
println!("Time: {}", time.display_iso_8601());
// Output: Time: 1970-01-01T01:00:00Z

Structs§

Iso8601
Parser and formatter for system time in ISO 8601 format.
Rfc2822
Parser and formatter for system time in RFC 2822 format, typically used in HTTP headers.
UnixSeconds
A system time represented as the number of whole seconds since the Unix epoch.