interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / crates / aienos-evidence / src / time.rs

64 lines · 2022 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! UTC timestamps without a date library.
17
18use std::time::{SystemTime, UNIX_EPOCH};
19
20/// Civil date from days since 1970-01-01 (Howard Hinnant's algorithm).
21fn civil(days: i64) -> (i64, u32, u32) {
22 let z = days + 719_468;
23 let era = z.div_euclid(146_097);
24 let doe = z.rem_euclid(146_097);
25 let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
26 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
27 let mp = (5 * doy + 2) / 153;
28 let day = (doy - (153 * mp + 2) / 5 + 1) as u32;
29 let month = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
30 let year = yoe + era * 400 + i64::from(month <= 2);
31 (year, month, day)
32}
33
34pub fn iso_utc(secs: u64) -> String {
35 let (y, m, d) = civil((secs / 86_400) as i64);
36 let rem = secs % 86_400;
37 format!(
38 "{y:04}-{m:02}-{d:02}T{:02}:{:02}:{:02}Z",
39 rem / 3_600,
40 rem % 3_600 / 60,
41 rem % 60
42 )
43}
44
45pub fn now_utc() -> String {
46 iso_utc(
47 SystemTime::now()
48 .duration_since(UNIX_EPOCH)
49 .map(|d| d.as_secs())
50 .unwrap_or(0),
51 )
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn known_instants() {
60 assert_eq!(iso_utc(0), "1970-01-01T00:00:00Z");
61 assert_eq!(iso_utc(951_782_400), "2000-02-29T00:00:00Z");
62 assert_eq!(iso_utc(1_790_205_008), "2026-09-23T23:10:08Z");
63 }
64}