Skip to main content

shared_framework/utils/
date.rs

1//! UTC date and time helpers.
2//!
3//! [`DateUtils`] returns the current UTC time and parses/formats RFC 3339 timestamps.
4//!
5//! ```ignore
6//! let now = DateUtils::now();
7//! let text = DateUtils::format_rfc3339(&now);
8//! let parsed = DateUtils::parse_rfc3339(&text)?;
9//! ```
10use chrono::{DateTime, Utc};
11
12/// Helpers for current time and RFC 3339 conversion in UTC.
13pub struct DateUtils;
14
15impl DateUtils {
16    /// Returns the current UTC time.
17    pub fn now() -> DateTime<Utc> { Utc::now() }
18    /// Parses an RFC 3339 timestamp into UTC. Returns an error on invalid input.
19    pub fn parse_rfc3339(s: &str) -> Result<DateTime<Utc>, chrono::ParseError> {
20        s.parse::<DateTime<Utc>>()
21    }
22    /// Formats a UTC timestamp as RFC 3339.
23    pub fn format_rfc3339(dt: &DateTime<Utc>) -> String { dt.to_rfc3339() }
24}