Skip to main content

luaur_vm/functions/
localtime_r.rs

1#[allow(non_camel_case_types)]
2pub type time_t = i64;
3
4#[repr(C)]
5#[allow(non_camel_case_types)]
6#[derive(Clone, Copy, Debug, Default)]
7pub struct tm {
8    pub tm_sec: core::ffi::c_int,
9    pub tm_min: core::ffi::c_int,
10    pub tm_hour: core::ffi::c_int,
11    pub tm_mday: core::ffi::c_int,
12    pub tm_mon: core::ffi::c_int,
13    pub tm_year: core::ffi::c_int,
14    pub tm_wday: core::ffi::c_int,
15    pub tm_yday: core::ffi::c_int,
16    pub tm_isdst: core::ffi::c_int,
17    #[cfg(not(target_os = "windows"))]
18    pub tm_gmtoff: core::ffi::c_long,
19    #[cfg(not(target_os = "windows"))]
20    pub tm_zone: *const core::ffi::c_char,
21}
22
23#[allow(non_snake_case)]
24pub unsafe fn localtime_r(timep: *const time_t, result: *mut tm) -> *mut tm {
25    #[cfg(target_os = "windows")]
26    {
27        extern "C" {
28            // MSVC's `localtime_s` is an inline wrapper in <time.h>, so it has no
29            // exported symbol to link against ("unresolved external symbol
30            // localtime_s"). The real UCRT export is `_localtime64_s`, taking a
31            // `__time64_t` (our `time_t = i64`).
32            fn _localtime64_s(result: *mut tm, timep: *const time_t) -> core::ffi::c_int;
33        }
34        if _localtime64_s(result, timep) == 0 {
35            result
36        } else {
37            core::ptr::null_mut()
38        }
39    }
40    #[cfg(not(target_os = "windows"))]
41    {
42        extern "C" {
43            fn localtime_r(timep: *const time_t, result: *mut tm) -> *mut tm;
44        }
45        localtime_r(timep, result)
46    }
47}