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
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors

//!
//! Module Structure
//! -----------------
//!  * [`crate`] - Contains the base `Time` struct, describing a standard `Hours/minutes/seconds` framework.
//!  * [`datetime`] - Contains `UTCDateTime` structs, describing a `Date` with a `Time`
//!  * [`epoch`] - Contains `Epoch`, `UnixEpoch`, `GPSEpoch`, and others, providing the datum anchor for timestamps
//!     `UnixTimestamp`, `GPSTimestamp`, etc.
//!  * [`gregorian`] - Contains `Date` and `Month`, that describe a gregorian calendar date.
//!  * [`julian`] - Contains `JulianDate` and it's associated epochs.
//!  * [`crate::format`] - Contains `Format` and `FormatParser` to tranlate dates to and from strings.
//!    * [`crate::format::iso8601`] - ISO8601 Implementations of `DateFormat` and `DateFormatParser`
//!
//! The top level module Contains the various representations of [`Time`]
//!
//! A [`Time`] is a specific time offset into a Day.  Intended for use where Hour:Minute:Seconds are
//! needed.
//!
//! The following are variants of [`epoch::Timestamp`], with specific methods and sizes to
//! to represent the Duration against an [`Epoch`].  These follow the same binary format as the NTP
//! Timestamp format, if used with the `NTP Epoch`.
//! * A [`Time32`] is a Q16.16 `Timestamp` where Seconds and Fractional Seconds are `u16`'s
//! * A [`Time64`] is a Q32.32 `Timestamp` where Seconds and Fractional Seconds are `u32`'s
//! * A [`Time128`] is a Q64.64 `Timestamp` where Seconds and Fractional Seconds are `u64`'s
//!
#![forbid(unsafe_code)]

use std::fmt::{Display, Formatter};

use irox_units::bounds::{GreaterThanEqualToValueError, LessThanValue, Range};
use irox_units::units::duration::{Duration, DurationUnit, NANOS_TO_SEC, SEC_TO_NANOS};

use crate::epoch::Epoch;
use crate::format::{Format, FormatError, FormatParser};

pub mod datetime;
pub mod epoch;
pub mod format;
pub mod gregorian;
pub mod julian;

///
/// Represents a time of the day, an offset into the day from midnight.
///
/// Corresponds to a `UTC of day` in section 5.3.3 of ISO8601
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Time {
    second_of_day: u32,
    nanoseconds: u32,
}

impl Time {
    ///
    /// Creates a Time from the specified seconds and nanoseconds,
    ///
    /// The valid range of 'second_of_day' is `0..86400`,
    /// The valid range of 'nanoseconds' is `0..1_000_000_000`
    pub fn new(
        second_of_day: u32,
        nanoseconds: u32,
    ) -> Result<Time, GreaterThanEqualToValueError<u32>> {
        LessThanValue::new(86400).check_value_is_valid(&second_of_day)?;
        LessThanValue::new(1_000_000_000).check_value_is_valid(&nanoseconds)?;
        Ok(Time {
            second_of_day,
            nanoseconds,
        })
    }

    ///
    /// Creates a Time from the specified fractional seconds, valid range `0..86400`
    pub fn from_seconds_f64(seconds: f64) -> Result<Time, GreaterThanEqualToValueError<f64>> {
        LessThanValue::new(86400_f64).check_value_is_valid(&seconds)?;

        let second_of_day = seconds as u32;
        let frac_nanos = seconds - second_of_day as f64;
        let nanoseconds = (frac_nanos * SEC_TO_NANOS) as u32;
        Ok(Time {
            second_of_day,
            nanoseconds,
        })
    }

    ///
    /// Returns the number of seconds into the "current day"
    #[must_use]
    pub fn get_seconds(&self) -> u32 {
        self.second_of_day
    }

    ///
    /// Returns the number of fractional second nanoseconds
    #[must_use]
    pub fn get_nanoseconds(&self) -> u32 {
        self.nanoseconds
    }

    ///
    /// Converts this time into a duration
    #[must_use]
    pub fn as_duration(&self) -> Duration {
        let time = self.second_of_day as f64 + self.nanoseconds as f64 * NANOS_TO_SEC;
        Duration::new(time, DurationUnit::Second)
    }

    ///
    /// Returns the number of hours represented by this time.
    #[must_use]
    pub fn as_hours(&self) -> u32 {
        (self.second_of_day as f64 / SECONDS_IN_HOUR as f64) as u32
    }

    ///
    /// Returns the minute offset into the day represented by this time.
    #[must_use]
    pub fn as_minutes(&self) -> u32 {
        (self.second_of_day as f64 / SECONDS_IN_MINUTE as f64) as u32
    }

    ///
    /// Returns a triplet, (hours, minutes, seconds) representing this time
    #[must_use]
    pub fn as_hms(&self) -> (u32, u32, u32) {
        let hours = self.as_hours();
        let minutes = self.as_minutes() - hours * MINUTES_IN_HOUR;
        let seconds = self.get_seconds() - hours * SECONDS_IN_HOUR - minutes * SECONDS_IN_MINUTE;
        (hours, minutes, seconds)
    }

    ///
    /// Formats this Time using the specified formatter
    #[must_use]
    pub fn format<F: Format<Item = Self>>(&self, format: &F) -> String {
        format.format(self)
    }

    ///
    /// Tries to parse a Time from the string using the specified Formatter
    pub fn parse_from<F: FormatParser<Item = Self>>(
        format: &F,
        string: &str,
    ) -> Result<Self, FormatError> {
        format.try_from(string)
    }
}

impl Display for Time {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let (h, m, s) = self.as_hms();
        f.write_fmt(format_args!("{h:0}:{m:02}:{s:02}"))
    }
}

impl From<Time> for Duration {
    fn from(value: Time) -> Self {
        value.as_duration()
    }
}

/// 24 Hours in a Day
pub const HOURS_IN_DAY: u32 = 24;

/// 60 Minutes in an Hour
pub const MINUTES_IN_HOUR: u32 = 60;

/// 60 Seconds in a Minute
pub const SECONDS_IN_MINUTE: u32 = 60;

/// 1440 Minutes in a Day
pub const MINUTES_IN_DAY: u32 = 1440;

/// 3600 Seconds in an Hour
pub const SECONDS_IN_HOUR: u32 = 3600;

///
/// Generally 86400, but occasionally 86401 for leap seconds.
pub const SECONDS_IN_DAY: u32 = 86400;

///
/// 32 Bit Fixed Precision Time Format, storing 16 bits of Seconds, and 16 bits
/// of Fractional Seconds.  This is the equivalent of Q16.16, and is semantically
/// equivalent to the NTP Short Format if using the [`epoch::NTP_EPOCH`].
///
/// The 16-bit seconds field can resolve a little over 18 hours, and the
/// 16-bit fractional seconds field can resolve a little over 15 microseconds.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Time32 {
    /// The Reference Epoch
    epoch: Epoch,

    /// The number of seconds into the reference epoch
    seconds: u16,

    /// The fractional number of seconds into the current second.  Divide this
    /// number by 2^16 to get the actual fractional component.
    fractional_seconds: u16,
}
impl Time32 {
    #[must_use]
    pub fn new(epoch: Epoch, seconds: u16, fractional_seconds: u16) -> Self {
        Self {
            epoch,
            seconds,
            fractional_seconds,
        }
    }

    ///
    /// Returns the value of this Time32 as a Q16.16
    #[must_use]
    pub fn as_u32(&self) -> u32 {
        ((self.seconds as u32) << 16) | (self.fractional_seconds as u32)
    }
}

///
/// 64 Bit Fixed Precision Time Format, storing 32 bits of Seconds, and 32 bits
/// of Fractional Seconds.  This is the equivalent of Q32.32, and is semantically
/// equivalent to the NTP Timestamp Format if using the [`epoch::NTP_EPOCH`].
///
/// The 32-bit seconds field can resolve 136 years, and the 32-bit fractional field
/// can resolve down to 232 picoseconds.
///
/// The raw value is 64 bits wide, if you take the middle 32
/// bits, this is identical to a [`Time32`] - (lower 16 of `seconds`, upper 16 of
/// `fractional_seconds`).
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Time64 {
    /// The Reference Epoch
    epoch: Epoch,

    /// The number of seconds into the current reference epoch
    seconds: u32,

    /// The fractional element into the current second.  Divide this number by
    /// 2^32 to get the actual fractional component.
    fractional_seconds: u32,
}

impl Time64 {
    #[must_use]
    pub fn new(epoch: Epoch, seconds: u32, fractional_seconds: u32) -> Self {
        Self {
            epoch,
            seconds,
            fractional_seconds,
        }
    }

    ///
    /// Returns the value of this Time64 as a Q32.32
    #[must_use]
    pub fn as_u64(&self) -> u64 {
        ((self.seconds as u64) << 32) | (self.fractional_seconds as u64)
    }

    ///
    /// Returns the reference epoch of this Time64
    #[must_use]
    pub fn get_epoch(&self) -> Epoch {
        self.epoch
    }
}

///
/// 128 Bit Fixed Precision Time Format, storing 64 bits of Seconds, and 64 bits
/// of Fractional Seconds.  This is the equivalent of Q64.64, and is semantically
/// equivalent to the NTP Datestamp Format if using the [`epoch::NTP_EPOCH`].
///
/// The 64-bit seconds field can resolve 584 million years, and the 64-bit
/// fractional field can resolve down to 54 zepto-seconds (5.4e-20).
///
/// 580 million years ago, multicellular life started.  580 million years from,
/// now, the average temperature of the Earth will be 25C higher - 40C.
///
/// The raw value is 128 bits wide, if you take the middle 64 bits, this is
/// identical to a [`Time64`] - (lower 32 of `seconds`, upper 32 of
/// `fractional_seconds`).
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Time128 {
    ///
    /// Reference Epoch Date
    epoch: Epoch,

    /// The number of seconds into the reference epoch
    seconds: u64,

    /// The fractional element into the current second.  Divide this number by
    /// 2^64 to get the actual fractional component.
    fractional_seconds: u64,
}

impl Time128 {
    #[must_use]
    pub fn new(epoch: Epoch, seconds: u64, fractional_seconds: u64) -> Self {
        Self {
            epoch,
            seconds,
            fractional_seconds,
        }
    }

    ///
    /// Returns the value of this Time128 as a Q64.64
    #[must_use]
    pub fn as_u128(&self) -> u128 {
        ((self.seconds as u128) << 64) | (self.fractional_seconds as u128)
    }

    ///
    /// Returns the reference epoch of this Time128
    #[must_use]
    pub fn get_epoch(&self) -> Epoch {
        self.epoch
    }
}