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
//! The timing module provides a pair of Time Formatters that splits up the
//! visualized time into the main part of the time and the fractional part. This
//! is the Time Formatter pair used by the Timer Component.

use super::{
    extract_hundredths, extract_milliseconds, extract_tenths, Accuracy, DigitsFormat,
    TimeFormatter, MINUS,
};
use crate::TimeSpan;
use std::fmt::{Display, Formatter, Result};

/// A Time Span to be formatted as the main part of the Time Formatter Pair.
pub struct TimeInner {
    time: Option<TimeSpan>,
    digits_format: DigitsFormat,
}

/// The Time Formatter that visualizes the main part of the Time Formatter Pair
/// for the Timer Component. This Time Formatter shows no fractional part and
/// prefixes as many zeros as you want. By default no zeros are used as a prefix.
///
/// # Example Formatting
///
/// * Empty Time `0`
/// * Seconds `23`
/// * Minutes `12:34`
/// * Hours `12:34:56`
/// * Negative Times `−23`
pub struct Time {
    digits_format: DigitsFormat,
}

impl Time {
    /// Creates a new default Time Formatter that doesn't prefix any zeros.
    pub fn new() -> Self {
        Default::default()
    }

    /// Creates a new Time Formatter that uses the digits format specified to
    /// determine how many digits to always show. Zeros are prefixed to fill up
    /// the missing digits.
    pub fn with_digits_format(digits_format: DigitsFormat) -> Self {
        Time { digits_format }
    }
}

impl Default for Time {
    fn default() -> Self {
        Time {
            digits_format: DigitsFormat::SingleDigitSeconds,
        }
    }
}

impl<'a> TimeFormatter<'a> for Time {
    type Inner = TimeInner;

    fn format<T>(&self, time: T) -> Self::Inner
    where
        T: Into<Option<TimeSpan>>,
    {
        TimeInner {
            time: time.into(),
            digits_format: self.digits_format,
        }
    }
}

impl Display for TimeInner {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        if let Some(time) = self.time {
            let mut total_seconds = time.total_seconds();
            if total_seconds < 0.0 {
                total_seconds *= -1.0;
                write!(f, "{}", MINUS)?;
            }
            let seconds = (total_seconds % 60.0) as u8;
            let total_minutes = (total_seconds / 60.0) as u64;
            let minutes = total_minutes % 60;
            let hours = total_minutes / 60;
            if self.digits_format == DigitsFormat::DoubleDigitHours {
                write!(f, "{:02}:{:02}:{:02}", hours, minutes, seconds)
            } else if hours > 0 || self.digits_format == DigitsFormat::SingleDigitHours {
                write!(f, "{}:{:02}:{:02}", hours, minutes, seconds)
            } else if self.digits_format == DigitsFormat::DoubleDigitMinutes {
                write!(f, "{:02}:{:02}", minutes, seconds)
            } else if total_minutes > 0 || self.digits_format == DigitsFormat::SingleDigitMinutes {
                write!(f, "{}:{:02}", minutes, seconds)
            } else if self.digits_format == DigitsFormat::DoubleDigitSeconds {
                write!(f, "{:02}", seconds)
            } else {
                write!(f, "{}", seconds)
            }
        } else {
            match self.digits_format {
                DigitsFormat::SingleDigitSeconds => write!(f, "0"),
                DigitsFormat::DoubleDigitSeconds => write!(f, "00"),
                DigitsFormat::SingleDigitMinutes => write!(f, "0:00"),
                DigitsFormat::DoubleDigitMinutes => write!(f, "00:00"),
                DigitsFormat::SingleDigitHours => write!(f, "0:00:00"),
                DigitsFormat::DoubleDigitHours => write!(f, "00:00:00"),
            }
        }
    }
}

/// A Time Span to be formatted as the fractional part of the Time Formatter
/// Pair.
pub struct FractionInner {
    time: Option<TimeSpan>,
    accuracy: Accuracy,
}

/// The Time Formatter that visualizes the fractional part of the Time Formatter
/// Pair for the Timer Component. This Time Formatter shows only the fractional
/// part of the time and uses as many digits for it as you want. By default 2
/// digits of the fractional part are shown.
///
/// # Example Formatting
///
/// * Empty Time `.00`
/// * No Fractional Part `​`
/// * Tenths `.1`
/// * Hundredths `.12`
pub struct Fraction {
    accuracy: Accuracy,
}

impl Fraction {
    /// Creates a new default Time Formatter that uses hundredths for showing
    /// the fractional part.
    pub fn new() -> Self {
        Default::default()
    }

    /// Creates a new Time Formatter that uses the accuracy provided for showing
    /// the fractional part.
    pub fn with_accuracy(accuracy: Accuracy) -> Self {
        Fraction { accuracy }
    }
}

impl Default for Fraction {
    fn default() -> Self {
        Fraction {
            accuracy: Accuracy::Hundredths,
        }
    }
}

impl<'a> TimeFormatter<'a> for Fraction {
    type Inner = FractionInner;

    fn format<T>(&self, time: T) -> Self::Inner
    where
        T: Into<Option<TimeSpan>>,
    {
        FractionInner {
            time: time.into(),
            accuracy: self.accuracy,
        }
    }
}

impl Display for FractionInner {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        if let Some(time) = self.time {
            match self.accuracy {
                Accuracy::Seconds => Ok(()),
                Accuracy::Tenths => write!(f, ".{}", extract_tenths(time.total_seconds())),
                Accuracy::Hundredths => {
                    write!(f, ".{:02}", extract_hundredths(time.total_seconds()))
                }
                Accuracy::Milliseconds => {
                    write!(f, ".{:03}", extract_milliseconds(time.total_seconds()))
                }
            }
        } else {
            match self.accuracy {
                Accuracy::Seconds => Ok(()),
                Accuracy::Tenths => write!(f, ".0"),
                Accuracy::Hundredths => write!(f, ".00"),
                Accuracy::Milliseconds => write!(f, ".000"),
            }
        }
    }
}

#[test]
fn test() {
    let time = "4:20.999999".parse::<TimeSpan>().unwrap();
    assert_eq!(Fraction::new().format(Some(time)).to_string(), ".99");
}