Struct UTCDatetime

Source
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

Source

pub const MIN: UTCDatetime

The minimum UTC datetime.

Equivalent to the unix epoch, 1970.

Source

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

Source

pub const MIN_ISO_DATETIME_LEN: usize = 20usize

The minimum length of an ISO datetime (in UTF8 characters)

Source

pub const fn from_components(date: UTCDate, tod: UTCTimeOfDay) -> Self

Create a datetime frome date and time-of-day components.

Source

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)

Source

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)

Source

pub const fn as_date(&self) -> UTCDate

Get the internal date component.

Source

pub const fn as_tod(&self) -> UTCTimeOfDay

Get the internal time-of-day component.

Source

pub fn try_from_iso_datetime(iso: &str) -> Result<Self, UTCDatetimeError>

Try parse datetime from str in the format:

  • YYYY-MM-DDThh:mm:ssZ or
  • YYYY-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

Source

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

Source

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

Source

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

Source§

fn clone(&self) -> UTCDatetime

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 UTCDatetime

Source§

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

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

impl Default for UTCDatetime

Source§

fn default() -> UTCDatetime

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

impl<'de> Deserialize<'de> for UTCDatetime

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for UTCDatetime

Source§

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

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

impl From<Duration> for UTCDatetime

Source§

fn from(duration: Duration) -> Self

Converts to this type from the input type.
Source§

impl From<UTCTimestamp> for UTCDatetime

Source§

fn from(timestamp: UTCTimestamp) -> Self

Converts to this type from the input type.
Source§

impl Hash for UTCDatetime

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for UTCDatetime

Source§

fn cmp(&self, other: &UTCDatetime) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for UTCDatetime

Source§

fn eq(&self, other: &UTCDatetime) -> 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 PartialOrd for UTCDatetime

Source§

fn partial_cmp(&self, other: &UTCDatetime) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for UTCDatetime

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl UTCTransformations for UTCDatetime

Source§

fn from_timestamp(timestamp: UTCTimestamp) -> Self

Create from a UTC timestamp.
Source§

fn as_timestamp(&self) -> UTCTimestamp

Convert to a UTC timestamp.
Source§

fn from_duration(duration: Duration) -> Self

Create from a duration measured from the Unix Epoch.
Source§

fn as_duration(&self) -> Duration

Convert to a duration measured from the Unix Epoch.
Source§

fn from_secs(secs: u64) -> Self

Create from seconds measured from the Unix Epoch.
Source§

fn as_secs(&self) -> u64

Convert to seconds measured from the Unix Epoch.
Source§

fn from_millis(millis: u64) -> Self

Create from milliseconds measured from the Unix Epoch.
Source§

fn as_millis(&self) -> u128

Convert to milliseconds measured from the Unix Epoch.
Source§

fn from_micros(micros: u64) -> Self

Create from microseconds measured from the Unix Epoch.
Source§

fn as_micros(&self) -> u128

Convert to microseconds measured from the Unix Epoch.
Source§

fn from_nanos(nanos: u64) -> Self

Create from nanoseconds measured from the Unix Epoch.
Source§

fn as_nanos(&self) -> u128

Convert to nanoseconds measured from the Unix Epoch.
Source§

fn try_from_system_time() -> Result<Self, SystemTimeError>

Create from local system time
Source§

impl Copy for UTCDatetime

Source§

impl Eq for UTCDatetime

Source§

impl StructuralPartialEq for UTCDatetime

Auto Trait Implementations§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,