Skip to content

Commit 8d1bf50

Browse files
committed
Refactor glibc time64 support, riscv32 always has 64-bit time_t
1 parent 516a6d3 commit 8d1bf50

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

library/std/src/sys/unix/time.rs

+36-14
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,34 @@ impl From<libc::timespec> for Timespec {
174174
}
175175
}
176176

177+
#[cfg(all(
178+
target_os = "linux",
179+
target_env = "gnu",
180+
target_pointer_width = "32",
181+
not(target_arch = "riscv32")
182+
))]
183+
#[repr(C)]
184+
pub(in crate::sys::unix) struct __timespec64 {
185+
pub(in crate::sys::unix) tv_sec: i64,
186+
#[cfg(target_endian = "big")]
187+
_padding: i32,
188+
pub(in crate::sys::unix) tv_nsec: i32,
189+
#[cfg(target_endian = "little")]
190+
_padding: i32,
191+
}
192+
193+
#[cfg(all(
194+
target_os = "linux",
195+
target_env = "gnu",
196+
target_pointer_width = "32",
197+
not(target_arch = "riscv32")
198+
))]
199+
impl From<__timespec64> for Timespec {
200+
fn from(t: __timespec64) -> Timespec {
201+
Timespec::new(t.tv_sec, t.tv_nsec.into())
202+
}
203+
}
204+
177205
#[cfg(any(
178206
all(target_os = "macos", any(not(target_arch = "aarch64"))),
179207
target_os = "ios",
@@ -352,29 +380,23 @@ mod inner {
352380
impl Timespec {
353381
pub fn now(clock: libc::clockid_t) -> Timespec {
354382
// Try to use 64-bit time in preparation for Y2038.
355-
#[cfg(all(target_os = "linux", target_env = "gnu", target_pointer_width = "32"))]
383+
#[cfg(all(
384+
target_os = "linux",
385+
target_env = "gnu",
386+
target_pointer_width = "32",
387+
not(target_arch = "riscv32")
388+
))]
356389
{
357390
use crate::sys::weak::weak;
358391

359392
// __clock_gettime64 was added to 32-bit arches in glibc 2.34,
360393
// and it handles both vDSO calls and ENOSYS fallbacks itself.
361-
weak!(fn __clock_gettime64(libc::clockid_t, *mut __timespec64) -> libc::c_int);
362-
363-
#[repr(C)]
364-
struct __timespec64 {
365-
tv_sec: i64,
366-
#[cfg(target_endian = "big")]
367-
_padding: i32,
368-
tv_nsec: i32,
369-
#[cfg(target_endian = "little")]
370-
_padding: i32,
371-
}
394+
weak!(fn __clock_gettime64(libc::clockid_t, *mut super::__timespec64) -> libc::c_int);
372395

373396
if let Some(clock_gettime64) = __clock_gettime64.get() {
374397
let mut t = MaybeUninit::uninit();
375398
cvt(unsafe { clock_gettime64(clock, t.as_mut_ptr()) }).unwrap();
376-
let t = unsafe { t.assume_init() };
377-
return Timespec::new(t.tv_sec, t.tv_nsec as i64);
399+
return Timespec::from(unsafe { t.assume_init() });
378400
}
379401
}
380402

0 commit comments

Comments
 (0)