Skip to main content

DateTime

Struct DateTime 

Source
pub struct DateTime {
    pub year: u16,
    pub month: u8,
    pub day: u8,
    pub hour: u8,
    pub minute: u8,
    pub second: u8,
    pub tz_offset_minutes: i16,
}
Expand description

Date-time with timezone offset preserved.

§References

  • RFC 5322 Section 3.3 (date-time specification)

Fields§

§year: u16

Year (e.g., 2025).

§month: u8

Month (1–12).

§day: u8

Day of month (1–31).

§hour: u8

Hour (0–23).

§minute: u8

Minute (0–59).

§second: u8

Second (0–59).

§tz_offset_minutes: i16

Timezone offset from UTC in minutes (e.g., +0530 → 330, −0800 → −480).

Implementations§

Source§

impl DateTime

Source

pub fn to_unix_timestamp(&self) -> i64

Converts this date-time to a Unix timestamp (seconds since 1970-01-01T00:00:00Z).

The timezone offset is applied so the result is always UTC-based. Uses the same civil-to-days algorithm as the builder.

§References
  • RFC 5322 Section 3.3
Source

pub fn from_unix_timestamp(timestamp: i64, tz_offset_minutes: i16) -> Self

Creates a DateTime from a Unix timestamp (seconds since epoch) and a timezone offset in minutes.

§References
  • RFC 5322 Section 3.3
Source

pub fn now() -> Self

Returns the current UTC time as a DateTime.

§References
  • RFC 5322 Section 3.3
Source

pub fn weekday(&self) -> u8

Returns the day of week for this date.

Returns 0 for Sunday, 1 for Monday, …, 6 for Saturday.

§References
  • RFC 5322 Section 3.3
Source

pub fn to_rfc5322_string(&self) -> String

Formats this date-time as an RFC 5322 date-time string.

Produces the format: day-of-week, DD Mon YYYY HH:MM:SS ±HHMM (e.g., "Thu, 13 Feb 2025 15:47:33 +0000").

§References
  • RFC 5322 Section 3.3
Source

pub fn to_iso8601_string(&self) -> String

Formats this date-time as an ISO 8601 / RFC 3339 string.

Produces the format: YYYY-MM-DDTHH:MM:SS±HH:MM (e.g., "2025-02-13T15:47:33+00:00").

This format is widely used in JSON APIs and structured data exchange.

§References
  • ISO 8601 (date-time representation)
  • RFC 3339 (date-time on the Internet)
Source

pub fn parse_rfc5322(input: &str) -> Option<Self>

Parses an RFC 5322 date-time string into a DateTime.

Accepts: [day-of-week ","] day month year hour ":" minute [":" second] zone

Strips CFWS (comments and folding white space) before parsing, as allowed by the obsolete date syntax (RFC 5322 Section 4.3).

Returns None if the input is not a valid RFC 5322 date-time.

§References
  • RFC 5322 Section 3.3
  • RFC 5322 Section 4.3 (obsolete syntax)

Trait Implementations§

Source§

impl Clone for DateTime

Source§

fn clone(&self) -> DateTime

Returns a duplicate 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 DateTime

Source§

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

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

impl Display for DateTime

Source§

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

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

impl FromStr for DateTime

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses an RFC 5322 date-time string into a DateTime.

Accepts: [day-of-week ","] day month year hour ":" minute [":" second] zone

This enables the ergonomic "Thu, 13 Feb 2025 15:47:33 +0000".parse::<DateTime>() pattern via the standard FromStr trait.

§References
  • RFC 5322 Section 3.3
  • RFC 5322 Section 4.3 (obsolete syntax)
Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

impl Hash for DateTime

Source§

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

Hashes by UTC-normalized timestamp, consistent with Eq and Ord.

Two DateTime values representing the same UTC instant (but with different timezone offsets) will produce the same hash, matching the Eq behavior.

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 DateTime

Source§

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

Compares two DateTime values by their UTC-normalized timestamps.

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 DateTime

Source§

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

Compares two DateTime values by their UTC-normalized timestamps, consistent with the Ord implementation.

§References
  • RFC 5322 Section 3.3 (date-time specification)
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 DateTime

Source§

fn partial_cmp(&self, other: &Self) -> 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 Eq for DateTime

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.