Skip to main content

UTCDate

Struct UTCDate 

Source
#[non_exhaustive]
pub struct UTCDate(/* private fields */);
Expand description

RFC 3339 UTC timestamp string (RFC 8620 §1.4).

Format: YYYY-MM-DDTHH:MM:SSZ — time-offset MUST be Z, letters uppercase, fractional seconds omitted if zero. Example: "2014-10-30T06:12:00Z".

§Deserialization is NOT validated

UTCDate is #[serde(transparent)] over String. Any string that deserializes into a String deserializes into a UTCDate — including "not-a-date", "2024-01-19T18:00:00" (no Z suffix), or any other shape that violates RFC 8620 §1.4. The newtype carries the type-level intent of “RFC 8620 UTC timestamp” but does NOT enforce it at the wire boundary.

Use UTCDate::new_validated when constructing from untrusted input, or call UTCDate::to_epoch_seconds when consuming a deserialized value: the latter re-validates structural format AND semantic ranges (month, day, hour, minute, second) and returns ValidationError on any deviation. Treat any field of type Option<UTCDate> arriving from a peer as “RFC 8620 timestamp by convention, not by contract”.

Implementations§

Source§

impl UTCDate

Source

pub fn into_inner(self) -> String

Consumes the value and returns the inner String.

Source§

impl UTCDate

Source

pub fn new_validated(s: impl Into<String>) -> Result<Self, ValidationError>

Construct a UTCDate with RFC 8620 §1.4 validation.

Validation has two layers:

  1. Shape: exactly 20 characters in the YYYY-MM-DDTHH:MM:SSZ layout with Z suffix and ASCII digits in every numeric position.
  2. Values: month 1..=12; day 1..=days_in_month(year, month) with proleptic Gregorian leap-year rules so e.g. 2024-02-29 is accepted but 2023-02-29 and 2024-02-30 are rejected; hour 0..=23; minute 0..=59; second 0..=59 (RFC 8620 §1.4 does not permit leap seconds even though RFC 3339 §5.6 does).
§Errors

Returns ValidationError when the input does not satisfy RFC 8620 §1.4. The error description contains a substring that callers can match on if they need finer-grained handling:

  • "exactly 20 characters" — wrong length;
  • "wrong structure" — separators or Z suffix missing / misplaced;
  • "is not a digit" — non-digit at a numeric position;
  • "month must be" — month outside 1..=12;
  • "day must be" — day outside 1..=days_in_month(year, month) (catches both out-of-range day numbers like 32 and non-existent dates like Feb 30 or Feb 29 in a non-leap year);
  • "hour must be", "minute must be", "second must be" — the corresponding time component is out of range.

Use UTCDate::from when the value is known to be valid.

Source

pub fn to_epoch_seconds(&self) -> Result<i64, ValidationError>

Convert this UTCDate to seconds since the Unix epoch (1970-01-01T00:00:00Z).

Re-validates the structural RFC 8620 §1.4 format (the value may have been constructed via UTCDate::from without validation) and also validates semantic ranges: month 1..=12, day 1..=days_in_month, hour 0..=23, minute 0..=59, second 0..=59 (no leap seconds). Returns ValidationError on any validation failure.

Negative values are returned for dates before 1970-01-01T00:00:00Z.

Uses the proleptic Gregorian calendar via Hinnant’s days_from_civil algorithm, which is exact-integer and handles leap years and century rules correctly. No external dependencies.

§Examples
use jmap_types::UTCDate;
let d = UTCDate::new_validated("1970-01-01T00:00:00Z").unwrap();
assert_eq!(d.to_epoch_seconds().unwrap(), 0);
let rfc = UTCDate::new_validated("2014-10-30T06:12:00Z").unwrap();
assert_eq!(rfc.to_epoch_seconds().unwrap(), 1_414_649_520);

Trait Implementations§

Source§

impl AsRef<str> for UTCDate

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<str> for UTCDate

Source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
Source§

impl Clone for UTCDate

Source§

fn clone(&self) -> UTCDate

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for UTCDate

Source§

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

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

impl<'de> Deserialize<'de> for UTCDate

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 UTCDate

Source§

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

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

impl Eq for UTCDate

Source§

impl From<&str> for UTCDate

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for UTCDate

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl Hash for UTCDate

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 PartialEq for UTCDate

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 PartialEq<&str> for UTCDate

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 PartialEq<str> for UTCDate

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 Serialize for UTCDate

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 StructuralPartialEq for UTCDate

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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.