Skip to content

Commit

Permalink
hotfix: resolve timezone issues (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsarrco authored Oct 14, 2023
2 parents 2b17d43 + cdf5df3 commit 92ec352
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
5 changes: 1 addition & 4 deletions MuoVErsi/sources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,8 @@ def upload_trip_stop_time_to_postgres(self, stop_time: TripStopTime):
stop_time_db = self.session.query(StopTime).filter_by(trip_id=train.id, stop_id=stop_id).first()

if stop_time_db:
if stop_time_db.platform != stop_time.platform or stop_time_db.sched_arr_dt != stop_time.arr_time or \
stop_time_db.sched_dep_dt != stop_time.dep_time:
if stop_time_db.platform != stop_time.platform:
stop_time_db.platform = stop_time.platform
stop_time_db.sched_arr_dt = stop_time.arr_time
stop_time_db.sched_dep_dt = stop_time.dep_time
self.session.commit()
else:
new_stop_time = StopTime(trip_id=train.id, stop_id=stop_id, sched_arr_dt=stop_time.arr_time,
Expand Down
19 changes: 12 additions & 7 deletions MuoVErsi/sources/trenitalia.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from datetime import datetime, timedelta, date
from urllib.parse import quote
from pytz import timezone

import requests
from sqlalchemy import and_, select
Expand All @@ -16,6 +17,8 @@
)
logger = logging.getLogger(__name__)

rome_tz = timezone('Europe/Rome')


class TrenitaliaRoute(Route):
pass
Expand Down Expand Up @@ -64,7 +67,7 @@ def save_data(self):
self.sync_stations_db(stations)

def get_stop_times_from_station(self, station) -> list[TripStopTime]:
now = datetime.now()
now = datetime.now(rome_tz)
departures = self.loop_get_times(10000, station, now, type='partenze')
arrivals = self.loop_get_times(10000, station, now, type='arrivi')

Expand Down Expand Up @@ -134,9 +137,11 @@ def loop_get_times(self, limit, stop: Station, dt, train_ids=None, type='partenz

def get_stop_times_from_start_dt(self, type, stop: Station, start_dt: datetime, train_ids: list[int] | None) -> \
list[TripStopTime]:
is_dst = start_dt.astimezone().dst() != timedelta(0)
date = (start_dt - timedelta(hours=(1 if is_dst else 0))).strftime("%a %b %d %Y %H:%M:%S GMT+0100")
url = f'http://www.viaggiatreno.it/infomobilita/resteasy/viaggiatreno/{type}/{stop.id}/{quote(date)}'
num_offset = start_dt.strftime('%z')
sc_num_offset = f'{num_offset[:3]}:{num_offset[3:]}'
url_dt = start_dt.strftime('%a %b %d %Y %H:%M:%S GMT') + f'{num_offset} (GMT{sc_num_offset})'
url_dt = url_dt.replace(' ', '%20')
url = f'http://www.viaggiatreno.it/infomobilita/resteasy/viaggiatreno/{type}/{stop.id}/{url_dt}'
r = requests.get(url)
if r.status_code != 200:
return []
Expand All @@ -153,7 +158,7 @@ def get_stop_times_from_start_dt(self, type, stop: Station, start_dt: datetime,
continue

try:
dep_time = datetime.fromtimestamp(departure['orarioPartenza'] / 1000) if departure[
dep_time = datetime.fromtimestamp(departure['orarioPartenza'] / 1000, tz=rome_tz) if departure[
'orarioPartenza'] else None
except ValueError:
dep_time = None
Expand All @@ -163,7 +168,7 @@ def get_stop_times_from_start_dt(self, type, stop: Station, start_dt: datetime,
continue

try:
arr_time = datetime.fromtimestamp(departure['orarioArrivo'] / 1000) if departure[
arr_time = datetime.fromtimestamp(departure['orarioArrivo'] / 1000, tz=rome_tz) if departure[
'orarioArrivo'] else None
except ValueError:
arr_time = None
Expand All @@ -183,7 +188,7 @@ def get_stop_times_from_start_dt(self, type, stop: Station, start_dt: datetime,
if departure[f'binarioEffettivo{type_text}Descrizione'] != '':
platform = departure[f'binarioEffettivo{type_text}Descrizione']

orig_dep_date = datetime.fromtimestamp(departure['dataPartenzaTreno'] / 1000).date() if departure[
orig_dep_date = datetime.fromtimestamp(departure['dataPartenzaTreno'] / 1000, tz=rome_tz).date() if departure[
'dataPartenzaTreno'] else None
origin_id = departure['codOrigine']
destination = departure.get('destinazione')
Expand Down

0 comments on commit 92ec352

Please sign in to comment.