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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! This module roughly corresponds to `mach/clock_types.h`.

use crate::ffi::{c_int, c_uint, c_ulonglong};
use core::ops::{Add, Sub, AddAssign, SubAssign};
use core::cmp::Ordering;
use core::time::Duration;

use crate::vm_types::integer_t;

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;

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

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

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;


#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
pub struct time_value_t {
    pub seconds: integer_t,
    pub microseconds: integer_t,
}
pub const TIME_MICROS_MAX: integer_t = 1_000_000;

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Hash)]
pub struct mach_timespec_t {
    pub tv_sec:  c_uint,
    pub tv_nsec: clock_res_t,
}
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;
pub type mach_timespec = mach_timespec_t;

impl PartialOrd for mach_timespec {
    fn partial_cmp(&self, other: &mach_timespec) -> Option<Ordering> {
        Some(mach_timespec::cmp(self, other))
    }
}
impl Ord for mach_timespec {
    fn cmp(&self, other: &mach_timespec) -> Ordering {
        mach_timespec::cmp(self, other)
    }
}

impl From<mach_timespec> for Duration {
    fn from(val: mach_timespec) -> Duration {
        val.to_duration()
    }
}
impl From<&mach_timespec> for Duration {
    fn from(val: &mach_timespec) -> Duration {
        val.to_duration()
    }
}
impl From<Duration> for mach_timespec {
    fn from(val: Duration) -> mach_timespec {
        mach_timespec::from_duration(val)
    }
}
impl From<&Duration> for mach_timespec {
    fn from(val: &Duration) -> mach_timespec {
        mach_timespec::from_duration(*val)
    }
}

/*
impl PartialEq for mach_timespec {
    fn eq(&self, other: &Self) -> bool {
        assert!( self.is_valid());
        assert!(other.is_valid());

        self.tv_sec == other.tv_sec && self.tv_nsec == other.tv_nsec
    }
}*/

impl<T> PartialEq<T> for mach_timespec
where T: Into<Duration> + Clone
{
    fn eq(&self, other: &T) -> bool {
        self.to_duration() == other.clone().into()
    }
}
impl Eq for mach_timespec {}

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

    pub const fn from_secs(sec: c_uint) -> Self {
        Self {
            tv_sec:  sec,
            tv_nsec: 0
        }
    }
    pub const fn from_nanos(nsec: clock_res_t) -> Self {
        if nsec < 0 {
            panic!("mach_timespec cannot convert from nanoseconds: `.tv_nsec` is a negative integer!");
        }
        Self::from_duration( Duration::from_nanos(nsec as u64) )
    }

    /* start non-const methods */
    pub fn from_secs_f64(fsec: f64) -> Self {
        Self::from_duration( Duration::from_secs_f64(fsec) )
    }
    pub fn as_secs_f64(&self) -> f64 {
        self.to_duration().as_secs_f64()
    }

    pub fn from_secs_f32(fsec: f32) -> Self {
        Self::from_duration( Duration::from_secs_f32(fsec) )
    }
    pub fn as_secs_f32(&self) -> f32 {
        self.to_duration().as_secs_f32()
    }
    /* end non-const methods */

    pub const fn to_duration(&self) -> Duration {
        if ! self.is_valid() {
            panic!("mach_timespec is invalid!");
        }

        let sec  = self.tv_sec  as u64;
        let nsec = self.tv_nsec as u32;

        Duration::new(sec, nsec)
    }
    pub const fn from_duration(dur: Duration) -> Self {
        let tv_sec = {
            let sec = dur.as_secs();

            // handle 2038 year issue (aka Y2038 problem)
            if sec > (c_uint::MAX as u64) {
                panic!("cannot convert from Duration to mach_timespec: .tv_sec overflow c_uint!");
            }

            sec as c_uint
        };

        // this is fine.
        // because 1,000,000,000 never overflow c_int (even it's 32-bit).
        // and the return value is never be negative number: https://doc.rust-lang.org/1.76.0/src/core/time.rs.html#36
        let tv_nsec = dur.subsec_nanos() as clock_res_t;

        let this = Self {
            tv_sec,
            tv_nsec,
        };
        assert!(this.is_valid()); // this should never fails (otherwise found a bug)
        this
    }

    pub const fn is_valid(&self) -> bool {
        if self.tv_nsec < 0 {
            return false;
        }

        if (self.tv_nsec as c_ulonglong) >= NSEC_PER_SEC {
            return false;
        }

        true
    }

    pub const fn cmp(&self, other: &Self) -> Ordering {
        if self.tv_sec > other.tv_sec {
            return Ordering::Greater;
        }
        if self.tv_sec < other.tv_sec {
            return Ordering::Less;
        }

        // self.tv_sec == other.tv_sec

        if self.tv_nsec > other.tv_nsec {
            return Ordering::Greater;
        }
        if self.tv_nsec < other.tv_nsec {
            return Ordering::Less;
        }

        // both of seconds and nano-seconds is equal.

        Ordering::Equal
    }

    pub const fn nsec_diff(&self, other: &Self)
        -> c_ulonglong
    {
        if self.tv_sec > other.tv_sec {
            return NSEC_PER_SEC;
        }

        if self.tv_sec < other.tv_sec {
            return !NSEC_PER_SEC;
        }

        (self.tv_nsec - other.tv_nsec) as c_ulonglong
    }

    pub const fn add(&self, other: &Self) -> Self {
        let mut result = Self::new();

        result.tv_sec  = self.tv_sec  + other.tv_sec;
        result.tv_nsec = self.tv_nsec + other.tv_nsec;

        let t1ns = self.tv_nsec as c_ulonglong;
        if t1ns >= NSEC_PER_SEC {
            result.tv_nsec =
                (t1ns - NSEC_PER_SEC) as clock_res_t;

            result.tv_sec += 1;
        }

        result
    }

    pub const fn sub(&self, other: &Self) -> Self {
        let mut result = Self::new();

        result.tv_sec  = self.tv_sec  - other.tv_sec;
        result.tv_nsec = self.tv_nsec - other.tv_nsec;

        if result.tv_nsec < 0 {
            result.tv_nsec +=
                NSEC_PER_SEC as clock_res_t;

            result.tv_sec  -= 1;
        }

        result
    }
}

macro_rules! _timespec_ops_impl {
    ($op:tt, $func:tt) => {
        _timespec_ops_impl!($op, $func, $func);
    };

    ($op:tt, $trait_func:tt, $orig_func:tt) => {
        impl $op<mach_timespec> for mach_timespec {
            type Output = mach_timespec;
            fn $trait_func(self, other: mach_timespec)
                -> mach_timespec
            {
                mach_timespec::$orig_func(&self, &other)
            }
        }

        // we can not use Self (due to it contains borrow symbol)
        impl $op<&mach_timespec> for &mach_timespec {
            type Output = mach_timespec;
            fn $trait_func(self, other: &mach_timespec)
                -> mach_timespec
            {
                mach_timespec::$orig_func(self, other)
            }
        }
        
        impl $op<&mach_timespec> for mach_timespec {
            type Output = mach_timespec;
            fn $trait_func(self, other: &mach_timespec)
                -> mach_timespec
            {
                mach_timespec::$orig_func(&self, other)
            }
        }
        impl $op<mach_timespec> for &mach_timespec {
            type Output = mach_timespec;
            fn $trait_func(self, other: mach_timespec)
                -> mach_timespec
            {
                mach_timespec::$orig_func(self, &other)
            }
        }
    };
}

_timespec_ops_impl!(Add, add);
_timespec_ops_impl!(Sub, sub);

macro_rules! _timespec_ops_assign_impl {
    ($op:tt, $func:tt, $orig_func:tt) => {
        impl $op for mach_timespec {
            fn $func(&mut self, other: mach_timespec) {
                let ret = mach_timespec::$orig_func(&self, &other);
                *self = ret;
            }
        }
    }
}

_timespec_ops_assign_impl!(AddAssign, add_assign, add);
_timespec_ops_assign_impl!(SubAssign, sub_assign, sub);

/* legacy functions */

#[allow(non_snake_case)]
pub const fn BAD_MACH_TIMESPEC(t: mach_timespec) ->bool{
    ! t.is_valid()
}

#[allow(non_snake_case)]
pub const fn CMP_MACH_TIMESPEC(
    t1: &mach_timespec,
    t2: &mach_timespec
) -> c_ulonglong {
    t1.nsec_diff(t2)
}

pub const fn add_mach_timespec(
    t1: &mach_timespec,
    t2: &mach_timespec
) -> mach_timespec {
    mach_timespec::add(t1, t2)
}
pub const fn sub_mach_timespec(
    t1: &mach_timespec,
    t2: &mach_timespec
) -> mach_timespec {
    mach_timespec::sub(t1, t2)
}

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

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

#[cfg(test)]
mod test {
    use super::*;
    use std::io::Write;

    use crate::pl;

    #[test]
    fn ops_add() {
        let mut rets = std::vec::Vec::new();
        let c = 10240;
        for _ in 0..c {
            let n = fastrand::i32(clock_res_t::MIN as i32..clock_res_t::MAX as i32) as clock_res_t;
            rets.push(_ops_add(n));
        }

        for out in
            rets.into_iter()
            .skip(c - 5)
            .collect::<Vec<_>>()
        {
            pl!("{}", out);
        }
    }

    fn _ops_add(n: clock_res_t) -> String {
        let ret = std::panic::catch_unwind(|| {
            mach_timespec::from_nanos(n)
        });

        let mut out = String::new();
        if n >= 0 {
            let spec = ret.expect("mach_timespec::from_nanos() parse failed");
            assert!(spec.is_valid());
            out.push_str(&format!("spec({spec:#?}) is valid"));
            let dur = Duration::from_nanos(n as u64);

            assert_eq!(spec, dur);
            out.push_str(&format!("spec is equal to {:#?}", dur));
        } else {
            assert!(ret.is_err());
            out.push_str(&format!("{n:?} is invalid"));
        }
        out
    }
}