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
// time.rs
//
// Copyright (C) 2019-2021  Minnesota Department of Transportation
// Copyright (C) 2019-2022  Douglas P Lau
//
//! Units of time.
//!
//! Each unit is defined relative to seconds with a conversion factor.  They
//! can be used to conveniently create [Period] and [Frequency] structs.
//!
//! ## Example
//!
//! ```rust
//! use mag::time::{s, ms, ns};
//!
//! let a = 22.8 * s; // Period<s>
//! let b = 50.6 * ms; // Period<ms>
//! let c = 60.0 / s; // Frequency<s>
//! let d = 3.1234 / ns; // Frequency<ns>
//!
//! assert_eq!(a.to_string(), "22.8 s");
//! assert_eq!(b.to_string(), "50.6 ms");
//! assert_eq!(c.to_string(), "60 ㎐");
//! assert_eq!(format!("{:.2}", d), "3.12 ㎓");
//! ```
//! [Frequency]: ../struct.Frequency.html
//! [Period]: ../struct.Period.html
//!
pub(crate) mod timepriv;

/// Unit definition for time
pub trait Unit {
    /// Unit label
    const LABEL: &'static str;

    /// Inverse unit label
    const INVERSE: &'static str;

    /// Multiplication factor to convert to seconds
    const S_FACTOR: f64;

    /// Multiplication factor to convert to another unit
    fn factor<T: Unit>() -> f64 {
        Self::S_FACTOR / T::S_FACTOR
    }
}

/// Define a custom [unit] of [time]
///
/// * `unit` Unit struct name
/// * `label` Standard unit label
/// * `inverse` Inverse time unit (frequency)
/// * `s_factor` Factor to convert to seconds
///
/// # Example: Fortnight
/// ```rust
/// use mag::{time_unit, time::h};
///
/// time_unit!(
///     Fortnight,
///     "fortnight",
///     "/fortnight",
///     14.0 * 24.0 * 60.0 * 60.0
/// );
///
/// let f = 1 * Fortnight;
/// assert_eq!(f.to::<h>(), 24 * 14 * h);
/// ```
///
/// [time]: struct.Period.html
/// [unit]: time/trait.Unit.html
#[macro_export]
macro_rules! time_unit {
    (
        $(#[$doc:meta])* $unit:ident,
        $label:expr,
        $inverse:expr,
        $s_factor:expr
    ) => {
        $(#[$doc])*
        #[allow(non_camel_case_types)]
        #[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
        pub struct $unit;

        impl $crate::time::Unit for $unit {
            const LABEL: &'static str = $label;
            const INVERSE: &'static str = $inverse;
            const S_FACTOR: f64 = $s_factor;
        }

        // f64 * <unit> => Period
        impl core::ops::Mul<$unit> for f64 {
            type Output = $crate::Period<$unit>;
            fn mul(self, _other: $unit) -> Self::Output {
                $crate::Period::new(self)
            }
        }

        // i32 * <unit> => Period
        impl core::ops::Mul<$unit> for i32 {
            type Output = $crate::Period<$unit>;
            fn mul(self, _other: $unit) -> Self::Output {
                $crate::Period::new(f64::from(self))
            }
        }

        // f64 / <unit> => Frequency
        impl core::ops::Div<$unit> for f64 {
            type Output = $crate::Frequency<$unit>;
            fn div(self, _other: $unit) -> Self::Output {
                $crate::Frequency::new(self)
            }
        }

        // i32 / <unit> => Frequency
        impl core::ops::Div<$unit> for i32 {
            type Output = $crate::Frequency<$unit>;
            fn div(self, _other: $unit) -> Self::Output {
                $crate::Frequency::new(f64::from(self))
            }
        }

        // Length / <unit> => Speed
        impl<L> core::ops::Div<$unit> for $crate::Length<L>
        where
            L: $crate::length::Unit
        {
            type Output = $crate::Speed<L, $unit>;
            fn div(self, _unit: $unit) -> Self::Output {
                $crate::Speed::new(self.quantity)
            }
        }
    };
}

time_unit!(
    /** Gigasecond */
    Gs,
    "Gs",
    "nHz",
    1_000_000_000.0
);

time_unit!(
    /** Megasecond */
    Ms,
    "Ms",
    "μHz",
    1_000_000.0
);

time_unit!(
    /** Kilosecond */
    Ks,
    "Ks",
    "mHz",
    1_000.0
);

time_unit!(
    /** Week */
    wk,
    "wk",
    "/wk",
    7.0 * 24.0 * 60.0 * 60.0
);

time_unit!(
    /** Day */
    d,
    "d",
    "/d",
    24.0 * 60.0 * 60.0
);

time_unit!(
    /** Hour */
    h,
    "h",
    "/h",
    60.0 * 60.0
);

time_unit!(
    /** Minute */
    min,
    "min",
    "/min",
    60.0
);

time_unit!(
    /** Second */
    s,
    "s",
    "㎐",
    1.0
);

time_unit!(
    /** Decisecond */
    ds,
    "ds",
    "daHz",
    0.1
);

time_unit!(
    /** Millisecond */
    ms,
    "ms",
    "㎑",
    0.001
);

time_unit!(
    /** Microsecond */
    us,
    "μs",
    "㎒",
    0.000_001
);

time_unit!(
    /** Nanosecond */
    ns,
    "ns",
    "㎓",
    0.000_000_001
);

time_unit!(
    /** Picosecond */
    ps,
    "ps",
    "㎔",
    0.000_000_000_001
);

#[cfg(test)]
mod test {
    extern crate alloc;

    use super::super::Frequency;
    use super::*;
    use alloc::{format, string::ToString};

    #[test]
    fn time_display() {
        assert_eq!((23.7 * s).to_string(), "23.7 s");
        assert_eq!((3.25 * h).to_string(), "3.25 h");
        assert_eq!((50.0 / s).to_string(), "50 ㎐");
        assert_eq!((2.0 / d).to_string(), "2 /d");
        assert_eq!(format!("{:.1}", 333.3333 / us), "333.3 ㎒");
    }

    #[test]
    fn time_to() {
        assert_eq!((4.75 * h).to(), 285.0 * min);
        assert_eq!((2.5 * s).to(), 2_500.0 * ms);
        assert_eq!((1_000.0 / s).to(), 1.0 / ms);
        assert_eq!((300.0 / ms).to(), 0.3 / us);
    }

    #[test]
    fn time_add() {
        assert_eq!(3.5 * d + 1.25 * d, 4.75 * d);
        assert_eq!(1.0 * wk + 2.1 * wk, 3.1 * wk);
        assert_eq!(5.0 / ns + 4.0 / ns, 9.0 / ns);
    }

    #[test]
    fn time_sub() {
        assert_eq!(567.8 * us - 123.4 * us, 444.4 * us);
        assert_eq!(23.0 / ms - 12.0 / ms, 11.0 / ms);
    }

    #[test]
    fn time_mul() {
        assert_eq!((6.5 * ns) * 12.0, 78.0 * ns);
        assert_eq!(4.0 * (1.5 * h), 6.0 * h);
        assert_eq!(2.5 / ds * 2.0, 5.0 / ds);
    }

    #[test]
    fn time_div() {
        assert_eq!(5. / h, Frequency::<h>::new(5.0));
        assert_eq!(60.0 / s, Frequency::<s>::new(60.0));
        assert_eq!(1.0 / (1.0 * s), 1.0 / s);
        assert_eq!(2.0 / (1.0 / min), 2.0 * min);
    }
}