git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/src/clock.rs
//! The time of day: the board's real-time clock, until the network says
//! better. The `time` service asks an NTP server (or an HTTPS one's date)
//! and sets it here (`clock` op 1); from then on the time is that, carried
//! on by the timer, and the board's clock is set too where it can be, so
//! the next start begins right before there is a network.
use crate::sync::SpinLock;
/// Where the time of day comes from, as `clock` answers in x1.
pub const NONE: u64 = 0;
pub const BOARD: u64 = 1;
pub const NETWORK: u64 = 2;
/// Set from the network: milliseconds since 1970 then, and the timer's
/// milliseconds since boot then.
static SET: SpinLock<Option<(u64, u64)>> = SpinLock::new(None);
/// Milliseconds since 1970 (UTC), and where they come from; 0 and
/// [`NONE`] with no clock and nothing set.
pub fn now_ms() -> (u64, u64) {
if let Some((at, then)) = *SET.lock() {
return (at + (crate::timer::now_ms() - then), NETWORK);
}
match crate::board::clock() {
0 => (0, NONE),
seconds => (seconds * 1000, BOARD),
}
}
/// Seconds since 1970, 0 without a clock: what the file system stamps a
/// change with.
pub fn seconds() -> u64 {
now_ms().0 / 1000
}
/// The time of day is `ms` since 1970, as the network said.
pub fn set(ms: u64) {
*SET.lock() = Some((ms, crate::timer::now_ms()));
crate::board::set_clock(ms / 1000);
}