forge
Explore
Log in

Don't you dare go Hollow

iz

Clone
★ 0

0 watching0 forksRustAGPL-3.0567.3 KiB3950aceTahinli27 days ago34 commits

time.rs81 lines, 2.9 KiBHistoryRawDownload
1//! UTC stamps for storage. Format: RFC 3339, 9 fractional digits, `Z` suffix, always 30 characters,
2//! so that text order is time order. Local calendar handling lives in `zones.rs`.
3
4use std::time::Duration;
5
6use time::{
7 OffsetDateTime, SignedDuration, format_description::FormatItem,
8 format_description::well_known::Rfc3339, macros::format_description,
9};
10
11const FORMAT: &[FormatItem<'static>] =
12 format_description!("[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:9]Z");
13
14/// Formats `at` as a stamp. The format only uses fields that always render, so this cannot fail.
15pub fn format_stamp(at: OffsetDateTime) -> String {
16 at.to_offset(time::UtcOffset::UTC)
17 .format(FORMAT)
18 .unwrap_or_default()
19}
20
21/// Current time as a stamp.
22pub fn now_stamp() -> String {
23 format_stamp(OffsetDateTime::now_utc())
24}
25
26/// The stamp of a Unix time in whole seconds (an ID token's `iat`), or `None` when it is out of range.
27pub fn stamp_from_unix(secs: i64) -> Option<String> {
28 OffsetDateTime::from_unix_timestamp(secs)
29 .ok()
30 .map(format_stamp)
31}
32
33/// Parses an RFC 3339 time (any offset, any fraction length) and re-formats it as a stamp. `None` when
34/// the text does not parse. This brings directory times into the stored form so text comparison works.
35pub fn normalize_stamp(raw: &str) -> Option<String> {
36 OffsetDateTime::parse(raw.trim(), &Rfc3339)
37 .ok()
38 .map(format_stamp)
39}
40
41/// The stamp for `now + after`. Saturates at the largest representable time instead of panicking.
42pub fn stamp_after(after: Duration) -> String {
43 let delta = SignedDuration::try_from(after).unwrap_or(SignedDuration::MAX);
44 format_stamp(OffsetDateTime::now_utc().saturating_add(delta))
45}
46
47/// The stamp of the time `before` ago. Text order is time order, so `deleted_at >= stamp_before(d)` is
48/// "deleted within the last `d`".
49pub fn stamp_before(before: Duration) -> String {
50 let delta = SignedDuration::try_from(before).unwrap_or(SignedDuration::MAX);
51 format_stamp(OffsetDateTime::now_utc().saturating_sub(delta))
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57 use time::macros::datetime;
58
59 #[test]
60 fn normalize_brings_rfc3339_into_stamp_form() {
61 assert_eq!(
62 normalize_stamp("2026-10-28T12:00:00+03:00").as_deref(),
63 Some("2026-10-28T09:00:00.000000000Z")
64 );
65 assert_eq!(
66 normalize_stamp("2026-10-28T09:00:00.5Z").as_deref(),
67 Some("2026-10-28T09:00:00.500000000Z")
68 );
69 assert_eq!(normalize_stamp(""), None);
70 assert_eq!(normalize_stamp("yesterday"), None);
71 }
72
73 #[test]
74 fn stamp_is_fixed_width_utc_with_nine_digits() {
75 let s = format_stamp(datetime!(2026-10-28 09:05:00.5 UTC));
76 assert_eq!(s, "2026-10-28T09:05:00.500000000Z");
77 assert_eq!(s.len(), 30);
78 assert_eq!(now_stamp().len(), 30);
79 assert!(stamp_after(Duration::from_secs(60)) > now_stamp());
80 }
81}