Skip to main content

strat9_kernel/shell/commands/util/
date.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23use super::*;
24use crate::{
25    hardware::timer::rtc::RtcDateTime,
26    shell::output::{format_epoch_stamp, format_ticks, format_uptime},
27};
28
29/// Returns `true` when every field of a CMOS read is in range.
30///
31/// A CMOS that was never programmed - or a platform whose port I/O is a stub,
32/// which is what `arch::io::{inb,outb}` are on riscv64 - reads back as zeroes
33/// or 0xFF. `RtcDateTime::to_timestamp` accepts both without complaint and
34/// returns an instant that looks believable, so the fields have to be checked
35/// before the wall clock is believed. Month 0 or 255 is the giveaway.
36pub(super) fn rtc_fields_plausible(dt: &RtcDateTime) -> bool {
37    (1..=12).contains(&dt.month)
38        && (1..=31).contains(&dt.day)
39        && dt.hour <= 23
40        && dt.minute <= 59
41        && dt.second <= 60 // 60 covers the leap second
42        && (2000..=2100).contains(&dt.year)
43}
44
45/// Reads the CMOS wall clock, or `None` when there is no usable one.
46fn read_wall_clock() -> Option<u64> {
47    if !crate::hardware::timer::rtc::is_available() {
48        return None;
49    }
50    let dt = crate::hardware::timer::rtc::get_datetime();
51    if !rtc_fields_plausible(&dt) {
52        return None;
53    }
54    Some(dt.to_timestamp())
55}
56
57/// Display the current time.
58///
59/// Usage: `date`
60///
61/// Prints the real wall clock when the RTC yields a plausible one, and always
62/// prints uptime. The two used to be conflated: the old command labelled
63/// `ticks / hz` as "Kernel time" and wrapped it with `% 24`, so a machine up
64/// for 30 hours reported "06:00:00" as though that were a time of day.
65pub fn cmd_date(_args: &[String]) -> Result<(), ShellError> {
66    // RTC first, ticks second, so the uptime shown is never older than the
67    // wall clock printed above it.
68    match read_wall_clock() {
69        Some(unix_secs) => shell_println!("{}", format_epoch_stamp(unix_secs)),
70        None => shell_println!("(no RTC: CMOS holds no plausible wall clock)"),
71    }
72
73    let ticks = crate::process::scheduler::ticks();
74    let hz = crate::arch::timer::TIMER_HZ.max(1);
75    let (secs, _cs) = format_ticks(ticks);
76    shell_println!(
77        "up {} since boot  ({} ticks @ {} Hz)",
78        format_uptime(secs),
79        ticks,
80        hz
81    );
82    Ok(())
83}