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
use std::{fmt, str::FromStr};

use crate::util::parse_error::ParseError;

/// Represents a specific time of day with second precision.
///
/// Does not contain any timezone information as the IGC specification mandates UTC everywhere.
#[derive(Debug, PartialEq, Eq)]
pub struct Time {
    pub seconds: u8,
    pub minutes: u8,
    pub hours: u8,
}

impl Time {
    /// Parse a time string of the form "HHMMSS"
    pub fn parse(time_string: &str) -> Result<Self, ParseError> {
        assert_eq!(time_string.len(), 6);

        if !time_string.is_ascii() {
            return Err(ParseError::NonASCIICharacters);
        }

        let hours = time_string[0..2].parse::<u8>()?;
        let minutes = time_string[2..4].parse::<u8>()?;
        let seconds = time_string[4..6].parse::<u8>()?;

        if hours > 24 || minutes > 60 || seconds > 60 {
            Err(ParseError::NumberOutOfRange)
        } else {
            Ok(Time {
                hours,
                minutes,
                seconds,
            })
        }
    }

    /// Helper method to create a Time from a (hour, minute, second) triplet.
    pub fn from_hms(hours: u8, minutes: u8, seconds: u8) -> Time {
        assert!(hours <= 24);
        assert!(minutes <= 60);
        assert!(seconds <= 60);

        Time {
            hours,
            minutes,
            seconds,
        }
    }

    /// Helper method to get the seconds since midnight.
    pub fn seconds_since_midnight(&self) -> u32 {
        let mins: u32 = u32::from(self.hours) * 60 + u32::from(self.minutes);
        mins * 60 + u32::from(self.seconds)
    }
}

impl FromStr for Time {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, ParseError> {
        Self::parse(s)
    }
}

impl fmt::Display for Time {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:02}{:02}{:02}", self.hours, self.minutes, self.seconds)
    }
}

/// Represents a single Gregorian calendar day
#[derive(Debug, PartialEq, Eq)]
pub struct Date {
    /// In the range [1, 31]
    pub day: u8,

    /// In the range [1, 12]
    pub month: u8,

    /// Only the least significant two digits of the year. In the range [0, 99]
    pub year: u8,
}

impl Date {
    /// Parses a date string of the form "DDMMYY"
    /// There are not enough digits for the year in this format (bytes are expensive, yo).
    pub fn parse(date_string: &str) -> Result<Self, ParseError> {
        assert_eq!(date_string.len(), 6);

        if !date_string.is_ascii() {
            return Err(ParseError::NonASCIICharacters);
        }

        let day = date_string[0..2].parse::<u8>()?;
        let month = date_string[2..4].parse::<u8>()?;
        let year = date_string[4..6].parse::<u8>()?;

        if day > 31 || month > 12 {
            Err(ParseError::NumberOutOfRange)
        } else {
            Ok(Date { day, month, year })
        }
    }

    /// Helper method to create a Date from a (day, month, year) triplet
    pub fn from_dmy(day: u8, month: u8, year: u8) -> Date {
        assert!(day >= 1 && day <= 31);
        assert!(month >= 1 && month <= 12);
        assert!(year <= 99);
        Date { day, month, year }
    }
}

impl FromStr for Date {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, ParseError> {
        Self::parse(s)
    }
}

impl fmt::Display for Date {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:02}{:02}{:02}", self.day, self.month, self.year)
    }
}

#[cfg(test)]
mod test {
    use super::{Date, Time};

    #[test]
    fn time_parse() {
        assert_eq!("012345".parse::<Time>().unwrap(), Time::from_hms(1, 23, 45));
        assert_eq!(
            "152136".parse::<Time>().unwrap(),
            Time::from_hms(15, 21, 36)
        );
    }

    #[test]
    fn time_parse_with_invalid_char_boundary() {
        assert!(Time::parse("🌀aa").is_err());
    }

    #[test]
    fn time_fmt() {
        assert_eq!(format!("{}", Time::from_hms(1, 23, 45)), "012345");
    }

    #[test]
    fn time_seconds_since_midnight() {
        assert_eq!(
            Time {
                hours: 0,
                minutes: 0,
                seconds: 0
            }
            .seconds_since_midnight(),
            0
        );
        assert_eq!(
            Time {
                hours: 1,
                minutes: 2,
                seconds: 3
            }
            .seconds_since_midnight(),
            3600 + 120 + 3
        );
    }

    #[test]
    fn date_parse() {
        assert_eq!("010118".parse::<Date>().unwrap(), Date::from_dmy(1, 1, 18));
        assert_eq!("120757".parse::<Date>().unwrap(), Date::from_dmy(12, 7, 57));
    }

    #[test]
    fn date_parse_with_invalid_char_boundary() {
        assert!(Date::parse("🌀aa").is_err());
    }

    #[test]
    fn date_fmt() {
        assert_eq!(format!("{}", Date::from_dmy(5, 10, 18)), "051018");
    }

    proptest! {
        #[test]
        #[allow(unused_must_use)]
        fn time_parse_back_to_original(h in 0u8..24, m in 0u8..60, s in 0u8..60) {
            let time = Time::from_hms(h, m, s);
            prop_assert_eq!(Time::parse(&format!("{}", time)).unwrap(), time);
        }

        #[test]
        #[allow(unused_must_use)]
        fn date_parse_back_to_original(d in 1u8..32, m in 1u8..13, y in 0u8..100) {
            let date = Date::from_dmy(d, m, y);
            prop_assert_eq!(Date::parse(&format!("{}", date)).unwrap(), date);
        }
    }
}