relibc/header/sys_timeb/mod.rs
1//! `sys/timeb.h` implementation.
2//!
3//! See <https://pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/timeb.h.html>.
4//!
5//! # Deprecation
6//! The `ftime()` function was marked as legacy in the Open Group Base
7//! Specifications Issue 6, and the entire `sys/timeb.h` header was removed in
8//! Issue 7.
9
10#[allow(deprecated)]
11use crate::header::sys_time::gettimeofday;
12use crate::{
13 header::{sys_select::timeval, sys_time::timezone},
14 out::Out,
15 platform::types::{c_int, c_short, c_ushort, time_t},
16};
17
18/// See <https://pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/timeb.h.html>.
19#[repr(C)]
20#[derive(Default)]
21pub struct timeb {
22 /// The seconds portion of the current time.
23 pub time: time_t,
24 /// The milliseconds portion of the current time.
25 pub millitm: c_ushort,
26 /// The local timezone in minutes west of Greenwich.
27 pub timezone: c_short,
28 /// TRUE if Daylight Savings Time is in effect.
29 pub dstflag: c_short,
30}
31
32/// See <https://pubs.opengroup.org/onlinepubs/009695399/functions/ftime.html>.
33///
34/// Sets the `time` and `millitm` members of the `timeb` structure pointed to
35/// by `tp` to contain the seconds and milliseconds portions, respectively,
36/// of the current time in seconds since the Epoch.
37///
38/// # Safety
39/// The caller must ensure that `tp` is convertible to an [`Out<timeb>`].
40///
41/// # Deprecation
42/// The `ftime()` function was marked as legacy in the Open Group Base
43/// Specifications Issue 6, and the entire `sys/timeb.h` header was removed in
44/// Issue 7.
45#[allow(deprecated)]
46#[deprecated]
47#[unsafe(no_mangle)]
48pub unsafe extern "C" fn ftime(tp: *mut timeb) -> c_int {
49 // SAFETY: the caller is required to ensure that the pointer is valid.
50 let mut tp_out = unsafe { Out::nonnull(tp) };
51
52 let mut tv = timeval::default();
53 let mut tz = timezone::default();
54
55 // SAFETY: tv and tz are created above, and thus will coerce to valid
56 // pointers.
57 if unsafe {
58 #[allow(deprecated)]
59 gettimeofday(&raw mut tv, &raw mut tz)
60 } < 0
61 {
62 return -1;
63 }
64
65 #[allow(deprecated)]
66 tp_out.write(timeb {
67 time: tv.tv_sec,
68 millitm: (tv.tv_usec / 1000) as c_ushort,
69 timezone: tz.tz_minuteswest as c_short,
70 dstflag: tz.tz_dsttime as c_short,
71 });
72
73 0
74}