From 4d5aff8a8d7cd196d153967b31c035745dc11123 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 4 Feb 2023 23:13:44 +0100 Subject: Journey: refactor day change handling into helper function --- lib/Travel/Status/DE/HAFAS/Journey.pm | 62 +++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 28 deletions(-) (limited to 'lib/Travel') diff --git a/lib/Travel/Status/DE/HAFAS/Journey.pm b/lib/Travel/Status/DE/HAFAS/Journey.pm index 2cd8075..f3c1c28 100644 --- a/lib/Travel/Status/DE/HAFAS/Journey.pm +++ b/lib/Travel/Status/DE/HAFAS/Journey.pm @@ -100,20 +100,14 @@ sub new { if ( not defined $timestr ) { next; } - if ( length($timestr) == 8 ) { - - # arrival time includes a day offset - my $offset_date = $hafas->{now}->clone; - $offset_date->add( days => substr( $timestr, 0, 2, q{} ) ); - $offset_date = $offset_date->strftime('%Y%m%d'); - $timestr = $hafas->{strptime_obj} - ->parse_datetime("${offset_date}T${timestr}"); - } - else { - $timestr - = $hafas->{strptime_obj} - ->parse_datetime("${date}T${timestr}"); - } + + $timestr = handle_day_change( + input => $timestr, + date => $date, + strp_obj => $hafas->{strptime_obj}, + now => $hafas->{now} + ); + } my $arr_delay @@ -214,20 +208,14 @@ sub new { if ( not defined $timestr ) { next; } - if ( length($timestr) == 8 ) { - - # arrival time includes a day offset - my $offset_date = $hafas->{now}->clone; - $offset_date->add( days => substr( $timestr, 0, 2, q{} ) ); - $offset_date = $offset_date->strftime('%Y%m%d'); - $timestr = $hafas->{strptime_obj} - ->parse_datetime("${offset_date}T${timestr}"); - } - else { - $timestr - = $hafas->{strptime_obj} - ->parse_datetime("${date}T${timestr}"); - } + + $timestr = handle_day_change( + input => $timestr, + date => $date, + strp_obj => $hafas->{strptime_obj}, + now => $hafas->{now} + ); + } my $datetime_s = $time_s; @@ -268,6 +256,24 @@ sub new { # }}} +sub handle_day_change { + my (%opt) = @_; + my $date = $opt{date}; + my $timestr = $opt{input}; + if ( length($timestr) == 8 ) { + + # arrival time includes a day offset + my $offset_date = $opt{now}->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}"); + } + else { + $timestr = $opt{strp_obj}->parse_datetime("${date}T${timestr}"); + } + return $timestr; +} + # {{{ Accessors # Legacy -- cgit v1.2.3