pub struct UTCDatetime { /* private fields */ }
Expand description
UTC Datetime.
A UTC Datetime consists of a date component and a time-of-day component with nanosecond resolution.
§Examples
use utc_dt::time::UTCTimeOfDay;
use utc_dt::date::UTCDate;
use utc_dt::UTCDatetime;
// UTC Time of Day from a time measurement (for secs, millis, micros, nanos)
let utc_tod = UTCTimeOfDay::try_from_millis(37088903).unwrap();
// UTC Date directly from components
let utc_date = UTCDate::try_from_components(2023, 6, 15).unwrap(); // OR
// UTC Datetime from date and time-of-day components
let utc_datetime = UTCDatetime::from_components(utc_date, utc_tod);
// Get date and time-of-day components
let (utc_date, time_of_day_ns) = (utc_datetime.as_date(), utc_datetime.as_tod()); // OR
let (utc_date, time_of_day_ns) = utc_datetime.as_components();
// Parse a UTC Datetime from an ISO 8601 datetime string `(YYYY-MM-DDThh:mm:ssZ)`
let utc_datetime = UTCDatetime::try_from_iso_datetime("2023-06-15T10:18:08.903Z").unwrap();
// Get UTC datetime string formatted according to ISO 8601 `(YYYY-MM-DDThh:mm:ssZ)`
const PRECISION_SECONDS: usize = 0;
let iso_datetime = utc_datetime.as_iso_datetime(PRECISION_SECONDS);
assert_eq!(iso_datetime, "2023-06-15T10:18:08Z");
// Write ISO 8601 datetime str to a stack buffer
let mut buf = [0; UTCDatetime::iso_datetime_len(PRECISION_SECONDS)];
let _bytes_written = utc_datetime.write_iso_datetime(&mut buf, PRECISION_SECONDS).unwrap();
let iso_datetime_str = core::str::from_utf8(&buf).unwrap();
assert_eq!(iso_datetime_str, "2023-06-15T10:18:08Z");
Implementations§
Source§impl UTCDatetime
impl UTCDatetime
Sourcepub const MIN: UTCDatetime
pub const MIN: UTCDatetime
The minimum UTC datetime.
Equivalent to the unix epoch, 1970.
Sourcepub const MAX: UTCDatetime
pub const MAX: UTCDatetime
The maximum UTC datetime.
Equal to November 9, 584_554_051_223, T07:00:15.999999999Z
.
Maximum datetime support is limited by the maximum UTCTimestamp
.
UTCDatetime can physically store dates up to December 31, 1_717_986_918_399, T23:59:59.999999999Z
Sourcepub const MIN_ISO_DATETIME_LEN: usize = 20usize
pub const MIN_ISO_DATETIME_LEN: usize = 20usize
The minimum length of an ISO datetime (in UTF8 characters)
Sourcepub const fn from_components(date: UTCDate, tod: UTCTimeOfDay) -> Self
pub const fn from_components(date: UTCDate, tod: UTCTimeOfDay) -> Self
Create a datetime frome date and time-of-day components.
Sourcepub const fn as_components(&self) -> (UTCDate, UTCTimeOfDay)
pub const fn as_components(&self) -> (UTCDate, UTCTimeOfDay)
Get copy of the internal date and time-of-day components
Returns tuple: (date: UTCDate, tod: UTCTimeOfDay)
Sourcepub const fn to_components(self) -> (UTCDate, UTCTimeOfDay)
pub const fn to_components(self) -> (UTCDate, UTCTimeOfDay)
Consume self into the internal date and time-of-day components
Returns tuple: (date: UTCDate, tod: UTCTimeOfDay)
Sourcepub const fn as_tod(&self) -> UTCTimeOfDay
pub const fn as_tod(&self) -> UTCTimeOfDay
Get the internal time-of-day component.
Sourcepub fn try_from_iso_datetime(iso: &str) -> Result<Self, UTCDatetimeError>
pub fn try_from_iso_datetime(iso: &str) -> Result<Self, UTCDatetimeError>
Try parse datetime from str in the format:
YYYY-MM-DDThh:mm:ssZ
orYYYY-MM-DDThh:mm:ss.nnnZ
Decimal precision of up to 9 places (inclusive) supported.
Conforms to ISO 8601: https://www.w3.org/TR/NOTE-datetime
Sourcepub fn as_iso_datetime(&self, precision: usize) -> String
pub fn as_iso_datetime(&self, precision: usize) -> String
Return datetime as a string in the format:
- Precision =
0
:YYYY-MM-DDThh:mm:ssZ
- Precision =
3
:YYYY-MM-DDThh:mm:ss.nnnZ
If precision
denotes the number decimal places included after the
seconds, limited to 9 decimal places (nanosecond precision).
If 0
, no decimal component is included.
Conforms to ISO 8601: https://www.w3.org/TR/NOTE-datetime
Sourcepub fn write_iso_datetime(
&self,
buf: &mut [u8],
precision: usize,
) -> Result<usize, UTCDatetimeError>
pub fn write_iso_datetime( &self, buf: &mut [u8], precision: usize, ) -> Result<usize, UTCDatetimeError>
Write an ISO datetime to a buffer in the format:
- Precision =
0
:YYYY-MM-DDThh:mm:ssZ
- Precision =
3
:YYYY-MM-DDThh:mm:ss.nnnZ
The buffer should have a minimum length as given by UTCDatetime::iso_datetime_len.
A buffer of insufficient length will error (UTCDatetimeError::InsufficientStrLen).
Returns number of UTF8 characters (bytes) written
Conforms to ISO 8601: https://www.w3.org/TR/NOTE-datetime
Sourcepub const fn iso_datetime_len(precision: usize) -> usize
pub const fn iso_datetime_len(precision: usize) -> usize
Calculate the number of characters in an ISO datetime str
Trait Implementations§
Source§impl Clone for UTCDatetime
impl Clone for UTCDatetime
Source§fn clone(&self) -> UTCDatetime
fn clone(&self) -> UTCDatetime
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more