rmv-core 1.0.0

Configuration, retries, stop resolution and connection status for the RMV API
Documentation
//! Times of the API. RMV gives dates and times in the local time of
//! Frankfurt, without an offset; the output is RFC 3339 with the offset of
//! `Europe/Berlin`, like `2026-11-28T08:44:00+01:00`.

use jiff::Timestamp;
use jiff::civil::{Date, DateTime, Time};
use jiff::tz::TimeZone;

/// The time zone of the RMV area.
pub const ZONE: &str = "Europe/Berlin";

fn zone() -> TimeZone {
    // The database is bundled, so the zone is always there.
    TimeZone::get(ZONE).unwrap_or(TimeZone::UTC)
}

/// A date (`2026-11-28`) and a time (`08:44:00` or `08:44`) of the API as
/// an instant. At the change from summer time, a time that occurs twice is
/// the first one.
pub(crate) fn parse(date: &str, time: &str) -> Option<Timestamp> {
    let date: Date = date.trim().parse().ok()?;
    let time: Time = time.trim().parse().ok()?;
    DateTime::from_parts(date, time)
        .to_zoned_datetime(zone())
        .ok()
        .map(|zoned| zoned.timestamp())
}

/// The date and time (`HH:MM`) to send for an instant.
pub(crate) fn request(time: Timestamp) -> (String, String) {
    let local = time.to_zoned_datetime(zone());
    (
        local.strftime("%Y-%m-%d").to_string(),
        local.strftime("%H:%M").to_string(),
    )
}

/// RFC 3339 with the offset of `Europe/Berlin`.
pub fn format(time: Timestamp) -> String {
    time.to_zoned_datetime(zone())
        .strftime("%Y-%m-%dT%H:%M:%S%:z")
        .to_string()
}

/// The `planRtTs` of a response, like `2026-11-28T08:44:05.000+01:00`, in
/// the form of [`format`].
pub(crate) fn plan_rt(text: Option<&str>) -> Option<String> {
    text?.trim().parse::<Timestamp>().ok().map(format)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn at(text: &str) -> Timestamp {
        text.parse().unwrap()
    }

    #[test]
    fn reads_local_times() {
        assert_eq!(
            parse("2026-11-28", "08:44:00"),
            Some(at("2026-11-28T07:44:00Z"))
        );
        assert_eq!(
            parse("2026-07-01", "08:44"),
            Some(at("2026-07-01T06:44:00Z"))
        );
        assert_eq!(parse("2026-11-28", "8 Uhr"), None);
        assert_eq!(parse("morgen", "08:44"), None);
    }

    #[test]
    fn handles_the_changes_of_summer_time() {
        // 02:30 does not exist on 2026-03-29 and is taken as 03:30.
        assert_eq!(
            parse("2026-03-29", "02:30"),
            Some(at("2026-03-29T01:30:00Z"))
        );
        // 02:30 occurs twice on 2026-10-25; the first one counts.
        assert_eq!(
            parse("2026-10-25", "02:30"),
            Some(at("2026-10-25T00:30:00Z"))
        );
    }

    #[test]
    fn formats_in_the_local_zone() {
        assert_eq!(
            format(at("2026-11-28T07:44:00Z")),
            "2026-11-28T08:44:00+01:00"
        );
        assert_eq!(
            format(at("2026-07-01T06:44:00Z")),
            "2026-07-01T08:44:00+02:00"
        );
        assert_eq!(
            request(at("2026-11-28T23:30:59Z")),
            ("2026-11-29".to_owned(), "00:30".to_owned())
        );
    }

    #[test]
    fn reads_the_time_of_the_realtime_data() {
        assert_eq!(
            plan_rt(Some("2026-11-28T08:44:05.000+01:00")).as_deref(),
            Some("2026-11-28T08:44:05+01:00")
        );
        assert_eq!(plan_rt(Some("soon")), None);
        assert_eq!(plan_rt(None), None);
    }
}