#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct timespec {
pub tv_sec: ::time_t,
pub tv_nsec: ::c_long
}
impl timespec {
pub fn new() -> timespec {
timespec { tv_sec: 0, tv_nsec: 0 }
}
pub fn from_seconds(seconds: i64) -> timespec {
timespec { tv_sec: seconds, tv_nsec: 0 }
}
pub fn to_milliseconds(&self) -> i64 {
(self.tv_sec as i64 * MSEC_PER_SEC) + (self.tv_nsec as i64 / NSEC_PER_MSEC)
}
pub fn to_seconds(&self) -> i64 {
self.to_milliseconds() / MSEC_PER_SEC
}
pub fn clear(&mut self) {
self.tv_sec = 0;
self.tv_nsec = 0;
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct timeval {
pub tv_sec: ::time_t,
pub tv_usec: ::suseconds_t
}
impl timeval {
pub fn new() -> timeval {
timeval { tv_sec: 0, tv_usec: 0 }
}
pub fn from_seconds(seconds: ::time_t) -> timeval {
timeval { tv_sec: seconds, tv_usec: 0 }
}
pub fn to_milliseconds(&self) -> i64 {
(self.tv_sec as i64 * MSEC_PER_SEC) + (self.tv_usec as i64 / USEC_PER_MSEC)
}
pub fn to_seconds(&self) -> i64 {
self.to_milliseconds() / MSEC_PER_SEC
}
pub fn clear(&mut self) {
self.tv_sec = 0;
self.tv_usec = 0;
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct timezone {
pub tz_minuteswest: ::c_int,
pub tz_dsttime: ::c_int
}
pub const ITIMER_REAL: ::c_int = 0;
pub const ITIMER_VIRTUAL: ::c_int = 1;
pub const ITIMER_PROF: ::c_int = 2;
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct itimerspec {
pub it_interval: timespec,
pub it_value: timespec
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct itimerval {
pub it_interval: timeval,
pub it_value: timeval
}
pub const CLOCK_REALTIME: ::clockid_t = 0;
pub const CLOCK_MONOTONIC: ::clockid_t = 1;
pub const CLOCK_PROCESS_CPUTIME_ID: ::clockid_t = 2;
pub const CLOCK_THREAD_CPUTIME_ID: ::clockid_t = 3;
pub const CLOCK_MONOTONIC_RAW: ::clockid_t = 4;
pub const CLOCK_REALTIME_COARSE: ::clockid_t = 5;
pub const CLOCK_MONOTONIC_COARSE: ::clockid_t = 6;
pub const CLOCK_BOOTIME: ::clockid_t = 7;
pub const CLOCK_REALTIME_ALARM: ::clockid_t = 8;
pub const CLOCK_BOOTTIME_ALARM: ::clockid_t = 9;
pub const CLOCK_SGI_CYCLE: ::clockid_t = 10;
pub const CLOCK_TAI: ::clockid_t = 11;
pub const MAX_CLOCKS: ::clockid_t = 16;
pub const CLOCKS_MASK: ::clockid_t = CLOCK_REALTIME | CLOCK_MONOTONIC;
pub const CLOCKS_MONO: ::clockid_t = CLOCK_MONOTONIC;
pub const TIMER_ABSTIME: ::c_int = 0x01;
pub type time64_t = i64;
pub const MSEC_PER_SEC: ::c_long = 1000;
pub const USEC_PER_MSEC: ::c_long = 1000;
pub const NSEC_PER_USEC: ::c_long = 1000;
pub const NSEC_PER_MSEC: ::c_long = 1000000;
pub const USEC_PER_SEC: ::c_long = 1000000;
pub const NSEC_PER_SEC: ::c_long = 1000000000;
pub const FSEC_PER_SEC: ::c_longlong = 1000000000000000;
#[cfg(any(target_arch = "x86",
target_arch = "le32",
target_arch = "powerpc",
target_arch = "arm",
target_arch = "mips",
target_arch = "mipsel"))]
pub const TIME_T_MAX: ::time_t = 0b01111111111111111111111111111111;
#[cfg(any(target_arch = "x86_64",
target_arch = "aarch64"))]
pub const TIME_T_MAX: ::time_t = 0b0111111111111111111111111111111111111111111111111111111111111111;
pub const TIME64_MAX: ::c_longlong = 0b0111111111111111111111111111111111111111111111111111111111111111;
pub const KTIME_MAX: ::c_longlong = 9_223_372_036_854_775_807;
pub const KTIME_SEC_MAX: ::c_longlong = 9_223_372_036;
#[cfg(test)]
mod tests {
use super::*;
#[cfg(any(target_arch = "x86_64",
target_arch = "aarch64"))]
#[test]
fn test_time_t_max_64() {
assert_eq!(9223372036854775807, TIME64_MAX);
}
#[cfg(any(target_arch = "x86",
target_arch = "le32",
target_arch = "powerpc",
target_arch = "arm",
target_arch = "mips",
target_arch = "mipsel"))]
#[test]
fn test_time_t_max_32() {
assert_eq!(2147483647, TIME64_MAX);
}
#[test]
fn test_time64_max() {
assert_eq!(9223372036854775807, TIME64_MAX);
}
#[test]
fn test_timeval_to_msec_sec() {
let mut val = ::timeval::from_seconds(4);
val.tv_usec += USEC_PER_SEC / 2;
assert_eq!(4500, val.to_milliseconds());
assert_eq!(4, val.to_seconds());
}
#[test]
fn test_timespec_to_msec_sec() {
let mut spec = ::timespec::from_seconds(4);
spec.tv_nsec += NSEC_PER_SEC / 2;
assert_eq!(4500, spec.to_milliseconds());
assert_eq!(4, spec.to_seconds());
}
#[test]
fn test_per_sec_accuracy() {
assert_eq!(NSEC_PER_MSEC, NSEC_PER_USEC * USEC_PER_MSEC);
assert_eq!(NSEC_PER_SEC, NSEC_PER_MSEC * MSEC_PER_SEC);
}
#[test]
fn test_timeval_utility_functions() {
let mut val: timeval = timeval::new();
assert_eq!(0, val.tv_sec);
val = timeval::from_seconds(100);
assert_eq!(100, val.tv_sec);
val.clear();
assert_eq!(0, val.tv_sec);
}
#[test]
fn test_timespec_utility_functions() {
let mut spec: timespec = timespec::new();
assert_eq!(0, spec.tv_sec);
spec = timespec::from_seconds(164);
assert_eq!(164, spec.tv_sec);
spec.clear();
assert_eq!(0, spec.tv_sec);
}
}