use time::OffsetDateTime;
use time::format_description::well_known::Rfc3339;
pub fn new_uuid() -> String {
uuid::Uuid::new_v4().simple().to_string()
}
pub fn now_iso8601() -> String {
let now = OffsetDateTime::now_utc();
let millis = now.millisecond();
let truncated = now
.replace_nanosecond(millis as u32 * 1_000_000)
.unwrap_or(now);
truncated
.format(&Rfc3339)
.unwrap_or_else(|_| "1970-01-01T00:00:00.000Z".to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn uuid_is_32_hex_chars() {
let u = new_uuid();
assert_eq!(u.len(), 32);
assert!(u.chars().all(|c| c.is_ascii_hexdigit()));
assert_ne!(new_uuid(), new_uuid());
}
#[test]
fn timestamp_is_rfc3339_ish() {
let ts = now_iso8601();
assert!(ts.contains('T'), "got {ts}");
assert!(ts.len() >= 20, "got {ts}");
}
}