From f40128dedfd431e91960e1271f1a677369941058 Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Tue, 2 Apr 2024 17:04:25 +0200 Subject: Convert non-local timestamps to local time zone --- lib/Travel/Routing/DE/HAFAS/Connection.pm | 38 ++++++++++++++--------- lib/Travel/Routing/DE/HAFAS/Connection/Section.pm | 38 ++++++++++++++--------- lib/Travel/Routing/DE/HAFAS/Utils.pm | 10 ++++++ 3 files changed, 58 insertions(+), 28 deletions(-) (limited to 'lib/Travel/Routing/DE/HAFAS') diff --git a/lib/Travel/Routing/DE/HAFAS/Connection.pm b/lib/Travel/Routing/DE/HAFAS/Connection.pm index 5ccf498..05341cc 100644 --- a/lib/Travel/Routing/DE/HAFAS/Connection.pm +++ b/lib/Travel/Routing/DE/HAFAS/Connection.pm @@ -62,20 +62,30 @@ sub new { ); # dProgType/aProgType: CORRECTED oder PROGNOSED - my $sched_dep = $connection->{dep}{dTimeS}; - my $rt_dep = $connection->{dep}{dTimeR}; - my $sched_arr = $connection->{arr}{aTimeS}; - my $rt_arr = $connection->{arr}{aTimeR}; - - for my $ts ( $sched_dep, $rt_dep, $sched_arr, $rt_arr ) { - if ($ts) { - $ts = handle_day_change( - date => $date, - time => $ts, - strp_obj => $strptime, - ); - } - } + my $sched_dep = handle_day_change( + date => $date, + time => $connection->{dep}{dTimeS}, + offset => $connection->{dep}{dTZOffset}, + strp_obj => $strptime, + ); + my $rt_dep = handle_day_change( + date => $date, + time => $connection->{dep}{dTimeR}, + offset => $connection->{dep}{dTZOffset}, + strp_obj => $strptime, + ); + my $sched_arr = handle_day_change( + date => $date, + time => $connection->{arr}{aTimeS}, + offset => $connection->{arr}{aTZOffset}, + strp_obj => $strptime, + ); + my $rt_arr = handle_day_change( + date => $date, + time => $connection->{arr}{aTimeR}, + offset => $connection->{arr}{aTZOffset}, + strp_obj => $strptime, + ); my @sections; for my $sec (@secL) { diff --git a/lib/Travel/Routing/DE/HAFAS/Connection/Section.pm b/lib/Travel/Routing/DE/HAFAS/Connection/Section.pm index 552401b..09032af 100644 --- a/lib/Travel/Routing/DE/HAFAS/Connection/Section.pm +++ b/lib/Travel/Routing/DE/HAFAS/Connection/Section.pm @@ -58,20 +58,30 @@ sub new { time_zone => 'Europe/Berlin' ); - my $sched_dep = $sec->{dep}{dTimeS}; - my $rt_dep = $sec->{dep}{dTimeR}; - my $sched_arr = $sec->{arr}{aTimeS}; - my $rt_arr = $sec->{arr}{aTimeR}; - - for my $ts ( $sched_dep, $rt_dep, $sched_arr, $rt_arr ) { - if ($ts) { - $ts = handle_day_change( - date => $date, - time => $ts, - strp_obj => $strptime, - ); - } - } + my $sched_dep = handle_day_change( + date => $date, + time => $sec->{dep}{dTimeS}, + offset => $sec->{dep}{dTZOffset}, + strp_obj => $strptime, + ); + my $rt_dep = handle_day_change( + date => $date, + time => $sec->{dep}{dTimeR}, + offset => $sec->{dep}{dTZOffset}, + strp_obj => $strptime, + ); + my $sched_arr = handle_day_change( + date => $date, + time => $sec->{arr}{aTimeS}, + offset => $sec->{arr}{aTZOffset}, + strp_obj => $strptime, + ); + my $rt_arr = handle_day_change( + date => $date, + time => $sec->{arr}{aTimeR}, + offset => $sec->{arr}{aTZOffset}, + strp_obj => $strptime, + ); my $tco = {}; for my $tco_id ( @{ $sec->{jny}{dTrnCmpSX}{tcocX} // [] } ) { diff --git a/lib/Travel/Routing/DE/HAFAS/Utils.pm b/lib/Travel/Routing/DE/HAFAS/Utils.pm index 5b26555..45ee5db 100644 --- a/lib/Travel/Routing/DE/HAFAS/Utils.pm +++ b/lib/Travel/Routing/DE/HAFAS/Utils.pm @@ -13,8 +13,13 @@ sub handle_day_change { my (%opt) = @_; my $datestr = $opt{date}; my $timestr = $opt{time}; + my $tz_offset = $opt{offset}; my $offset_days = 0; + if ( not defined $timestr ) { + return; + } + # timestr may include a day offset, resulting in DDHHMMSS if ( length($timestr) == 8 ) { $offset_days = substr( $timestr, 0, 2, q{} ); @@ -26,6 +31,11 @@ sub handle_day_change { $ts->add( days => $offset_days ); } + if ( defined $tz_offset and $tz_offset != $ts->offset / 60 ) { + my $ts_offset = $tz_offset - $ts->offset / 60; + $ts->subtract( minutes => $ts_offset ); + } + return $ts; } -- cgit v1.2.3