1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! This module roughly corresponds to `mach/clock_types.h`.

use core::ffi::{c_int, c_uint, c_ulonglong};

pub type alarm_type_t   = c_int;
pub type sleep_type_t   = c_int;
pub type clock_id_t     = c_int;
pub type clock_flavor_t = c_int;
pub type clock_attr_t   = *mut c_int;
pub type clock_res_t    = c_int;

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
pub struct mach_timespec_t {
    pub tv_sec:  c_uint,
    pub tv_nsec: clock_res_t,
}
pub type mach_timespec = mach_timespec_t;

impl mach_timespec_t {
    pub const fn new() -> Self {
        Self {
            tv_sec:  0,
            tv_nsec: 0,
        }
    }
}

pub const SYSTEM_CLOCK:   c_uint = 0;
pub const CALENDAR_CLOCK: c_uint = 1;
pub const REALTIME_CLOCK: c_uint = 0;

pub const CLOCK_GET_TIME_RES: c_uint = 1;
pub const CLOCK_ALARM_CURRES: c_uint = 3;
pub const CLOCK_ALARM_MINRES: c_uint = 4;
pub const CLOCK_ALARM_MAXRES: c_uint = 5;


pub const USEC_PER_SEC:  c_ulonglong = 1_000_000;
pub const NSEC_PER_SEC:  c_ulonglong = 1_000_000_000;

pub const NSEC_PER_USEC: c_ulonglong = 1_000;
pub const NSEC_PER_MSEC: c_ulonglong = 1_000_000;

#[allow(non_snake_case)]
pub const fn BAD_MACH_TIMESPEC(t: mach_timespec) -> bool {
    t.tv_nsec < 0 || (t.tv_nsec as c_ulonglong) >= NSEC_PER_SEC
}

#[allow(non_snake_case)]
pub const fn CMP_MACH_TIMESPEC(
    t1: &mach_timespec,
    t2: &mach_timespec
) -> c_ulonglong {
    if t1.tv_sec > t2.tv_sec {
        return NSEC_PER_SEC;
    }
    if t1.tv_sec < t2.tv_sec {
        return !NSEC_PER_SEC;
    }
    (t1.tv_nsec as c_ulonglong) - (t2.tv_nsec as c_ulonglong)
}

pub const fn add_mach_timespec(t1: &mach_timespec, t2: &mach_timespec) -> mach_timespec {
    let mut t3 = mach_timespec::new();

    t3.tv_sec  = t1.tv_sec  + t2.tv_sec;
    t3.tv_nsec = t1.tv_nsec + t2.tv_nsec;

    let t1ns = t1.tv_nsec as c_ulonglong;
    if t1ns >= NSEC_PER_SEC {
        t3.tv_nsec = (t1ns - NSEC_PER_SEC) as clock_res_t;
        t3.tv_sec += 1;
    }

    t3
}
pub const fn sub_mach_timespec(t1: &mach_timespec, t2: &mach_timespec) -> mach_timespec {
    let mut t3 = mach_timespec::new();

    t3.tv_sec  = t1.tv_sec  - t2.tv_sec;
    t3.tv_nsec = t1.tv_nsec - t2.tv_nsec;

    if t3.tv_nsec < 0 {
        t3.tv_nsec += NSEC_PER_SEC as clock_res_t;
        t3.tv_sec  -= 1;
    }

    t3
}

#[allow(non_snake_case)]
#[deprecated]
pub fn ADD_MACH_TIMESPEC(t1: &mut mach_timespec, t2: &mach_timespec) {
    *t1 = add_mach_timespec(t1, t2);
}
#[allow(non_snake_case)]
#[deprecated]
pub fn SUB_MACH_TIMESPEC(t1: &mut mach_timespec, t2: &mach_timespec) {
    *t1 = sub_mach_timespec(t1, t2);
}

pub const ALRMTYPE:      c_uint = 0xff;
pub const TIME_ABSOLUTE: c_uint = 0x00;
pub const TIME_RELATIVE: c_uint = 0x01;

#[allow(non_snake_case)]
pub const fn BAD_ALRMTYPE(t: c_uint) -> bool {
    ( t & (!TIME_RELATIVE) ) != 0
}