From d876202506621eb76012c12cbb0e91fd2bb0ada0 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Mon, 9 Sep 2024 17:24:14 +0200 Subject: print less formatting errors in times these used to go wrong in case there was no real-time info on departure times. Instead traveltext now falls back on scheduled departure times if no real-time info is available. These still fail sometimes (at exit-only stops both appear to be unset on the oebb hafas), but it's a lot better than what was before. Also I've observed travelynx returning actual null values for missing time stamps now, so the old hack of checking if timestamp = unix epoch and considering that "unknown" might no longer be necessary, but I'm keeping it around just in case. --- src/traits.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/traits.rs b/src/traits.rs index b4b74e7..b5785be 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -9,19 +9,29 @@ pub trait IsStation { fn ds100(&self) -> Option<&str>; fn to_fancy_string(&self) -> String { - // travelynx literally sends this entire precise date in case of an - // unknown time instead of, like, a null + // travelynx used to send this entire precise date in case of an + // unknown time instead of null. I'm not sure it still does this + // (i have since seen it return null) but keeping this here just + // in case let epoch = "1970-01-01T00:00:00Z".parse::>().unwrap(); + + let format_time = |time: &DateTime| -> String { + if time.eq(&epoch) { + "??:??:?? ".to_owned() + } else { // chrono's API for timezones is expressive, but reads like c++ … + >::from(*time).time().format("%T").to_string() + } + }; + format!( "{} {} – {} ({})", self .real_arrival() - .map(|t| if t.eq(&epoch) { - "unknown ".to_owned() - } else { // chrono's API for timezones is expressive, but reads like c++ … - >::from(*t).time().format("%T").to_string() - }) - .unwrap_or("".to_string()) + .map(format_time) + .unwrap_or_else(|| + self.scheduled_arrival() + .map(format_time).unwrap_or_else(|| "??:??:??".to_string()) + ) .blue(), { let delay = match (self.real_arrival(), self.scheduled_arrival()) { -- cgit v1.2.3