diff options
author | stuebinm | 2024-09-09 17:24:14 +0200 |
---|---|---|
committer | stuebinm | 2024-09-09 17:24:14 +0200 |
commit | d876202506621eb76012c12cbb0e91fd2bb0ada0 (patch) | |
tree | 0aae96f78f6284fb45c022ceb000a3baaf574a2f | |
parent | 002b7d02d57d52bdb5309092285ea94c4cd68f7e (diff) |
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.
Diffstat (limited to '')
-rw-r--r-- | src/traits.rs | 26 |
1 files changed, 18 insertions, 8 deletions
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::<DateTime<Local>>().unwrap(); + + let format_time = |time: &DateTime<Utc>| -> String { + if time.eq(&epoch) { + "??:??:?? ".to_owned() + } else { // chrono's API for timezones is expressive, but reads like c++ … + <DateTime<Local>>::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++ … - <DateTime<Local>>::from(*t).time().format("%T").to_string() - }) - .unwrap_or("<format error>".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()) { |