pub struct FmtUtc2k(/* private fields */);Expand description
§Formatted UTC2K.
This is the formatted companion to Utc2k. You can use it to obtain a
string version of the date, print it, etc.
While this acts essentially as a glorified String, it is sized exactly
and therefore requires less memory to represent. It also implements Copy.
It follows the simple Unix date format of YYYY-MM-DD hh:mm:ss.
Speaking of, you can obtain an &str using AsRef<str>,
Borrow<str>, or FmtUtc2k::as_str.
If you only want the date or time half, call FmtUtc2k::date or
FmtUtc2k::time respectively.
§Examples
Generally it makes more sense to initialize a Utc2k first, but you can
skip straight to a FmtUtc2k instead:
use utc2k::{FmtUtc2k, Utc2k};
// Start with the current date/time.
let date = FmtUtc2k::now();
// Source from a specific timestamp.
let date = FmtUtc2k::from(946_684_800_u32);
assert_eq!(date.as_str(), "2000-01-01 00:00:00");
// Source from a `Utc2k`.
let utc_date = Utc2k::from(946_684_800_u32);
assert_eq!(FmtUtc2k::from(utc_date), utc_date.formatted());Implementations§
Source§impl FmtUtc2k
§Min/Max.
impl FmtUtc2k
§Min/Max.
Source§impl FmtUtc2k
§Instantiation/Reuse.
impl FmtUtc2k
§Instantiation/Reuse.
Sourcepub const fn from_ascii(src: &[u8]) -> Option<Self>
pub const fn from_ascii(src: &[u8]) -> Option<Self>
§From ASCII Date/Time Slice.
Try to parse a date/time value from an ASCII slice, returning a
FmtUtc2k instance if successful, None if not.
Note that this method will automatically clamp dates outside the
supported 2000..=2099 range to FmtUtc2k::MIN/FmtUtc2k::MAX.
See Utc2k::from_ascii for a rundown of supported formats, etc.
§Examples
use utc2k::FmtUtc2k;
// Separators are flexible.
let dates: [&[u8]; 5] = [
b"20250615", // Squished.
b"2025 06 15", // Spaced.
b"2025/06/15", // Slashed.
b"2025-06-15", // Dashed.
b"2025#06#15", // Hashed? Haha.
];
for raw in dates {
assert_eq!(
FmtUtc2k::from_ascii(raw).unwrap().as_str(),
"2025-06-15 00:00:00",
// ^ ^ ^ Time defaults to midnight.
);
}
// Same for datetimes.
let datetimes: [&[u8]; 10] = [
b"20250615123001",
b"2025-06-15 12:30:01",
b"2025-06-15T12:30:01Z",
b"2025/06/15:12:30:01 GMT",
b"2025/06/15:12:30:01 UT",
b"2025/06/15:12:30:01 UTC",
b"2025/06/15 12:30:01.000 +0000",
b"2025/06/15 12:30:01+0000",
b"2025/06/15T16:30:01+04:00",
b"2025/06/15T05:30:01-07:00",
];
for raw in datetimes {
assert_eq!(
FmtUtc2k::from_ascii(raw).unwrap().as_str(),
"2025-06-15 12:30:01",
);
}Sourcepub const fn from_rfc2822(src: &[u8]) -> Option<Self>
pub const fn from_rfc2822(src: &[u8]) -> Option<Self>
§From RFC2822 Date/Time Slice.
Try to parse a date/time value from a RFC2822-formatted
byte slice, returning a FmtUtc2k instance if successful, None if
not.
§Examples
use utc2k::FmtUtc2k;
// This spec tolerates a lot of variation…
let dates: [&[u8]; 7] = [
b"Tue, 1 Jul 2003 10:52:37 +0000", // Single-digit day.
b"Tue, 1 Jul 2003 10:52:37 +0000", // Digit/space substitution.
b"Tue, 01 Jul 2003 10:52:37 +0000", // Leading zero.
b"1 Jul 2003 10:52:37", // No weekday or offset.
b"01 Jul 2003 10:52:37", // Same, but w/ leading zero.
b"Tue, 01 Jul 2003 03:52:37 -0700", // Negative UTC offset.
b"Tue, 1 Jul 2003 15:22:37 +0430", // Positive UTC offset.
];
for raw in dates {
assert_eq!(
FmtUtc2k::from_rfc2822(raw).unwrap().as_str(),
"2003-07-01 10:52:37",
);
}
// The same variation exists for date-only representations too.
let dates: [&[u8]; 5] = [
b"Tue, 1 Jul 2003", // Single-digit day.
b"Tue, 1 Jul 2003", // Digit/space substitution.
b"Tue, 01 Jul 2003", // Leading zero.
b"1 Jul 2003", // No weekday or offset.
b"01 Jul 2003", // Same, but w/ leading zero.
];
for raw in dates {
assert_eq!(
FmtUtc2k::from_rfc2822(raw).unwrap().as_str(),
"2003-07-01 00:00:00",
);
}Sourcepub const fn from_unixtime(src: u32) -> Self
pub const fn from_unixtime(src: u32) -> Self
§From Timestamp.
Initialize a new FmtUtc2k from a unix timestamp, saturating to
FmtUtc2k::MIN/FmtUtc2k::MAX if out of range.
§Examples
use utc2k::FmtUtc2k;
assert_eq!(
FmtUtc2k::from_unixtime(1_748_672_925).as_str(),
"2025-05-31 06:28:45",
);
// Same as the above, but using the `From<u32>` impl.
assert_eq!(
FmtUtc2k::from(1_748_672_925_u32).as_str(),
"2025-05-31 06:28:45",
);
// Out of range values will saturate to the boundaries of the
// century.
assert_eq!(
FmtUtc2k::from_unixtime(0).as_str(),
"2000-01-01 00:00:00",
);
assert_eq!(
FmtUtc2k::from_unixtime(u32::MAX).as_str(),
"2099-12-31 23:59:59",
);Sourcepub const fn set_datetime(&mut self, src: Utc2k)
pub const fn set_datetime(&mut self, src: Utc2k)
§Set Date/Time.
This can be used to recycle an existing buffer.
As with all other part-based operations, overflows and underflows will
be adjusted automatically, with minimum and maximum dates capped to
FmtUtc2k::MIN and FmtUtc2k::MAX respectively.
§Examples
use utc2k::{FmtUtc2k, Utc2k};
let mut fmt = FmtUtc2k::default();
assert_eq!(fmt.as_str(), "2000-01-01 00:00:00");
fmt.set_datetime(Utc2k::MAX);
assert_eq!(fmt.as_str(), "2099-12-31 23:59:59");
fmt.set_datetime(Utc2k::MIN);
assert_eq!(fmt.as_str(), "2000-01-01 00:00:00");Sourcepub const fn set_parts(&mut self, y: u16, m: u8, d: u8, hh: u8, mm: u8, ss: u8)
pub const fn set_parts(&mut self, y: u16, m: u8, d: u8, hh: u8, mm: u8, ss: u8)
§Set Parts.
This can be used to recycle an existing buffer.
As with all other part-based operations, overflows and underflows will
be adjusted automatically, with minimum and maximum dates capped to
FmtUtc2k::MIN and FmtUtc2k::MAX respectively.
§Examples
use utc2k::{FmtUtc2k, Utc2k};
let mut fmt = FmtUtc2k::default();
assert_eq!(fmt.as_str(), "2000-01-01 00:00:00");
fmt.set_parts(2010, 10, 31, 12, 33, 59);
assert_eq!(fmt.as_str(), "2010-10-31 12:33:59");
// And if you do something weird with the dates...
fmt.set_parts(2010, 10, 32, 12, 33, 59);
assert_eq!(fmt.as_str(), "2010-11-01 12:33:59");Sourcepub fn set_unixtime(&mut self, src: u32)
pub fn set_unixtime(&mut self, src: u32)
§Set Unixtime.
This can be used to recycle an existing buffer.
As with all other part-based operations, overflows and underflows will
be adjusted automatically, with minimum and maximum dates capped to
Utc2k::MIN_UNIXTIME and Utc2k::MAX_UNIXTIME respectively.
§Examples
use utc2k::{FmtUtc2k, Utc2k};
let mut fmt = FmtUtc2k::from(Utc2k::MIN_UNIXTIME);
assert_eq!(fmt.as_str(), "2000-01-01 00:00:00");
fmt.set_unixtime(Utc2k::MAX_UNIXTIME);
assert_eq!(fmt.as_str(), "2099-12-31 23:59:59");Source§impl FmtUtc2k
§Getters.
impl FmtUtc2k
§Getters.
Sourcepub const fn as_bytes(&self) -> &[u8] ⓘ
pub const fn as_bytes(&self) -> &[u8] ⓘ
§As Bytes.
Return a byte string slice in YYYY-MM-DD hh:mm:ss format.
A byte slice can also be obtained using FmtUtc2k::as_ref.
§Examples
use utc2k::FmtUtc2k;
let fmt = FmtUtc2k::MAX;
assert_eq!(fmt.as_bytes(), b"2099-12-31 23:59:59");Source§impl FmtUtc2k
§Formatting.
impl FmtUtc2k
§Formatting.
Sourcepub fn to_rfc2822(&self) -> String
pub fn to_rfc2822(&self) -> String
§To RFC2822.
Return a string formatted according to RFC2822.
§Examples
use utc2k::{FmtUtc2k, Utc2k};
let date = FmtUtc2k::from(Utc2k::new(2003, 7, 1, 10, 52, 37));
assert_eq!(
date.to_rfc2822(),
"Tue, 01 Jul 2003 10:52:37 +0000",
// ^ This implementation zero-pads short day
// numbers rather than truncating them…
);
let date = FmtUtc2k::from(Utc2k::new(2036, 12, 15, 16, 30, 55));
assert_eq!(
date.to_rfc2822(),
"Mon, 15 Dec 2036 16:30:55 +0000",
// ^-----------------------------^ …to keep the output
// length consistent.
);Sourcepub fn to_rfc3339(&self) -> String
pub fn to_rfc3339(&self) -> String
§To RFC3339.
Return a string formatted according to RFC3339.
§Examples
use utc2k::{FmtUtc2k, Utc2k};
let mut fmt = FmtUtc2k::from(Utc2k::MIN_UNIXTIME);
assert_eq!(fmt.to_rfc3339(), "2000-01-01T00:00:00Z");
fmt.set_unixtime(Utc2k::MAX_UNIXTIME);
assert_eq!(fmt.to_rfc3339(), "2099-12-31T23:59:59Z");
// The reverse operation — parsing an RFC3339 datetime string into
// a FmtUtc2k — can be done using `FmtUtc2k::from_ascii`.
assert_eq!(
FmtUtc2k::from_ascii(fmt.to_rfc3339().as_bytes()),
Some(fmt),
);Trait Implementations§
Source§impl<'de> Deserialize<'de> for FmtUtc2k
Available on crate feature serde only.
impl<'de> Deserialize<'de> for FmtUtc2k
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
§Deserialize.
Use the optional serde crate feature to enable serialization support.