Skip to main content

rmv_core/
time.rs

1//! Times of the API. RMV gives dates and times in the local time of
2//! Frankfurt, without an offset; the output is RFC 3339 with the offset of
3//! `Europe/Berlin`, like `2026-11-28T08:44:00+01:00`.
4
5use jiff::Timestamp;
6use jiff::civil::{Date, DateTime, Time};
7use jiff::tz::TimeZone;
8
9/// The time zone of the RMV area.
10pub const ZONE: &str = "Europe/Berlin";
11
12fn zone() -> TimeZone {
13    // The database is bundled, so the zone is always there.
14    TimeZone::get(ZONE).unwrap_or(TimeZone::UTC)
15}
16
17/// A date (`2026-11-28`) and a time (`08:44:00` or `08:44`) of the API as
18/// an instant. At the change from summer time, a time that occurs twice is
19/// the first one.
20pub(crate) fn parse(date: &str, time: &str) -> Option<Timestamp> {
21    let date: Date = date.trim().parse().ok()?;
22    let time: Time = time.trim().parse().ok()?;
23    DateTime::from_parts(date, time)
24        .to_zoned_datetime(zone())
25        .ok()
26        .map(|zoned| zoned.timestamp())
27}
28
29/// The date and time (`HH:MM`) to send for an instant.
30pub(crate) fn request(time: Timestamp) -> (String, String) {
31    let local = time.to_zoned_datetime(zone());
32    (
33        local.strftime("%Y-%m-%d").to_string(),
34        local.strftime("%H:%M").to_string(),
35    )
36}
37
38/// RFC 3339 with the offset of `Europe/Berlin`.
39pub fn format(time: Timestamp) -> String {
40    time.to_zoned_datetime(zone())
41        .strftime("%Y-%m-%dT%H:%M:%S%:z")
42        .to_string()
43}
44
45/// The `planRtTs` of a response, like `2026-11-28T08:44:05.000+01:00`, in
46/// the form of [`format`].
47pub(crate) fn plan_rt(text: Option<&str>) -> Option<String> {
48    text?.trim().parse::<Timestamp>().ok().map(format)
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    fn at(text: &str) -> Timestamp {
56        text.parse().unwrap()
57    }
58
59    #[test]
60    fn reads_local_times() {
61        assert_eq!(
62            parse("2026-11-28", "08:44:00"),
63            Some(at("2026-11-28T07:44:00Z"))
64        );
65        assert_eq!(
66            parse("2026-07-01", "08:44"),
67            Some(at("2026-07-01T06:44:00Z"))
68        );
69        assert_eq!(parse("2026-11-28", "8 Uhr"), None);
70        assert_eq!(parse("morgen", "08:44"), None);
71    }
72
73    #[test]
74    fn handles_the_changes_of_summer_time() {
75        // 02:30 does not exist on 2026-03-29 and is taken as 03:30.
76        assert_eq!(
77            parse("2026-03-29", "02:30"),
78            Some(at("2026-03-29T01:30:00Z"))
79        );
80        // 02:30 occurs twice on 2026-10-25; the first one counts.
81        assert_eq!(
82            parse("2026-10-25", "02:30"),
83            Some(at("2026-10-25T00:30:00Z"))
84        );
85    }
86
87    #[test]
88    fn formats_in_the_local_zone() {
89        assert_eq!(
90            format(at("2026-11-28T07:44:00Z")),
91            "2026-11-28T08:44:00+01:00"
92        );
93        assert_eq!(
94            format(at("2026-07-01T06:44:00Z")),
95            "2026-07-01T08:44:00+02:00"
96        );
97        assert_eq!(
98            request(at("2026-11-28T23:30:59Z")),
99            ("2026-11-29".to_owned(), "00:30".to_owned())
100        );
101    }
102
103    #[test]
104    fn reads_the_time_of_the_realtime_data() {
105        assert_eq!(
106            plan_rt(Some("2026-11-28T08:44:05.000+01:00")).as_deref(),
107            Some("2026-11-28T08:44:05+01:00")
108        );
109        assert_eq!(plan_rt(Some("soon")), None);
110        assert_eq!(plan_rt(None), None);
111    }
112}