Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/synapse/blob/main/crates/synapse-coord/src/time.rs # synapse A component of the AienOS ecosystem. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `synapse / crates / synapse-coord / src / time.rs` 111 lines ยท 3437 bytes Raw Blame History 1 //! UTC timestamps exactly as `crumb.c` writes and reads them (`now_iso`, `parse_iso`). 2 3 fn days_from_civil(y: i64, m: i64, d: i64) -> i64 { 4 let y = if m <= 2 { y - 1 } else { y }; 5 let era = y.div_euclid(400); 6 let yoe = y - era * 400; 7 let mp = (m + 9) % 12; 8 let doy = (153 * mp + 2) / 5 + d - 1; 9 let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; 10 era * 146_097 + doe - 719_468 11 } 12 13 fn civil_from_days(z: i64) -> (i64, i64, i64) { 14 let z = z + 719_468; 15 let era = z.div_euclid(146_097); 16 let doe = z - era * 146_097; 17 let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; 18 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); 19 let mp = (5 * doy + 2) / 153; 20 let d = doy - (153 * mp + 2) / 5 + 1; 21 let m = if mp < 10 { mp + 3 } else { mp - 9 }; 22 (yoe + era * 400 + i64::from(m <= 2), m, d) 23 } 24 25 /// `now_iso`: `%Y-%m-%dT%H:%M:%SZ` (UTC) for a Unix time in seconds. 26 pub fn iso(t: i64) -> String { 27 let (y, m, d) = civil_from_days(t.div_euclid(86_400)); 28 let s = t.rem_euclid(86_400); 29 format!( 30 "{y:04}-{m:02}-{d:02}T{:02}:{:02}:{:02}Z", 31 s / 3600, 32 (s / 60) % 60, 33 s % 60 34 ) 35 } 36 37 /// One `%d` conversion of `sscanf`: skip whitespace, optional sign, at least one digit. 38 fn scan_int(s: &[u8], i: &mut usize) -> Option { 39 while *i < s.len() && matches!(s[*i], b' ' | b'\t' | b'\n' | b'\r' | 0x0b | 0x0c) { 40 *i += 1; 41 } 42 let mut neg = false; 43 if *i < s.len() && (s[*i] == b'-' || s[*i] == b'+') { 44 neg = s[*i] == b'-'; 45 *i += 1; 46 } 47 let start = *i; 48 let mut v: i64 = 0; 49 while *i < s.len() && s[*i].is_ascii_digit() { 50 v = v.saturating_mul(10).saturating_add(i64::from(s[*i] - b'0')); 51 *i += 1; 52 } 53 if *i == start { 54 return None; 55 } 56 Some(if neg { -v } else { v }) 57 } 58 59 /// `parse_iso`: `sscanf("%d-%d-%dT%d:%d:%d")` then `timegm` (which normalizes out-of-range 60 /// fields). Returns -1 on failure, as the C tool does; callers treat any negative as expired. 61 pub fn parse_iso(s: Option<&[u8]>) -> i64 { 62 let Some(s) = s else { return -1 }; 63 let mut i = 0; 64 let mut f = [0i64; 6]; 65 let seps = b"--T::"; 66 for k in 0..6 { 67 match scan_int(s, &mut i) { 68 Some(v) => f[k] = v, 69 None => return -1, 70 } 71 if k < 5 { 72 if s.get(i) != Some(&seps[k]) { 73 return -1; 74 } 75 i += 1; 76 } 77 } 78 let [y, mo, d, h, mi, se] = f; 79 let m0 = mo - 1; 80 let y = y + m0.div_euclid(12); 81 let m = m0.rem_euclid(12) + 1; 82 let days = days_from_civil(y, m, 1) + d - 1; 83 days * 86_400 + h * 3600 + mi * 60 + se 84 } 85 86 /// Seconds since the Unix epoch from the system clock (C `time(NULL)`). 87 pub fn system_now() -> i64 { 88 std::time::SystemTime::now() 89 .duration_since(std::time::UNIX_EPOCH) 90 .map(|d| d.as_secs() as i64) 91 .unwrap_or(0) 92 } 93 94 #[cfg(test)] 95 mod tests { 96 use super::*; 97 98 #[test] 99 fn iso_round_trip() { 100 for t in [0i64, 951_782_400, 1_760_400_000, 4_102_444_799] { 101 assert_eq!(parse_iso(Some(iso(t).as_bytes())), t); 102 } 103 assert_eq!(iso(0), "1970-01-01T00:00:00Z"); 104 assert_eq!( 105 parse_iso(Some(b"2026-13-01T00:00:00Z")), 106 parse_iso(Some(b"2027-01-01T00:00:00Z")) 107 ); 108 assert_eq!(parse_iso(Some(b"garbage")), -1); 109 assert_eq!(parse_iso(None), -1); 110 } 111 }