1 use super::{abi, error::expect_success};
2 use crate::{mem::MaybeUninit, time::Duration};
4 pub use super::itron::time::Instant;
6 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
7 pub struct SystemTime(abi::time_t);
9 pub const UNIX_EPOCH: SystemTime = SystemTime(0);
12 pub fn now() -> SystemTime {
14 let mut out = MaybeUninit::zeroed();
15 expect_success(abi::SOLID_RTC_ReadTime(out.as_mut_ptr()), &"SOLID_RTC_ReadTime");
19 libc::mktime(&mut libc::tm {
24 tm_mon: rtc.tm_mon - 1,
30 tm_zone: crate::ptr::null_mut(),
33 assert_ne!(t, -1, "mktime failed");
37 pub(super) fn from_time_t(t: abi::time_t) -> Self {
41 pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
42 if self.0 >= other.0 {
43 Ok(Duration::from_secs((self.0 as u64).wrapping_sub(other.0 as u64)))
45 Err(Duration::from_secs((other.0 as u64).wrapping_sub(self.0 as u64)))
49 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
50 Some(SystemTime(self.0.checked_add(other.as_secs().try_into().ok()?)?))
53 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
54 Some(SystemTime(self.0.checked_sub(other.as_secs().try_into().ok()?)?))