libpulse_binding/time/
unix.rs

1// Copyright 2018 Lyndon Brown
2//
3// This file is part of the PulseAudio Rust language binding.
4//
5// Licensed under the MIT license or the Apache license (version 2.0), at your option. You may not
6// copy, modify, or distribute this file except in compliance with said license. You can find copies
7// of these licenses either in the LICENSE-MIT and LICENSE-APACHE files, or alternatively at
8// <http://opensource.org/licenses/MIT> and <http://www.apache.org/licenses/LICENSE-2.0>
9// respectively.
10//
11// Portions of documentation are copied from the LGPL 2.1+ licensed PulseAudio C headers on a
12// fair-use basis, as discussed in the overall project readme (available in the git repository).
13
14//! Unix timestamps.
15
16use std::ops::{Add, AddAssign, Sub, SubAssign};
17use super::{Timeval, MicroSeconds, op_err};
18
19/// A Unix timestamp.
20#[repr(transparent)]
21#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
22pub struct UnixTs(pub(crate) Timeval);
23
24impl UnixTs {
25    /// Gets the current ‘time of day’.
26    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    /// Calculates the difference between the two specified timestamps.
33    #[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    /// Gets the time difference between now and self.
39    #[inline]
40    pub fn age(&self) -> MicroSeconds {
41        MicroSeconds(unsafe { capi::pa_timeval_age(&(self.0).0) })
42    }
43
44    /// Checked integer addition. Computes `self + rhs`, returning `None` if overflow occurred,
45    /// using the inner integer’s [`checked_add()`](Timeval::checked_add) method.
46    #[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    /// Checked integer subtraction. Computes `self - rhs`, returning `None` if overflow occurred,
52    /// using the inner integer’s [`checked_sub()`](Timeval::checked_sub) method.
53    #[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}