Skip to main content

luaur_vm/functions/
os_timegm.rs

1#[allow(non_camel_case_types)]
2pub type time_t = i64;
3
4#[repr(C)]
5#[derive(Debug, Copy, Clone)]
6#[allow(non_camel_case_types)]
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}
18
19pub fn os_timegm(timep: *const tm) -> time_t {
20    let timep = unsafe { &*timep };
21
22    let day = timep.tm_mday;
23    let month = timep.tm_mon + 1;
24    let year = timep.tm_year + 1900;
25
26    let a = if timep.tm_mon % 12 < 2 { 1 } else { 0 };
27    let a = a - (timep.tm_mon / 12);
28
29    let y = year + 4800 - a;
30    let m = month + (12 * a) - 3;
31
32    let julianday = day + ((153 * m + 2) / 5) + (365 * y) + (y / 4) - (y / 100) + (y / 400) - 32045;
33
34    let utcstartasjulianday = 2440588;
35    let utcstartasjuliansecond: i64 = (utcstartasjulianday as i64) * 86400;
36
37    if (julianday as i64) < (utcstartasjulianday as i64) {
38        return -1;
39    }
40
41    let daysecond =
42        (timep.tm_hour as i64) * 3600 + (timep.tm_min as i64) * 60 + (timep.tm_sec as i64);
43    let julianseconds = (julianday as i64) * 86400 + daysecond;
44
45    if julianseconds < utcstartasjuliansecond {
46        return -1;
47    }
48
49    julianseconds - utcstartasjuliansecond
50}