//! UTC timestamps as stored in the database. //! //! The format is RFC 3339 with 9 fractional digits and a `Z` suffix. It has a fixed width, so //! comparing two stamps as text gives the same order as comparing the times. use std::time::Duration; use time::{ OffsetDateTime, SignedDuration, format_description::FormatItem, macros::format_description, }; const FORMAT: &[FormatItem<'static>] = format_description!("[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:9]Z"); fn format_stamp(at: OffsetDateTime) -> String { // The format description only uses fields that always format, so this cannot fail. at.format(FORMAT).unwrap_or_default() } /// Converts a `std` duration to the signed form `time` uses, saturating on overflow. fn signed(d: Duration) -> SignedDuration { SignedDuration::try_from(d).unwrap_or(SignedDuration::MAX) } /// The current time as a stamp. pub fn now_stamp() -> String { format_stamp(OffsetDateTime::now_utc()) } /// The stamp for `now + after`. Saturates at the maximum representable time instead of panicking. pub fn stamp_after(after: Duration) -> String { format_stamp(OffsetDateTime::now_utc().saturating_add(signed(after))) } /// The stamp for `now - before`. Saturates at the minimum representable time instead of panicking. pub fn stamp_before(before: Duration) -> String { format_stamp(OffsetDateTime::now_utc().saturating_sub(signed(before))) } /// Parses a stored stamp back into a time. `None` for anything that is not a stamp. fn parse_stamp(stamp: &str) -> Option { OffsetDateTime::parse(stamp, &time::format_description::well_known::Rfc3339).ok() } /// The Unix time, in whole seconds, of a stored stamp. `None` when it is not a stamp. pub fn unix_seconds(stamp: &str) -> Option { parse_stamp(stamp).map(OffsetDateTime::unix_timestamp) } /// The stamp `after` later than `stamp`, saturating at the largest time. `None` when `stamp` is not a /// stamp. pub fn stamp_plus(stamp: &str, after: Duration) -> Option { Some(format_stamp( parse_stamp(stamp)?.saturating_add(signed(after)), )) } /// The stamp `before` earlier than `stamp`, saturating at the smallest time. `None` when `stamp` is not /// a stamp. pub fn stamp_minus(stamp: &str, before: Duration) -> Option { Some(format_stamp( parse_stamp(stamp)?.saturating_sub(signed(before)), )) } /// The current Unix time in whole seconds. pub fn unix_now() -> i64 { OffsetDateTime::now_utc().unix_timestamp() } /// The stamp of a Unix time in whole seconds. `None` when the time is out of range. pub fn stamp_of_unix(seconds: i64) -> Option { OffsetDateTime::from_unix_timestamp(seconds) .ok() .map(format_stamp) } /// The IANA time zone names of the database compiled into this build, sorted. The bundled database /// is used on purpose, so every install offers the same list whatever the host has installed. pub fn zone_names() -> Vec { let mut names: Vec = jiff::tz::db() .available() .map(|name| name.as_str().to_owned()) .collect(); names.sort(); names } /// True only for an exact IANA name from [`zone_names`]. Matching is case-sensitive and nothing is /// trimmed, so the stored value is always the canonical name. pub fn is_zone(name: &str) -> bool { jiff::tz::db() .available() .any(|known| known.as_str() == name) } #[cfg(test)] mod tests { use super::*; #[test] fn zones_are_checked_against_the_bundled_list() { assert!(is_zone("Europe/Istanbul")); assert!(is_zone("UTC")); for bad in [ "", "europe/istanbul", " Europe/Istanbul", "../../etc/passwd", "Mars/Olympus", ] { assert!(!is_zone(bad), "accepted {bad:?}"); } assert!(zone_names().len() > 300); } #[test] fn stamps_have_fixed_width_and_sort_in_time_order() { let now = now_stamp(); let later = stamp_after(Duration::from_secs(3600)); let much_later = stamp_after(Duration::from_secs(7200)); assert_eq!(now.len(), 30, "bad stamp: {now}"); assert!(now.ends_with('Z')); assert!(now < later && later < much_later); assert!(stamp_before(Duration::from_secs(3600)) < now); } }