From 549a625d57952293012fcd5e90b0752776b88769 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 12 Jan 2021 12:49:07 -0800 Subject: [PATCH] Parse times more like what people expect -- 07:30 is 7h30m, not 7m30s --- geom/src/time.rs | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/geom/src/time.rs b/geom/src/time.rs index fa1f56ba07..7cb70f0b38 100644 --- a/geom/src/time.rs +++ b/geom/src/time.rs @@ -102,22 +102,13 @@ impl Time { bail!("Time {}: no :'s", string); } - let mut seconds: f64 = 0.0; - if parts.last().unwrap().contains('.') { - let last_parts: Vec<&str> = parts.last().unwrap().split('.').collect(); - if last_parts.len() != 2 { - bail!("Time {}: no . in last part", string); - } - seconds += last_parts[1].parse::()? / 10.0; - seconds += last_parts[0].parse::()?; - } else { - seconds += parts.last().unwrap().parse::()?; - } - + let mut seconds = parts.last().unwrap().parse::()?; match parts.len() { 1 => Ok(Time::seconds_since_midnight(seconds)), 2 => { - seconds += 60.0 * parts[0].parse::()?; + // They're really minutes + seconds *= 60.0; + seconds += 3600.0 * parts[0].parse::()?; Ok(Time::seconds_since_midnight(seconds)) } 3 => { @@ -213,3 +204,27 @@ impl ops::Sub for Time { Duration::seconds(self.0 - other.0) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse() { + assert_eq!( + Time::START_OF_DAY + Duration::seconds(42.3), + Time::parse("42.3").unwrap() + ); + assert_eq!( + Time::START_OF_DAY + Duration::hours(7) + Duration::minutes(30), + Time::parse("07:30").unwrap() + ); + assert_eq!( + Time::START_OF_DAY + + Duration::hours(7) + + Duration::minutes(30) + + Duration::seconds(5.0), + Time::parse("07:30:05").unwrap() + ); + } +}