1use crate::arch;
2
3#[allow(non_camel_case_types)]
4pub type time_t = i64;
5#[allow(non_camel_case_types)]
6pub type useconds_t = u32;
7#[allow(non_camel_case_types)]
8pub type suseconds_t = i32;
9
10#[derive(Copy, Clone, Debug)]
13#[repr(C)]
14pub struct timeval {
15 pub tv_sec: time_t,
17 pub tv_usec: suseconds_t,
19}
20
21impl timeval {
22 pub fn from_usec(microseconds: i64) -> Self {
23 Self {
24 tv_sec: (microseconds / 1_000_000),
25 tv_usec: (microseconds % 1_000_000) as i32,
26 }
27 }
28
29 pub fn into_usec(&self) -> Option<i64> {
30 self.tv_sec
31 .checked_mul(1_000_000)
32 .and_then(|usec| usec.checked_add(self.tv_usec.into()))
33 }
34}
35
36#[derive(Copy, Clone, Debug)]
37#[repr(C)]
38pub struct itimerval {
39 pub it_interval: timeval,
40 pub it_value: timeval,
41}
42
43#[derive(Copy, Clone, Debug, Default)]
46#[repr(C)]
47pub struct timespec {
48 pub tv_sec: time_t,
50 pub tv_nsec: i32,
52}
53
54impl timespec {
55 pub fn from_usec(microseconds: i64) -> Self {
56 Self {
57 tv_sec: (microseconds / 1_000_000),
58 tv_nsec: ((microseconds % 1_000_000) * 1000) as i32,
59 }
60 }
61
62 pub fn into_usec(&self) -> Option<i64> {
63 self.tv_sec
64 .checked_mul(1_000_000)
65 .and_then(|usec| usec.checked_add((self.tv_nsec / 1000).into()))
66 }
67}
68
69#[derive(Copy, Clone, Debug, Default)]
70pub struct SystemTime(timespec);
71
72impl SystemTime {
73 pub fn now() -> Self {
75 Self(timespec::from_usec(
76 arch::kernel::systemtime::now_micros() as i64
77 ))
78 }
79}
80
81impl From<timespec> for SystemTime {
82 fn from(t: timespec) -> Self {
83 Self(t)
84 }
85}
86
87impl From<SystemTime> for timespec {
88 fn from(value: SystemTime) -> Self {
89 value.0
90 }
91}