libpulse_binding/time/
unix.rs1use std::ops::{Add, AddAssign, Sub, SubAssign};
17use super::{Timeval, MicroSeconds, op_err};
18
19#[repr(transparent)]
21#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
22pub struct UnixTs(pub(crate) Timeval);
23
24impl UnixTs {
25 pub fn now() -> Self {
27 let mut tv = Timeval::new_zero();
28 unsafe { capi::pa_gettimeofday(&mut tv.0) };
29 Self(tv)
30 }
31
32 #[inline]
34 pub fn diff(a: &Self, b: &Self) -> MicroSeconds {
35 MicroSeconds(unsafe { capi::pa_timeval_diff(&(a.0).0, &(b.0).0) })
36 }
37
38 #[inline]
40 pub fn age(&self) -> MicroSeconds {
41 MicroSeconds(unsafe { capi::pa_timeval_age(&(self.0).0) })
42 }
43
44 #[inline]
47 pub fn checked_add(self, rhs: MicroSeconds) -> Option<Self> {
48 self.0.checked_add_us(rhs).and_then(|us| Some(Self(us)))
49 }
50
51 #[inline]
54 pub fn checked_sub(self, rhs: MicroSeconds) -> Option<Self> {
55 self.0.checked_sub_us(rhs).and_then(|us| Some(Self(us)))
56 }
57}
58
59impl std::fmt::Display for UnixTs {
60 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
61 write!(f, "{:?}", self.0)
62 }
63}
64
65impl Add<MicroSeconds> for UnixTs {
66 type Output = Self;
67
68 #[track_caller]
69 #[inline]
70 fn add(self, rhs: MicroSeconds) -> Self {
71 self.checked_add(rhs).expect(op_err::ADD)
72 }
73}
74impl AddAssign<MicroSeconds> for UnixTs {
75 #[track_caller]
76 #[inline]
77 fn add_assign(&mut self, rhs: MicroSeconds) {
78 *self = self.add(rhs);
79 }
80}
81
82impl Sub<MicroSeconds> for UnixTs {
83 type Output = Self;
84
85 #[track_caller]
86 #[inline]
87 fn sub(self, rhs: MicroSeconds) -> Self {
88 self.checked_sub(rhs).expect(op_err::SUB)
89 }
90}
91impl SubAssign<MicroSeconds> for UnixTs {
92 #[track_caller]
93 #[inline]
94 fn sub_assign(&mut self, rhs: MicroSeconds) {
95 *self = self.sub(rhs);
96 }
97}