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
//! Time utilities.
use crate::{
    core::time::{nstd_core_time_duration_new, NSTDDuration},
    NSTDFloat64, NSTDInt64, NSTDUInt32,
};
use cfg_if::cfg_if;
use nstdapi::nstdapi;
use std::time::{SystemTime, UNIX_EPOCH};

cfg_if! {
    if #[cfg(unix)] {
        use crate::os::unix::time::{
            nstd_os_unix_time_add, nstd_os_unix_time_get, nstd_os_unix_time_nanoseconds,
            nstd_os_unix_time_now, nstd_os_unix_time_seconds, nstd_os_unix_time_sub,
            NSTDUnixOptionalTime, NSTDUnixTime,
        };

        /// A structure representing system time since January 1st 1970.
        pub type NSTDTime = NSTDUnixTime;
        impl From<SystemTime> for NSTDTime {
            /// Converts a [SystemTime] into an [NSTDTime] object.
            fn from(value: SystemTime) -> Self {
                match value.duration_since(UNIX_EPOCH) {
                    Ok(dur) => NSTDTime::from_duration(
                        #[allow(unused_unsafe)]
                        // SAFETY: This operation is safe.
                        unsafe { nstd_core_time_duration_new(dur.as_secs_f64()) },
                    ),
                    Err(dur) => NSTDTime::from_duration(
                        #[allow(unused_unsafe)]
                        // SAFETY: This operation is safe.
                        unsafe { nstd_core_time_duration_new(-dur.duration().as_secs_f64()) },
                    ),
                }
            }
        }

        /// Represents an optional value of type `NSTDTime`.
        pub type NSTDOptionalTime = NSTDUnixOptionalTime;
    } else {
        use crate::core::{
            optional::{gen_optional, NSTDOptional},
            time::{
                nstd_core_time_duration_get, nstd_core_time_duration_nanoseconds,
                nstd_core_time_duration_seconds,
            },
        };

        /// A structure representing system time since January 1st 1970.
        #[nstdapi]
        #[derive(Clone, Copy, PartialEq)]
        pub struct NSTDTime {
            /// The time span since January 1st 1970.
            duration: NSTDDuration,
        }
        impl From<SystemTime> for NSTDTime {
            /// Converts a [SystemTime] into an [NSTDTime] object.
            fn from(value: SystemTime) -> Self {
                match value.duration_since(UNIX_EPOCH) {
                    #[allow(unused_unsafe)]
                    Ok(dur) => NSTDTime {
                        // SAFETY: This operation is safe.
                        duration: unsafe {
                            nstd_core_time_duration_new(dur.as_secs_f64())
                        },
                    },
                    #[allow(unused_unsafe)]
                    Err(dur) => NSTDTime {
                        // SAFETY: This operation is safe.
                        duration: unsafe {
                            nstd_core_time_duration_new(-dur.duration().as_secs_f64())
                        },
                    },
                }
            }
        }
        gen_optional!(NSTDOptionalTime, NSTDTime);
    }
}

/// Returns the current system time as an `NSTDTime` object.
///
/// # Returns
///
/// `NSTDOptionalTime time` - The current time on success, or an uninitialized "none" variant on
/// failure.
#[inline]
#[nstdapi]
pub fn nstd_time_now() -> NSTDOptionalTime {
    #[cfg(unix)]
    return nstd_os_unix_time_now();
    #[cfg(not(unix))]
    return NSTDOptional::Some(NSTDTime::from(SystemTime::now()));
}

/// Returns the number of seconds stored in an `NSTDTime` object as an `NSTDFloat64`.
///
/// # Parameters:
///
/// - `NSTDTime time` - The time object.
///
/// # Returns
///
/// `NSTDFloat64 seconds` - The number of seconds in a time object represented as an
/// `NSTDFloat64`.
#[inline]
#[nstdapi]
pub fn nstd_time_get(time: NSTDTime) -> NSTDFloat64 {
    #[cfg(unix)]
    return nstd_os_unix_time_get(time);
    #[cfg(not(unix))]
    return nstd_core_time_duration_get(time.duration);
}

/// Returns the number of seconds in an `NSTDTime` object.
///
/// # Parameters:
///
/// - `NSTDTime time` - The time object.
///
/// # Returns
///
/// `NSTDInt64 seconds` - The number of seconds held in `time`.
#[inline]
#[nstdapi]
pub fn nstd_time_seconds(time: NSTDTime) -> NSTDInt64 {
    #[cfg(unix)]
    return nstd_os_unix_time_seconds(time);
    #[cfg(not(unix))]
    return nstd_core_time_duration_seconds(time.duration);
}

/// Returns the number of nanoseconds in an `NSTDTime` object.
///
/// # Parameters:
///
/// - `NSTDTime time` - The time object.
///
/// # Returns
///
/// `NSTDUInt32 nanoseconds` - The number of nanoseconds held in `time`.
#[inline]
#[nstdapi]
pub fn nstd_time_nanoseconds(time: NSTDTime) -> NSTDUInt32 {
    #[cfg(unix)]
    return nstd_os_unix_time_nanoseconds(time);
    #[cfg(not(unix))]
    return nstd_core_time_duration_nanoseconds(time.duration);
}

/// Computes the addition of an `NSTDTime` object and an `NSTDDuration`.
///
/// # Parameters:
///
/// - `NSTDTime time` - The time object
///
/// - `NSTDDuration duration` - The duration to add.
///
/// # Returns
///
/// `NSTDTime time` - The result of the addition.
#[inline]
#[nstdapi]
pub fn nstd_time_add(time: NSTDTime, duration: NSTDDuration) -> NSTDTime {
    #[cfg(unix)]
    return nstd_os_unix_time_add(time, duration);
    #[cfg(not(unix))]
    {
        let s = nstd_core_time_duration_get(time.duration) + nstd_core_time_duration_get(duration);
        NSTDTime {
            duration: nstd_core_time_duration_new(s),
        }
    }
}

/// Computes the subtraction between an `NSTDTime` object and an `NSTDDuration`.
///
/// # Parameters:
///
/// - `NSTDTime time` - The time object
///
/// - `NSTDDuration duration` - The duration to subtract.
///
/// # Returns
///
/// `NSTDTime time` - The result of the subtraction.
#[inline]
#[nstdapi]
pub fn nstd_time_sub(time: NSTDTime, duration: NSTDDuration) -> NSTDTime {
    #[cfg(unix)]
    return nstd_os_unix_time_sub(time, duration);
    #[cfg(not(unix))]
    {
        let s = nstd_core_time_duration_get(time.duration) - nstd_core_time_duration_get(duration);
        NSTDTime {
            duration: nstd_core_time_duration_new(s),
        }
    }
}