1#![deny(missing_docs)]
10
11pub mod admin;
12pub mod config;
13pub mod merge;
14pub mod server;
15pub mod store;
16
17pub use config::{Config, ConfigError, TlsMode};
18pub use merge::Merger;
19pub use server::Server;
20pub use store::Store;
21
22use time::OffsetDateTime;
23
24pub fn now() -> String {
32 format_timestamp(OffsetDateTime::now_utc())
33}
34
35pub(crate) fn format_timestamp(at: OffsetDateTime) -> String {
40 let fmt = time::macros::format_description!(
43 "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z"
44 );
45 at.to_offset(time::UtcOffset::UTC)
46 .format(&fmt)
47 .expect("the timestamp format is a compile-time constant")
48}
49
50pub(crate) fn parse_timestamp(text: &str) -> Option<OffsetDateTime> {
52 OffsetDateTime::parse(text, &time::format_description::well_known::Rfc3339).ok()
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn timestamps_read_back_to_the_same_moment() {
61 let at = OffsetDateTime::from_unix_timestamp_nanos(1_788_472_195_191_000_000).unwrap();
62 assert_eq!(parse_timestamp(&format_timestamp(at)), Some(at));
63 assert_eq!(parse_timestamp("yesterday"), None);
64 }
65
66 #[test]
67 fn timestamps_match_javascripts_toisostring() {
68 let t = now();
69 assert_eq!(t.len(), 24, "got {t}");
70 assert!(t.ends_with('Z'), "got {t}");
71 assert_eq!(&t[10..11], "T", "got {t}");
72 assert_eq!(&t[19..20], ".", "got {t}");
73 assert!(
74 t[20..23].chars().all(|c| c.is_ascii_digit()),
75 "milliseconds must be digits, got {t}"
76 );
77 }
78
79 #[test]
82 fn renders_real_milliseconds_not_literal_zeroes() {
83 let at = OffsetDateTime::from_unix_timestamp_nanos(1_788_472_195_191_000_000).unwrap();
84 assert_eq!(format_timestamp(at), "2026-09-03T21:49:55.191Z");
85 }
86}