xref: /third_party/rust/crates/rustix/src/time/clock.rs (revision b8a62b91)
1use crate::{backend, io};
2
3pub use backend::time::types::{Nsecs, Secs, Timespec};
4
5/// `clockid_t`
6#[cfg(not(target_os = "wasi"))]
7pub use backend::time::types::{ClockId, DynamicClockId};
8
9/// `clock_getres(id)`—Returns the resolution of a clock.
10///
11/// # References
12///  - [POSIX]
13///  - [Linux]
14///
15/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html
16/// [Linux]: https://man7.org/linux/man-pages/man2/clock_getres.2.html
17#[cfg(any(not(any(target_os = "redox", target_os = "wasi"))))]
18#[inline]
19#[must_use]
20pub fn clock_getres(id: ClockId) -> Timespec {
21    backend::time::syscalls::clock_getres(id)
22}
23
24/// `clock_gettime(id)`—Returns the current value of a clock.
25///
26/// This function uses `ClockId` which only contains clocks which are known to
27/// always be supported at runtime, allowing this function to be infallible.
28/// For a greater set of clocks and dynamic clock support, see
29/// [`clock_gettime_dynamic`].
30///
31/// # References
32///  - [POSIX]
33///  - [Linux]
34///
35/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html
36/// [Linux]: https://man7.org/linux/man-pages/man2/clock_gettime.2.html
37#[cfg(not(target_os = "wasi"))]
38#[inline]
39#[must_use]
40pub fn clock_gettime(id: ClockId) -> Timespec {
41    backend::time::syscalls::clock_gettime(id)
42}
43
44/// Like [`clock_gettime`] but with support for dynamic clocks.
45///
46/// # References
47///  - [POSIX]
48///  - [Linux]
49///
50/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html
51/// [Linux]: https://man7.org/linux/man-pages/man2/clock_gettime.2.html
52#[cfg(not(target_os = "wasi"))]
53#[inline]
54pub fn clock_gettime_dynamic(id: DynamicClockId<'_>) -> io::Result<Timespec> {
55    backend::time::syscalls::clock_gettime_dynamic(id)
56}
57