Skip to content

Commit

Permalink
PB-1319: correct format for timestamps used in OJPStopEventRequest
Browse files Browse the repository at this point in the history
Accodring to https://opentransportdata.swiss/de/cookbook/ojpstopeventrequest/ the timestamps
used in OJPStopEventRequests should be in Zulu time, to prevent their code from trying
to interpret the given times as local times.
This is now implemented correctly and a few test for timestamp conversions are added.
  • Loading branch information
hansmannj committed Jan 7, 2025
1 parent 61cca21 commit 3e0ca19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 16 additions & 3 deletions chsdi/lib/opentransapi/opentransapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import requests
import xml.etree.ElementTree as et
from pytz import timezone
from pytz import UTC
from datetime import datetime
from dateutil.parser import isoparse
import re
Expand All @@ -19,8 +20,12 @@ def format_time(str_date_time):
# fractional seconds, e.g. 7 instead of 6, should be handled, too
date_time = isoparse(str_date_time)

# Convert to local timezone explicitly
local_tz = timezone('Europe/Zurich') # Replace with your local timezone
local_date_time = date_time.astimezone(local_tz)

# Return time in local time, as needed.
return date_time.strftime('%d/%m/%Y %H:%M')
return local_date_time.strftime('%d/%m/%Y %H:%M')


class OpenTrans:
Expand All @@ -32,9 +37,13 @@ def __init__(self, open_trans_api_key, open_trans_url):

def get_departures(self, station_id, number_results=5, request_dt_time=False):
if not request_dt_time:
request_dt_time = datetime.now(timezone('Europe/Zurich')).strftime('%Y-%m-%dT%H:%M:%S')
# Note: according to https://opentransportdata.swiss/de/cookbook/ojpstopeventrequest/
# the timestamps used in the OJPStopEventRequest should preferably be in Zulu time
# and MUST include the seconds, in order to prevent their code from trying to interpret
# the given times as a form of local time!
request_dt_time = datetime.now(UTC).strftime('%Y-%m-%dT%H:%M:%SZ')
self.station_id = station_id
api_response_xml = self.send_post(station_id, request_dt_time, number_results) # request_dt_time in format 2017-12-11T14:26:18Z
api_response_xml = self.send_post(station_id, request_dt_time, number_results) # zulu times!
results = self.xml_to_array(api_response_xml)
return results

Expand Down Expand Up @@ -86,6 +95,10 @@ def create_ojp_payload(self, station_id, request_dt_time, number_results=5):
# ATTENTION: The value "swisstopo_Abfahrtsmonitor" for the RequestorRef
# in the payload below is suggested by the OJP product owner.
# Hence it MUST NOT be changed!

# Further ATTENTION:
# Timestamps used for RequestTimestamp and DepArrTime MUST be in Zulu time, see:
# https://opentransportdata.swiss/de/cookbook/ojpstopeventrequest/
payload = f"""<?xml version="1.0" encoding="UTF-8"?>
<OJP xmlns='http://www.vdv.de/ojp' xmlns:siri='http://www.siri.org.uk/siri' version='2.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.vdv.de/ojp ../../../../OJP4/OJP.xsd'>
<OJPRequest>
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/test_opentransapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ def test_stationboard(self, mock_requests):
self.assertEqual(results[0]["destinationName"], "Hogwarts")
self.assertEqual(results[0]["destinationId"], "ch:1:sloid:91178::3")

def test_format_time(self):
# assert, that several timestamp formats are correctly handled and transformed into the
# correct local time
self.assertEqual(format_time("2024-11-19T08:52:00Z"), "19/11/2024 08:52")
self.assertEqual(format_time("2024-11-19T08:52:00.1234567"), "19/11/2024 08:52")
self.assertEqual(format_time("2024-11-19T08:52:00Z"), "19/11/2024 09:52")
self.assertEqual(format_time("2024-11-19T09:52:00.123456789"), "19/11/2024 09:52")
self.assertEqual(format_time("2024-11-19T08:52:00.123456789Z"), "19/11/2024 09:52")
self.assertEqual(format_time("2024-11-19T08:52:00+01:00"), "19/11/2024 08:52")

@requests_mock.Mocker()
Expand Down

0 comments on commit 3e0ca19

Please sign in to comment.