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
use super::{TimeFormatter, ASCII_MINUS};
use crate::TimeSpan;
use std::fmt::{Display, Formatter, Result};
pub struct Inner(Option<TimeSpan>);
#[derive(Default)]
pub struct Complete;
impl Complete {
pub fn new() -> Self {
Complete
}
}
impl<'a> TimeFormatter<'a> for Complete {
type Inner = Inner;
fn format<T>(&self, time: T) -> Self::Inner
where
T: Into<Option<TimeSpan>>,
{
Inner(time.into())
}
}
impl Display for Inner {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if let Some(time) = self.0 {
let mut total_seconds = time.total_seconds();
if total_seconds < 0.0 {
total_seconds *= -1.0;
write!(f, "{}", ASCII_MINUS)?;
}
let seconds = total_seconds % 60.0;
let total_minutes = (total_seconds / 60.0) as u64;
let minutes = total_minutes % 60;
let total_hours = total_minutes / 60;
let hours = total_hours % 24;
let days = total_hours / 24;
if days > 0 {
write!(f, "{}.", days)?;
}
write!(f, "{:02}:{:02}:{:010.7}", hours, minutes, seconds)
} else {
write!(f, "00:00:00.0000000")
}
}
}