1use crate::{backend, io};
2
3pub use crate::timespec::{Nsecs, Secs, Timespec};
4
5/// `clockid_t`
6#[cfg(not(target_os = "wasi"))]
7pub use crate::clockid::{ClockId, DynamicClockId};
8
9/// `clock_getres(id)`—Returns the resolution of a clock.
10///
11/// # References
12/// - [POSIX]
13/// - [Linux]
14/// - [FreeBSD]
15/// - [NetBSD]
16/// - [OpenBSD]
17/// - [DragonFly BSD]
18/// - [illumos]
19///
20/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html
21/// [Linux]: https://man7.org/linux/man-pages/man2/clock_getres.2.html
22/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=clock_getres&sektion=2
23/// [NetBSD]: https://man.netbsd.org/clock_getres.2
24/// [OpenBSD]: https://man.openbsd.org/clock_getres.2
25/// [DragonFly BSD]: https://man.dragonflybsd.org/?command=clock_getres&section=2
26/// [illumos]: https://illumos.org/man/3C/clock_getres
27#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
28#[inline]
29#[must_use]
30pub fn clock_getres(id: ClockId) -> Timespec {
31 backend::time::syscalls::clock_getres(which_clock:id)
32}
33
34/// `clock_gettime(id)`—Returns the current value of a clock.
35///
36/// This function uses `ClockId` which only contains clocks which are known to
37/// always be supported at runtime, allowing this function to be infallible.
38/// For a greater set of clocks and dynamic clock support, see
39/// [`clock_gettime_dynamic`].
40///
41/// # References
42/// - [POSIX]
43/// - [Linux]
44/// - [FreeBSD]
45/// - [NetBSD]
46/// - [OpenBSD]
47/// - [DragonFly BSD]
48/// - [illumos]
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/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=clock_getres&sektion=2
53/// [NetBSD]: https://man.netbsd.org/clock_getres.2
54/// [OpenBSD]: https://man.openbsd.org/clock_getres.2
55/// [DragonFly BSD]: https://man.dragonflybsd.org/?command=clock_getres&section=2
56/// [illumos]: https://illumos.org/man/3C/clock_gettime
57#[cfg(not(target_os = "wasi"))]
58#[inline]
59#[must_use]
60pub fn clock_gettime(id: ClockId) -> Timespec {
61 backend::time::syscalls::clock_gettime(which_clock:id)
62}
63
64/// Like [`clock_gettime`] but with support for dynamic clocks.
65///
66/// # References
67/// - [POSIX]
68/// - [Linux]
69///
70/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html
71/// [Linux]: https://man7.org/linux/man-pages/man2/clock_gettime.2.html
72#[cfg(not(target_os = "wasi"))]
73#[inline]
74pub fn clock_gettime_dynamic(id: DynamicClockId<'_>) -> io::Result<Timespec> {
75 backend::time::syscalls::clock_gettime_dynamic(which_clock:id)
76}
77
78/// `clock_settime(id, timespec)`—Sets the current value of a settable clock.
79///
80/// This fails with [`io::Errno::INVAL`] if the clock is not settable, and
81/// [`io::Errno::ACCESS`] if the current process does not have permission to
82/// set it.
83///
84/// # References
85/// - [POSIX]
86/// - [Linux]
87/// - [FreeBSD]
88/// - [NetBSD]
89/// - [OpenBSD]
90/// - [DragonFly BSD]
91/// - [illumos]
92///
93/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_settime.html
94/// [Linux]: https://man7.org/linux/man-pages/man2/clock_settime.2.html
95/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=clock_settime&sektion=2
96/// [NetBSD]: https://man.netbsd.org/clock_settime.2
97/// [OpenBSD]: https://man.openbsd.org/clock_settime.2
98/// [DragonFly BSD]: https://man.dragonflybsd.org/?command=clock_settime&section=2
99/// [illumos]: https://illumos.org/man/3C/clock_settime
100#[cfg(not(any(
101 target_os = "redox",
102 target_os = "wasi",
103 all(apple, not(target_os = "macos"))
104)))]
105#[inline]
106pub fn clock_settime(id: ClockId, timespec: Timespec) -> io::Result<()> {
107 backend::time::syscalls::clock_settime(which_clock:id, timespec)
108}
109