1use super::*;
2
3pub type Time = TimeOf<float>;
9pub type DeltaTime = Time;
10pub type DeltaTimeOf<T> = TimeOf<T>;
11
12pub trait TimeExtension
13{
14 type Output;
15 fn ms (self) -> Self::Output;
16 fn s (self) -> Self::Output;
17 fn mins(self) -> Self::Output;
19 fn hour(self) -> Self::Output;
20 fn day (self) -> Self::Output;
21}
22impl_composite_output_with_methods!(TimeExtension, ms, s, mins, hour, day);
23
24impl<T> TimeExtension for T where T : ToFloat<Output = float>
25{
26 type Output = Time;
27
28 fn ms (self) -> Self::Output { Time::from_ms(self.to_float()) }
29 fn s (self) -> Self::Output { Time::from_s(self.to_float()) }
30 fn mins(self) -> Self::Output { Time::from_mins(self.to_float()) }
31 fn hour(self) -> Self::Output { Time::from_hour(self.to_float()) }
32 fn day (self) -> Self::Output { Time::from_day(self.to_float()) }
33}
34
35#[derive(Clone, Copy, PartialEq, PartialOrd, Default)]
39pub struct TimeOf<T:FloatingNumber> { second : T }
40
41impl<T:FloatingNumber> Debug for TimeOf<T> { fn fmt(&self, f: &mut Formatter<'_>) -> DResult { write!(f, "{}", self) } }
42
43impl<T:FloatingNumber> TimeOf<T>
44{
45 fn display_non_zero_unit(f: &mut Formatter<'_>, val : i32, unit : &str) -> DResult
47 { if val != 0 { Self::display_unit(f, val, unit)?; write!(f, " ") } else { Ok(())} }
48
49 fn display_unit(f: &mut Formatter<'_>, val : i32, unit : &str) -> DResult
50 { write!(f, "{}{}", val, unit) }
51}
52
53impl<T:FloatingNumber> Display for TimeOf<T>
54{
55 fn fmt(&self, f: &mut Formatter<'_>) -> DResult
56 {
57 if self.is_zero() { return write!(f, "0s"); }
58
59 if self.is_strictly_negative() { write!(f, "-")?; }
60
61 Self::display_non_zero_unit(f, self.timer_day(), "d")?;
62 Self::display_non_zero_unit(f, self.timer_hour(), "h")?;
63 Self::display_non_zero_unit(f, self.timer_mins(), "m")?;
64 Self::display_non_zero_unit(f, self.timer_s(), "s")?;
65 Self::display_non_zero_unit(f, self.timer_ms(), "ms")?;
66 Ok(())
67 }
68}
69
70
71impl<T:FloatingNumber> Zero for TimeOf<T> { const ZERO : Self = Self::from_s(T::ZERO); }
72
73impl<T:FloatingNumber> Absolute for TimeOf<T> where T : Absolute
74{
75 fn abs(self) -> Self { TimeOf::from_s(self.second.abs()) }
76}
77
78impl<T:FloatingNumber> TimeOf<T>
79{
80 const fn from_internal_unit(internal_unit : T) -> Self { Self { second : internal_unit } }
81
82 pub fn from_ms (ms : T) -> Self { Self::from_s(ms / T::THOUSAND) }
88
89 pub fn ms(self) -> T { self.second * T::THOUSAND }
95
96
97 pub fn whole_ms(self) -> i32 { self.ms().round_toward_zero().to_i32() }
111
112 pub fn timer_ms(self) -> i32 { self.ms().abs().floor().to_i32() % 1000 }
123
124 pub const fn from_s(second : T) -> Self { Self::from_internal_unit(second) }
130
131 pub fn s(self) -> T { self.second }
137
138 pub fn whole_s(self) -> i32 { self.s().round_toward_zero().to_i32() }
148
149 pub fn timer_s(self) -> i32 { self.s().abs().floor().to_i32() % 60 }
161
162 pub fn from_mins(min : T) -> Self { Self::from_s(min * T::SIXTY) }
168
169 pub fn mins(self) -> T { self.second / T::SIXTY }
175
176 pub fn whole_mins(self) -> i32 { self.mins().round_toward_zero().to_i32() }
190
191
192 pub fn timer_mins(self) -> i32 { self.mins().abs().floor().to_i32() % 60 }
203
204 pub fn from_hour(hours : T) -> Self { Self::from_s(hours * (T::SIXTY * T::SIXTY)) }
210 pub fn hour(self) -> T { self.second / (T::SIXTY * T::SIXTY) }
216
217
218 pub fn whole_hour(self) -> i32 { self.hour().round_toward_zero().to_i32() }
231
232
233 pub fn timer_hour(self) -> i32 { self.hour().abs().floor().to_i32() % 24 }
244
245 pub fn from_day(day : T) -> Self { Self::from_s(day * (T::SIXTY * T::SIXTY * T::TWENTY_FOUR)) }
251 pub fn day(self) -> T { self.second / (T::SIXTY * T::SIXTY * T::TWENTY_FOUR) }
257 pub fn whole_day(self) -> i32 { self.day().round_toward_zero().to_i32() }
271
272
273 pub fn timer_day(self) -> i32 { self.day().abs().floor().to_i32() }
284}
285
286impl<T:FloatingNumber> Add<TimeOf<T>> for TimeOf<T> { type Output=Self; fn add(self, rhs: Self) -> Self::Output { Self::from_internal_unit(self.second.add(rhs.second)) }}
287impl<T:FloatingNumber> AddAssign<TimeOf<T>> for TimeOf<T> { fn add_assign(&mut self, rhs: Self) { self.second.add_assign(rhs.second); }}
288
289impl<T:FloatingNumber> Sub<TimeOf<T>> for TimeOf<T> { type Output=Self; fn sub(self, rhs: Self) -> Self::Output { Self::from_internal_unit(self.second.sub(rhs.second)) }}
290impl<T:FloatingNumber> SubAssign<TimeOf<T>> for TimeOf<T> { fn sub_assign(&mut self, rhs: Self) { self.second.sub_assign(rhs.second); }}
291
292impl<T:FloatingNumber> Div<TimeOf<T>> for TimeOf<T> { type Output=T; fn div(self, rhs: Self) -> Self::Output { self.second / rhs.second } }
293impl<T:FloatingNumber> DivAssign<T> for TimeOf<T> { fn div_assign(&mut self, rhs: T) { self.second.div_assign(rhs) }}
294
295impl<T:FloatingNumber> Mul<T> for TimeOf<T> { type Output=Self; fn mul(self, rhs: T) -> Self::Output { Self::from_internal_unit(self.second.mul(rhs)) }}
296impl<T:FloatingNumber> MulAssign<T> for TimeOf<T> { fn mul_assign(&mut self, rhs: T) { self.second.mul_assign(rhs); }}
297
298impl<T:FloatingNumber> Div<T> for TimeOf<T> { type Output=TimeOf<T>; fn div(self, rhs: T) -> Self::Output { Self::from_internal_unit(self.second.div(rhs)) } }
299impl<T:FloatingNumber> Rem<T> for TimeOf<T> { type Output=TimeOf<T>; fn rem(self, rhs: T) -> Self::Output { Self::from_internal_unit(self.second.rem(rhs)) } }
300impl<T:FloatingNumber> RemAssign<T> for TimeOf<T> { fn rem_assign(&mut self, rhs: T) { self.second.rem_assign(rhs); } }
301
302impl<T:FloatingNumber> Sum for TimeOf<T>
303{
304 fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
305 iter.fold(Self::ZERO, Self::add)
306 }
307}
308
309impl<T:FloatingNumber> MinValue for TimeOf<T> where T : MinValue
310{
311 const MIN : Self = Self::from_s(T::MIN);
312}
313
314impl<T:FloatingNumber> MaxValue for TimeOf<T> where T : MaxValue
315{
316 const MAX : Self = Self::from_s(T::MAX);
317}
318
319
320
321
322#[cfg(feature = "serde")]
323impl<T: FloatingNumber> Serialize for TimeOf<T> where T : Serialize {
324 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,
325 { self.s().serialize(serializer) }
326}
327
328#[cfg(feature = "serde")]
329impl<'de, T: FloatingNumber> Deserialize<'de> for TimeOf<T> where T : Deserialize<'de> {
330 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>,
331 {
332 Ok(Self::from_s(T::deserialize(deserializer)?))
333 }
334}