nstd_sys/core/
time.rs

1//! Low level time utilities.
2use crate::{
3    core::optional::{gen_optional, NSTDOptional},
4    NSTDFloat64, NSTDInt64, NSTDUInt32,
5};
6use core::time::Duration;
7use nstdapi::nstdapi;
8
9/// Represents a span of time.
10#[nstdapi]
11#[derive(Clone, Copy, PartialEq)]
12pub struct NSTDDuration {
13    /// The duration in seconds.
14    seconds: NSTDFloat64,
15}
16impl NSTDDuration {
17    /// Converts an [`NSTDDuration`] into a [Duration].
18    ///
19    /// # Panics
20    ///
21    /// Panics if `duration` is negative, overflows Rust's `Duration` structure, or is non-finite.
22    #[inline]
23    #[allow(dead_code)]
24    pub(crate) fn into_duration(self) -> Duration {
25        Duration::from_secs_f64(self.seconds)
26    }
27}
28gen_optional!(NSTDOptionalDuration, NSTDDuration);
29
30/// Creates a new `NSTDDuration` object from an `NSTDFloat64` representing the duration in seconds.
31///
32/// # Parameters:
33///
34/// - `NSTDFloat64 seconds` - The time span in seconds.
35///
36/// # Returns
37///
38/// `NSTDDuration duration` - The time span represented as an `NSTDDuration` object.
39#[inline]
40#[nstdapi]
41pub const fn nstd_core_time_duration_new(seconds: NSTDFloat64) -> NSTDDuration {
42    NSTDDuration { seconds }
43}
44
45/// Returns the number of seconds stored in an `NSTDDuration` as an `NSTDFloat64`.
46///
47/// # Parameters:
48///
49/// - `NSTDDuration duration` - The duration object.
50///
51/// # Returns
52///
53/// `NSTDFloat64 seconds` - The number of seconds in a duration object represented as an
54/// `NSTDFloat64`.
55#[inline]
56#[nstdapi]
57pub const fn nstd_core_time_duration_get(duration: NSTDDuration) -> NSTDFloat64 {
58    duration.seconds
59}
60
61/// Returns the number of seconds in an `NSTDDuration` object.
62///
63/// # Parameters:
64///
65/// - `NSTDDuration duration` - The duration object.
66///
67/// # Returns
68///
69/// `NSTDInt64 seconds` - The number of seconds held in `duration`.
70#[inline]
71#[nstdapi]
72#[allow(clippy::cast_possible_truncation)]
73pub const fn nstd_core_time_duration_seconds(duration: NSTDDuration) -> NSTDInt64 {
74    duration.seconds as _
75}
76
77/// Returns the number of nanoseconds in an `NSTDDuration` object.
78///
79/// # Parameters:
80///
81/// - `NSTDDuration duration` - The duration object.
82///
83/// # Returns
84///
85/// `NSTDUInt32 nanoseconds` - The number of nanoseconds held in `duration`.
86#[nstdapi]
87pub fn nstd_core_time_duration_nanoseconds(duration: NSTDDuration) -> NSTDUInt32 {
88    /// The number of nanoseconds in a full second.
89    const NANOS_IN_SEC: NSTDFloat64 = 1_000_000_000.0;
90    #[allow(
91        clippy::cast_possible_truncation,
92        clippy::cast_precision_loss,
93        clippy::cast_sign_loss
94    )]
95    {
96        let nanos = duration.seconds - duration.seconds as NSTDInt64 as NSTDFloat64;
97        match nanos >= 0.0 {
98            true => (nanos * NANOS_IN_SEC) as _,
99            false => (nanos * -NANOS_IN_SEC) as _,
100        }
101    }
102}
103
104/// Computes the addition of two time spans.
105///
106/// # Parameters:
107///
108/// - `NSTDDuration lhs` - The left-hand side operand.
109///
110/// - `NSTDDuration rhs` - The right-hand side operand.
111///
112/// # Returns
113///
114/// `NSTDDuration duration` - The result of the time span addition.
115#[inline]
116#[nstdapi]
117pub fn nstd_core_time_duration_add(lhs: NSTDDuration, rhs: NSTDDuration) -> NSTDDuration {
118    nstd_core_time_duration_new(lhs.seconds + rhs.seconds)
119}
120
121/// Computes the subtraction between two time spans.
122///
123/// # Parameters:
124///
125/// - `NSTDDuration lhs` - The left-hand side operand.
126///
127/// - `NSTDDuration rhs` - The right-hand side operand.
128///
129/// # Returns
130///
131/// `NSTDDuration duration` - The result of the time span subtraction.
132#[inline]
133#[nstdapi]
134pub fn nstd_core_time_duration_sub(lhs: NSTDDuration, rhs: NSTDDuration) -> NSTDDuration {
135    nstd_core_time_duration_new(lhs.seconds - rhs.seconds)
136}