use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(target_arch = "wasm32")]
#[path = "sys/wasm32.rs"]
mod inner;
#[cfg(unix)]
#[path = "sys/unix.rs"]
mod inner;
#[cfg(windows)]
#[path = "sys/windows.rs"]
mod inner;
pub struct Timespec {
pub sec: i64,
pub nsec: i32,
}
impl Timespec {
pub fn now() -> Timespec {
let st =
SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch");
Timespec { sec: st.as_secs() as i64, nsec: st.subsec_nanos() as i32 }
}
pub fn local(self) -> Tm {
let mut tm = Tm {
tm_sec: 0,
tm_min: 0,
tm_hour: 0,
tm_mday: 0,
tm_mon: 0,
tm_year: 0,
tm_wday: 0,
tm_yday: 0,
tm_isdst: 0,
tm_utcoff: 0,
tm_nsec: 0,
};
inner::time_to_local_tm(self.sec, &mut tm);
tm.tm_nsec = self.nsec;
tm
}
}
#[cfg(feature = "clock")]
#[repr(C)]
pub struct Tm {
pub tm_sec: i32,
pub tm_min: i32,
pub tm_hour: i32,
pub tm_mday: i32,
pub tm_mon: i32,
pub tm_year: i32,
pub tm_wday: i32,
pub tm_yday: i32,
pub tm_isdst: i32,
pub tm_utcoff: i32,
pub tm_nsec: i32,
}
impl Tm {
pub fn to_timespec(&self) -> Timespec {
let sec = match self.tm_utcoff {
0 => inner::utc_tm_to_time(self),
_ => inner::local_tm_to_time(self),
};
Timespec { sec: sec, nsec: self.tm_nsec }
}
}