irox_units/units/
duration.rs

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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors

//!
//! Contains [`Duration`] and [`DurationUnit`], a Physical Quantity of amount of Time passed.
//!

use crate::units::{FromUnits, Unit};
use core::fmt::{Display, Formatter};

///
/// Represents a specific duration unit - SI or otherwise.
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum DurationUnit {
    /// SI Base Unit for Duration - Second.
    ///
    /// The second division of the hour by 60.
    #[default]
    Second,

    /// A milli-second, one-thousandth of a second (1e-3)
    Millisecond,

    /// A micro-second, one-millionth of a second (1e-6)
    Microsecond,

    /// A nano-second, one-billionth of a second (1e-9)
    Nanosecond,

    /// A minute, the first division of an hour by 60.
    Minute,

    /// An hour, 24 in a day.
    Hour,

    /// NIST 811 defines a "Day" as 86400 seconds, without the concept of leap seconds.
    Day,

    /// NIST 811 defines a "Year" as 365 days, or 31_536_000 seconds, without the concept of leap
    /// days.
    Year,
}

impl DurationUnit {
    ///
    /// Converts the specified value into Seconds
    pub fn as_seconds(self, value: f64) -> f64 {
        Duration::new(value, self)
            .as_unit(DurationUnit::Second)
            .value
    }
    ///
    /// Converts the specified value into Milliseconds
    pub fn as_millis(self, value: f64) -> f64 {
        Duration::new(value, self)
            .as_unit(DurationUnit::Millisecond)
            .value
    }

    ///
    /// Converts the specified value into Microseconds
    pub fn as_micros(self, value: f64) -> f64 {
        Duration::new(value, self)
            .as_unit(DurationUnit::Microsecond)
            .value
    }

    ///
    /// Converts the specified value into Nanoseconds
    pub fn as_nanos(self, value: f64) -> f64 {
        Duration::new(value, self)
            .as_unit(DurationUnit::Nanosecond)
            .value
    }

    ///
    /// Converts the specified value into Minutes
    pub fn as_minutes(self, value: f64) -> f64 {
        Duration::new(value, self)
            .as_unit(DurationUnit::Minute)
            .value
    }

    ///
    /// Converts the specified value into Hours
    pub fn as_hours(self, value: f64) -> f64 {
        Duration::new(value, self).as_unit(DurationUnit::Hour).value
    }
}

macro_rules! from_units_duration {
    ($type:ident) => {
        impl crate::units::FromUnits<$type> for DurationUnit {
            fn from(&self, value: $type, units: Self) -> $type {
                match self {
                    // target
                    DurationUnit::Nanosecond => match units {
                        // source
                        DurationUnit::Nanosecond => value as $type,
                        DurationUnit::Microsecond => value * MICROS_TO_NANOS as $type,
                        DurationUnit::Millisecond => value * MILLIS_TO_NANOS as $type,
                        DurationUnit::Second => value * SEC_TO_NANOS as $type,
                        DurationUnit::Minute => value * MIN_TO_NANOS as $type,
                        DurationUnit::Hour => value * HOUR_TO_NANOS as $type,
                        DurationUnit::Day => value * DAY_TO_NANOS as $type,
                        DurationUnit::Year => value * YEAR_TO_NANOS as $type,
                    },
                    DurationUnit::Microsecond => match units {
                        // source
                        DurationUnit::Nanosecond => value * NANOS_TO_MICROS as $type,
                        DurationUnit::Microsecond => value as $type,
                        DurationUnit::Millisecond => value * MILLIS_TO_MICROS as $type,
                        DurationUnit::Second => value * SEC_TO_MILLIS as $type,
                        DurationUnit::Minute => value * MIN_TO_MICROS as $type,
                        DurationUnit::Hour => value * HOUR_TO_MICROS as $type,
                        DurationUnit::Day => value * DAY_TO_MICROS as $type,
                        DurationUnit::Year => value * YEAR_TO_MICROS as $type,
                    },
                    DurationUnit::Millisecond => match units {
                        // source
                        DurationUnit::Nanosecond => value * NANOS_TO_MILLIS as $type,
                        DurationUnit::Microsecond => value * MICROS_TO_MILLIS as $type,
                        DurationUnit::Millisecond => value as $type,
                        DurationUnit::Second => value * SEC_TO_MILLIS as $type,
                        DurationUnit::Minute => value * MIN_TO_MILLIS as $type,
                        DurationUnit::Hour => value * HOUR_TO_MILLIS as $type,
                        DurationUnit::Day => value * DAY_TO_MILLIS as $type,
                        DurationUnit::Year => value * YEAR_TO_MILLIS as $type,
                    },
                    DurationUnit::Second => match units {
                        // source
                        DurationUnit::Nanosecond => value * NANOS_TO_SEC as $type,
                        DurationUnit::Microsecond => value * MICROS_TO_SECS as $type,
                        DurationUnit::Millisecond => value * MILLIS_TO_SEC as $type,
                        DurationUnit::Second => value as $type,
                        DurationUnit::Minute => value * MIN_TO_SEC as $type,
                        DurationUnit::Hour => value * HOUR_TO_SEC as $type,
                        DurationUnit::Day => value * DAY_TO_SEC as $type,
                        DurationUnit::Year => value * YEAR_TO_SEC as $type,
                    },
                    DurationUnit::Minute => match units {
                        // source
                        DurationUnit::Nanosecond => value * NANOS_TO_MIN as $type,
                        DurationUnit::Microsecond => value * MICROS_TO_MIN as $type,
                        DurationUnit::Millisecond => value * MILLIS_TO_MIN as $type,
                        DurationUnit::Second => value * SEC_TO_MIN as $type,
                        DurationUnit::Minute => value as $type,
                        DurationUnit::Hour => value * HOUR_TO_MIN as $type,
                        DurationUnit::Day => value * DAY_TO_MIN as $type,
                        DurationUnit::Year => value * YEAR_TO_MIN as $type,
                    },
                    DurationUnit::Hour => match units {
                        // source
                        DurationUnit::Nanosecond => value * NANOS_TO_HOUR as $type,
                        DurationUnit::Microsecond => value * MICROS_TO_HOUR as $type,
                        DurationUnit::Millisecond => value * MILLIS_TO_HOUR as $type,
                        DurationUnit::Second => value * SEC_TO_HOUR as $type,
                        DurationUnit::Minute => value * MIN_TO_HOUR as $type,
                        DurationUnit::Hour => value as $type,
                        DurationUnit::Day => value * DAY_TO_HOUR as $type,
                        DurationUnit::Year => value * YEAR_TO_HOUR as $type,
                    },
                    DurationUnit::Day => match units {
                        // source
                        DurationUnit::Nanosecond => value * NANOS_TO_DAY as $type,
                        DurationUnit::Microsecond => value * MICROS_TO_DAY as $type,
                        DurationUnit::Millisecond => value * MILLIS_TO_DAY as $type,
                        DurationUnit::Second => value * SEC_TO_DAY as $type,
                        DurationUnit::Minute => value * MIN_TO_DAY as $type,
                        DurationUnit::Hour => value * HOUR_TO_DAY as $type,
                        DurationUnit::Day => value as $type,
                        DurationUnit::Year => value * YEAR_TO_DAY as $type,
                    },
                    DurationUnit::Year => match units {
                        // source
                        DurationUnit::Nanosecond => value * NANOS_TO_YEAR as $type,
                        DurationUnit::Microsecond => value * MICROS_TO_YEAR as $type,
                        DurationUnit::Millisecond => value * MILLIS_TO_YEAR as $type,
                        DurationUnit::Second => value * SEC_TO_YEAR as $type,
                        DurationUnit::Minute => value * MIN_TO_YEAR as $type,
                        DurationUnit::Hour => value * HOUR_TO_YEAR as $type,
                        DurationUnit::Day => value * DAY_TO_YEAR as $type,
                        DurationUnit::Year => value as $type,
                    },
                }
            }
        }
    };
}

basic_unit!(Duration, DurationUnit, Second);
from_units_duration!(u32);
from_units_duration!(i32);
from_units_duration!(u64);
from_units_duration!(i64);
from_units_duration!(f32);
from_units_duration!(f64);

impl From<core::time::Duration> for Duration {
    fn from(value: core::time::Duration) -> Self {
        Duration::new(value.as_secs_f64(), DurationUnit::Second)
    }
}

impl From<Duration> for core::time::Duration {
    fn from(value: Duration) -> Self {
        let secs = value.as_seconds();
        let frac_sec = value.as_seconds_f64() - secs as f64;
        let nanos = DurationUnit::Second.as_nanos(frac_sec) as u32;
        core::time::Duration::new(secs, nanos)
    }
}

impl Duration {
    ///
    /// Creates a new duration using the specified number of seconds
    pub const fn new_seconds(value: f64) -> Duration {
        Duration {
            value,
            units: DurationUnit::Second,
        }
    }

    ///
    /// Returns this duration as (Years, Days, Hours, Minutes, Seconds)
    pub fn as_ydhms(&self) -> (u64, u16, u8, u8, u8) {
        let mut rem = *self;
        let years = rem.as_years();
        rem -= Duration::from_years(years);
        let (d, h, m, s) = rem.as_dhms();
        (years, d as u16, h, m, s)
    }

    ///
    /// Returns this duration as (Days, Hours, Minutes, Seconds)
    pub fn as_dhms(&self) -> (u64, u8, u8, u8) {
        let mut rem = *self;
        let days = rem.as_days();
        rem -= Duration::from_days(days);
        let (h, m, s) = rem.as_hms();
        (days, h as u8, m, s)
    }

    ///
    /// Returns this duration as (Hours, Minutes, Seconds)
    pub fn as_hms(&self) -> (u64, u8, u8) {
        let mut rem = *self;
        let hours = rem.as_hours();
        rem -= Duration::from_hours(hours);
        let minutes = rem.as_minutes();
        rem -= Duration::from_minutes(minutes);
        let seconds = rem.as_seconds();
        (hours, minutes as u8, seconds as u8)
    }

    /// Returns the value of this duration as whole seconds, with any fractional
    /// element truncated off.
    pub fn as_seconds(&self) -> u64 {
        self.as_unit(DurationUnit::Second).value() as u64
    }

    /// Returns the value of this duration in fractional seconds
    pub fn as_seconds_f64(&self) -> f64 {
        self.as_unit(DurationUnit::Second).value()
    }

    /// Returns the value of this duration in fractional seconds
    pub fn as_seconds_f32(&self) -> f32 {
        self.as_unit(DurationUnit::Second).value() as f32
    }

    /// Returns the value of this duration as whole milliseconds, with any
    /// fractional element truncated off.
    pub fn as_millis(&self) -> u64 {
        self.as_unit(DurationUnit::Millisecond).value() as u64
    }

    /// Returns the value of this duration as whole microseconds, with any
    /// fractional element truncated off.
    pub fn as_micros(&self) -> u64 {
        self.as_unit(DurationUnit::Microsecond).value() as u64
    }

    /// Returns the value of this duration as whole microseconds, with any
    /// fractional element truncated off.
    pub fn as_nanos(&self) -> u64 {
        self.as_unit(DurationUnit::Nanosecond).value() as u64
    }

    /// Returns the value of this duration as whole minutes, with any fractional
    /// element truncated off
    pub fn as_minutes(&self) -> u64 {
        self.as_unit(DurationUnit::Minute).value() as u64
    }

    /// Returns the value of this duration as whole hours, with any fractional
    /// element truncated off
    pub fn as_hours(&self) -> u64 {
        self.as_unit(DurationUnit::Hour).value() as u64
    }

    /// Returns the value of this duration as whole days, with any fractional
    /// element truncated off
    pub fn as_days(&self) -> u64 {
        self.as_unit(DurationUnit::Day).value() as u64
    }

    /// Returns the value of this duration as whole years, with any fractional
    /// element truncated off
    pub fn as_years(&self) -> u64 {
        self.as_unit(DurationUnit::Year).value() as u64
    }
}

// Backwards compatibility for [`core::time::Duration`] drop-in creation
impl Duration {
    /// Creates a new `Duration` from the specified number of microseconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use irox_units::units::duration::Duration;
    ///
    /// let duration = Duration::from_micros(1_000_002);
    ///
    /// assert_eq!(1, duration.as_seconds());
    /// ```
    pub const fn from_micros(micros: u64) -> Duration {
        Duration::new(micros as f64, DurationUnit::Microsecond)
    }

    /// Creates a new `Duration` from the specified number of milliseconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use irox_units::units::duration::Duration;
    ///
    /// let duration = Duration::from_millis(2569);
    ///
    /// assert_eq!(2, duration.as_seconds());
    /// ```
    pub const fn from_millis(millis: u64) -> Duration {
        Duration::new(millis as f64, DurationUnit::Millisecond)
    }

    /// Creates a new `Duration` from the specified number of nanoseconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use irox_units::units::duration::Duration;
    ///
    /// let duration = Duration::from_nanos(1_000_000_123);
    ///
    /// assert_eq!(1, duration.as_seconds());
    /// ```
    pub const fn from_nanos(nanos: u64) -> Duration {
        Duration::new(nanos as f64, DurationUnit::Nanosecond)
    }

    /// Creates a new `Duration` from the specified number of minutes.
    ///
    /// # Examples
    ///
    /// ```
    /// use irox_units::units::duration::Duration;
    ///
    /// let duration = Duration::from_minutes(1);
    ///
    /// assert_eq!(60, duration.as_seconds());
    /// ```
    pub const fn from_minutes(minutes: u64) -> Duration {
        Duration::new(minutes as f64, DurationUnit::Minute)
    }

    /// Creates a new `Duration` from the specified number of hours.
    ///
    /// # Examples
    ///
    /// ```
    /// use irox_units::units::duration::Duration;
    ///
    /// let duration = Duration::from_hours(1);
    ///
    /// assert_eq!(3600, duration.as_seconds());
    /// ```
    pub const fn from_hours(hours: u64) -> Duration {
        Duration::new(hours as f64, DurationUnit::Hour)
    }

    /// Creates a new `Duration` from the specified number of NIST 811 Days where 1 Day = 86400 Seconds
    ///
    /// # Examples
    ///
    /// ```
    /// use irox_units::units::duration::Duration;
    ///
    /// let duration = Duration::from_days(1);
    ///
    /// assert_eq!(86400, duration.as_seconds());
    /// ```
    pub const fn from_days(days: u64) -> Duration {
        Duration::new(days as f64, DurationUnit::Day)
    }

    /// Creates a new `Duration` from the specified number of NIST 811 Years where 1 Year = 365 Days.
    ///
    /// # Examples
    ///
    /// ```
    /// use irox_units::units::duration::Duration;
    ///
    /// let duration = Duration::from_years(1);
    ///
    /// assert_eq!(31_536_000, duration.as_seconds());
    /// ```
    pub const fn from_years(years: u64) -> Duration {
        Duration::new(years as f64, DurationUnit::Year)
    }

    /// Creates a new `Duration` from the specified number of seconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use irox_units::units::duration::Duration;
    ///
    /// let duration = Duration::from_seconds(100);
    ///
    /// assert_eq!(100, duration.as_seconds());
    /// ```
    pub const fn from_seconds(seconds: u64) -> Duration {
        Duration::new(seconds as f64, DurationUnit::Second)
    }

    /// Creates a new `Duration` from the specified number of f64 seconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use irox_units::units::duration::Duration;
    ///
    /// let duration = Duration::from_seconds_f64(25.5);
    ///
    /// assert_eq!(25.5, duration.as_seconds_f64());
    /// ```
    pub const fn from_seconds_f64(seconds: f64) -> Duration {
        Duration::new(seconds, DurationUnit::Second)
    }
}

impl Display for Duration {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!("{} {:?}", self.value, self.units))
    }
}

// going up
pub const NANOS_TO_MICROS: f64 = 1e-3;
pub const MICROS_TO_MILLIS: f64 = 1e-3;
pub const MILLIS_TO_SEC: f64 = 1e-3;
pub const SEC_TO_MIN: f64 = 1. / MIN_TO_SEC;
pub const MIN_TO_HOUR: f64 = 1. / HOUR_TO_MIN;
pub const HOUR_TO_DAY: f64 = 1. / DAY_TO_HOUR;
pub const DAY_TO_YEAR: f64 = 1. / YEAR_TO_DAY;

// going down
pub const YEAR_TO_DAY: f64 = 365_f64;
pub const DAY_TO_HOUR: f64 = 24_f64;
pub const HOUR_TO_MIN: f64 = 60_f64;
pub const MIN_TO_SEC: f64 = 60_f64;
pub const SEC_TO_MILLIS: f64 = 1e3;
pub const MILLIS_TO_MICROS: f64 = 1e3;
pub const MICROS_TO_NANOS: f64 = 1e3;

// going down double jumps
pub const YEAR_TO_HOUR: f64 = YEAR_TO_DAY * DAY_TO_HOUR;
pub const DAY_TO_MIN: f64 = DAY_TO_HOUR * HOUR_TO_MIN;
pub const HOUR_TO_SEC: f64 = HOUR_TO_MIN * MIN_TO_SEC;
pub const MIN_TO_MILLIS: f64 = MIN_TO_SEC * SEC_TO_MILLIS;
pub const SEC_TO_MICROS: f64 = SEC_TO_MILLIS * MILLIS_TO_MICROS;
pub const MILLIS_TO_NANOS: f64 = MILLIS_TO_MICROS * MICROS_TO_NANOS;

// going up double jumps
pub const NANOS_TO_MILLIS: f64 = NANOS_TO_MICROS * MICROS_TO_MILLIS;
pub const MICROS_TO_SECS: f64 = MICROS_TO_MILLIS * MILLIS_TO_SEC;
pub const MILLIS_TO_MIN: f64 = MILLIS_TO_SEC * SEC_TO_MIN;
pub const SEC_TO_HOUR: f64 = SEC_TO_MIN * MIN_TO_HOUR;
pub const MIN_TO_DAY: f64 = MIN_TO_HOUR * HOUR_TO_DAY;
pub const HOUR_TO_YEAR: f64 = HOUR_TO_DAY * DAY_TO_YEAR;

// going down triples
pub const YEAR_TO_MIN: f64 = YEAR_TO_HOUR * HOUR_TO_MIN;
pub const DAY_TO_SEC: f64 = DAY_TO_MIN * MIN_TO_SEC;
pub const HOUR_TO_MILLIS: f64 = HOUR_TO_SEC * SEC_TO_MILLIS;
pub const MIN_TO_MICROS: f64 = MIN_TO_MILLIS * MILLIS_TO_MICROS;
pub const SEC_TO_NANOS: f64 = SEC_TO_MICROS * MICROS_TO_NANOS;

// going up triples
pub const NANOS_TO_SEC: f64 = NANOS_TO_MILLIS * MILLIS_TO_SEC;
pub const MICROS_TO_MIN: f64 = MICROS_TO_SECS * SEC_TO_MIN;
pub const MILLIS_TO_HOUR: f64 = MILLIS_TO_MIN * MIN_TO_HOUR;
pub const SEC_TO_DAY: f64 = SEC_TO_HOUR * HOUR_TO_DAY;
pub const MIN_TO_YEAR: f64 = MIN_TO_DAY * DAY_TO_YEAR;

// going down quads
pub const YEAR_TO_SEC: f64 = YEAR_TO_MIN * MIN_TO_SEC;
pub const DAY_TO_MILLIS: f64 = DAY_TO_SEC * SEC_TO_MILLIS;
pub const HOUR_TO_MICROS: f64 = HOUR_TO_MILLIS * MILLIS_TO_MICROS;
pub const MIN_TO_NANOS: f64 = MIN_TO_MICROS * MICROS_TO_NANOS;

// going up quads
pub const NANOS_TO_MIN: f64 = NANOS_TO_SEC * SEC_TO_MIN;
pub const MICROS_TO_HOUR: f64 = MICROS_TO_MIN * MIN_TO_HOUR;
pub const MILLIS_TO_DAY: f64 = MILLIS_TO_HOUR * HOUR_TO_DAY;
pub const SEC_TO_YEAR: f64 = SEC_TO_DAY * DAY_TO_YEAR;

// going down pentas
pub const YEAR_TO_MILLIS: f64 = YEAR_TO_SEC * SEC_TO_MILLIS;
pub const DAY_TO_MICROS: f64 = DAY_TO_MILLIS * MILLIS_TO_MICROS;
pub const HOUR_TO_NANOS: f64 = HOUR_TO_MICROS * MICROS_TO_NANOS;

// going up pentas
pub const NANOS_TO_HOUR: f64 = NANOS_TO_MIN * MIN_TO_HOUR;
pub const MICROS_TO_DAY: f64 = MICROS_TO_HOUR * HOUR_TO_DAY;
pub const MILLIS_TO_YEAR: f64 = MILLIS_TO_DAY * DAY_TO_YEAR;

// going down hexas
pub const YEAR_TO_MICROS: f64 = YEAR_TO_MILLIS * MILLIS_TO_MICROS;
pub const DAY_TO_NANOS: f64 = DAY_TO_MICROS * MICROS_TO_NANOS;

// going up hexas
pub const NANOS_TO_DAY: f64 = NANOS_TO_HOUR * HOUR_TO_DAY;
pub const MICROS_TO_YEAR: f64 = MICROS_TO_DAY * DAY_TO_YEAR;

// going down septs
pub const YEAR_TO_NANOS: f64 = YEAR_TO_MICROS * MICROS_TO_NANOS;

// going up septs
pub const NANOS_TO_YEAR: f64 = NANOS_TO_DAY * DAY_TO_YEAR;