#[non_exhaustive]pub struct DateTime { /* private fields */ }Expand description
Date-time with timezone offset preserved.
§Equality and Ordering
PartialEq, Eq, Hash, PartialOrd, and Ord are implemented manually
rather than derived. All comparisons normalize to UTC first, so two values
representing the same instant with different timezone offsets are considered
equal (e.g., 2025-01-01T00:00:00+0000 == 2024-12-31T19:00:00-0500).
§References
- RFC 5322 Section 3.3 (date-time specification)
Implementations§
Source§impl DateTime
impl DateTime
Sourcepub fn new(
year: u16,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
tz_offset_minutes: i16,
) -> Self
pub fn new( year: u16, month: u8, day: u8, hour: u8, minute: u8, second: u8, tz_offset_minutes: i16, ) -> Self
Creates a new DateTime from individual components.
Fields are stored as-is; clamping to valid ranges happens at
formatting / comparison time (see Self::to_rfc5322_string,
Self::to_unix_timestamp).
§References
- RFC 5322 Section 3.3 (date-time specification)
Sourcepub fn day(&self) -> u8
pub fn day(&self) -> u8
Returns the day of month, clamped to a valid range for the current month and year (RFC 5322 Section 3.3).
Sourcepub fn second(&self) -> u8
pub fn second(&self) -> u8
Returns the second, clamped to 0–60 (60 is valid for leap seconds per RFC 5322 Section 3.3).
Sourcepub fn tz_offset_minutes(&self) -> i16
pub fn tz_offset_minutes(&self) -> i16
Returns the timezone offset from UTC in minutes (e.g., +0530 → 330, −0800 → −480), clamped to ±1439 (RFC 5322 Section 3.3).
Sourcepub fn to_unix_timestamp(&self) -> i64
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
Sourcepub fn from_unix_timestamp(timestamp: i64, tz_offset_minutes: i16) -> Self
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
Sourcepub fn weekday(&self) -> u8
pub fn weekday(&self) -> u8
Returns the day of week for this date.
Returns 0 for Sunday, 1 for Monday, …, 6 for Saturday.
Uses clamped month/day values for consistency with to_rfc5322_string()
(RFC 5322 Section 3.3).
§References
- RFC 5322 Section 3.3
Sourcepub fn to_rfc5322_string(&self) -> String
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").
All fields are clamped to their valid ranges per RFC 5322 Section 3.3 to ensure well-formed output (Postel’s law: be conservative in what you send).
§References
- RFC 5322 Section 3.3
Sourcepub fn to_iso8601_string(&self) -> String
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").
All fields are clamped to their valid ranges for consistency with
to_rfc5322_string().
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)
Sourcepub fn parse_rfc5322(input: &str) -> Option<Self>
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 FromStr for DateTime
impl FromStr for DateTime
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
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§impl Hash for DateTime
impl Hash for DateTime
Source§impl Ord for DateTime
impl Ord for DateTime
Source§fn cmp(&self, other: &Self) -> Ordering
fn cmp(&self, other: &Self) -> Ordering
Compares two DateTime values by their UTC-normalized timestamps.