openhab / openhab-addons

Add-ons for openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.85k stars 3.56k forks source link

[deutscheBahn] Binding is not aware of time zones #12354

Open OnkelMato opened 2 years ago

OnkelMato commented 2 years ago

Expected Behavior

The Deutsche Bahn widget should show the next 4 upcoming trains.

Current Behavior

The thing, model, and items were created as described in the documentation. After adding the widget, it shows the next/last trains from the previous hour. At my favourite station are exactly 4 trains per hour, so it shows exactly the trains from the last 60 minutes.

Possible Solution

Make the binding aware of timezones and query the REST service with local time, not UTC.

Steps to Reproduce (for Bugs)

1 Install a Ubuntu-based system with NTP enabled and set the timezone to Europe/Berlin. 2 UTC is one hour behind the local time 3 Create widget according to documentation 4 Widget shows wrong training (next four trains one hour ago)

  root@openhab:~# timedatectl
                 Local time: Wed 2022-02-23 14:26:44 CET
             Universal time: Wed 2022-02-23 13:26:44 UTC
                   RTC time: n/a
                  Time zone: Europe/Berlin (CET, +0100)
  System clock synchronized: yes
                NTP service: active
            RTC in local TZ: no

To verify the bug

5 Turn off NTP timedatectl set-ntp false 6 Set time to "local time" timedatectl set-time 14:33

root@openhab:~# timedatectl
               Local time: Wed 2022-02-23 14:45:46 UTC
           Universal time: Wed 2022-02-23 14:45:46 UTC
                 RTC time: n/a
                Time zone: UTC (UTC, +0000)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

Open UI and verify, it shows the correct next four trains.

Context

Your Environment

lsiepel commented 6 months ago

Not that easy to find, dates are not my favorite objects to work with in Java. But i did some digging and have found some key aspects that might relate to this issue. Maybe @soenkekueper has some time to spare, maybe he also has some thoughts on this.

Notes to self: Here is a ZonedDateTime created with system timezone, would have expected openHAB TimeZone https://github.com/openhab/openhab-addons/blob/099dc692c87405a2e6d59e56c55cbc9d68ce2afb/bundles/org.openhab.binding.deutschebahn/src/main/java/org/openhab/binding/deutschebahn/internal/EventAttribute.java#L300

Here is a Date created that is used all over de binding. If i understand correct, Date is like instant a point in time UTC and independent from the system locale. https://github.com/openhab/openhab-addons/blob/099dc692c87405a2e6d59e56c55cbc9d68ce2afb/bundles/org.openhab.binding.deutschebahn/src/main/java/org/openhab/binding/deutschebahn/internal/DeutscheBahnHandlerFactory.java#L53

These to SimpleDateFormatters are used to format the Date for API-calls. I expect these to take the system timezone into account. https://github.com/openhab/openhab-addons/blob/099dc692c87405a2e6d59e56c55cbc9d68ce2afb/bundles/org.openhab.binding.deutschebahn/src/main/java/org/openhab/binding/deutschebahn/internal/timetable/TimetablesV1Impl.java#L78-L79

Forcing the timezone in the constructor might also fix this.

        // unfortunately the SimpleDateFormat does not have a constructor with timezone, set it in TimetablesV1Impl constructor
        DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("Europ/Berlin"));
        HOUR_FORMAT.setTimeZone(TimeZone.getTimeZone("Europ/Berlin"));