From 139f52fbda84da76cb2a8ee5e878e41b6bfe70f8 Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Tue, 22 Aug 2023 00:23:36 +0200 Subject: Fix datetime calculation when crossing midnight after midnight Previously, this module generated timestamps that were 24h in the future whenever requesting a journey that crosses midnight after midnight. --- lib/Travel/Status/DE/HAFAS/Journey.pm | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/Travel/Status/DE/HAFAS/Journey.pm b/lib/Travel/Status/DE/HAFAS/Journey.pm index 9dd5561..952a990 100644 --- a/lib/Travel/Status/DE/HAFAS/Journey.pm +++ b/lib/Travel/Status/DE/HAFAS/Journey.pm @@ -9,6 +9,7 @@ use 5.014; no if $] >= 5.018, warnings => 'experimental::smartmatch'; use parent 'Class::Accessor'; +use DateTime::Format::Strptime; use List::Util qw(any); use Travel::Status::DE::HAFAS::Stop; @@ -88,6 +89,12 @@ sub new { } } + my $date_ref = ( split( qr{[|]}, $jid ) )[4]; + my $datetime_ref = DateTime::Format::Strptime->new( + pattern => '%d%m%Y', + time_zone => 'Europe/Berlin' + )->parse_datetime($date_ref); + my $class = $product->{cls}; my @stops; @@ -114,7 +121,7 @@ sub new { input => $timestr, date => $date, strp_obj => $hafas->{strptime_obj}, - now => $hafas->{now} + ref => $datetime_ref ); } @@ -177,7 +184,6 @@ sub new { } my $ref = { - datetime_now => $hafas->{now}, id => $jid, name => $name, number => $train_no, @@ -232,7 +238,7 @@ sub new { input => $timestr, date => $date, strp_obj => $hafas->{strptime_obj}, - now => $hafas->{now} + ref => $datetime_ref, ); } @@ -282,7 +288,7 @@ sub handle_day_change { if ( length($timestr) == 8 ) { # arrival time includes a day offset - my $offset_date = $opt{now}->clone; + my $offset_date = $opt{ref}->clone; $offset_date->add( days => substr( $timestr, 0, 2, q{} ) ); $offset_date = $offset_date->strftime('%Y%m%d'); $timestr = $opt{strp_obj}->parse_datetime("${offset_date}T${timestr}"); -- cgit v1.2.3