From 85af20955c828eb7bfb435d8afa3770d9198e955 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 15 Dec 2013 22:13:27 +0100 Subject: add -s, -S options to allow user control of time/duration formats --- Build.PL | 1 + README | 1 + bin/aseag-m | 56 +++++++++++++++++++++++++++--------- lib/Travel/Status/DE/ASEAG.pm | 2 +- lib/Travel/Status/DE/ASEAG/Result.pm | 6 ++-- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/Build.PL b/Build.PL index f6dc818..c8ecc23 100644 --- a/Build.PL +++ b/Build.PL @@ -21,6 +21,7 @@ Module::Build->new( 'Carp' => 0, 'Class::Accessor' => 0, 'DateTime' => 0, + 'DateTime::Format::Duration' => 0, 'Getopt::Long' => 0, 'List::MoreUtils' => 0, 'LWP::UserAgent' => 0, diff --git a/README b/README index 420bd91..d5b1044 100644 --- a/README +++ b/README @@ -10,6 +10,7 @@ Dependencies * perl version 5.10.1 or newer * Class::Accessor * DateTime + * DateTime::Format::Duration * LWP::UserAgent diff --git a/bin/aseag-m b/bin/aseag-m index 73b5236..e0ac784 100755 --- a/bin/aseag-m +++ b/bin/aseag-m @@ -9,14 +9,18 @@ our $VERSION = '0.00'; binmode( STDOUT, ':encoding(utf-8)' ); +use DateTime; +use DateTime::Format::Duration; use Getopt::Long qw(:config no_ignore_case); use List::Util qw(max); use Travel::Status::DE::ASEAG; my (@grep_lines); -my $full_route = 0; -my $hide_past = 1; -my $relative_times = 0; +my $full_route = 0; +my $hide_past = 1; +my $relative_times = 0; +my $strftime_format = '%H:%M:%S'; +my $strfrel_format = '%M min'; my $via; GetOptions( @@ -27,6 +31,8 @@ GetOptions( 'l|line=s@' => \@grep_lines, 'p|with-past' => sub { $hide_past = 0 }, 'r|relative' => \$relative_times, + 's|strftime=s' => \$strftime_format, + 'S|strfrel=s' => \$strfrel_format, 'v|via=s' => \$via, 'V|version' => \&show_version, @@ -89,6 +95,10 @@ sub display_result { sub show_results { my @output; + my $dt_now = DateTime->now( time_zone => 'Europe/Berlin' ); + my $dt_format + = DateTime::Format::Duration->new( pattern => $strfrel_format ); + for my $d ( $status->results( full_routes => $full_route, @@ -99,8 +109,6 @@ sub show_results { ) { - my $dtime = $d->time; - if ( ( @grep_lines and not( $d->line ~~ \@grep_lines ) ) ) { next; } @@ -111,10 +119,17 @@ sub show_results { [ $d->line, $d->destination, - sprintf( '%3d min', $d->countdown ), - join( "\n", - map { sprintf( '%-8s %s', @{$_} ) } - @{ $d->route_timetable } ) + $dt_format->format_duration( + $d->datetime->subtract_datetime($dt_now) + ), + join( + "\n", + map { + sprintf( '%s %s', + $_->[0]->strftime($strftime_format), + $_->[1] ) + } @{ $d->route_timetable } + ) ] ); } @@ -122,12 +137,17 @@ sub show_results { push( @output, [ - $dtime, + $d->datetime->strftime($strftime_format), $d->line, $d->destination, - join( "\n", - map { sprintf( '%-8s %s', @{$_} ) } - @{ $d->route_timetable } ) + join( + "\n", + map { + sprintf( '%s %s', + $_->[0]->strftime($strftime_format), + $_->[1] ) + } @{ $d->route_timetable } + ) ] ); } @@ -193,6 +213,16 @@ B<-a> / B<-b> / B<-f>. Use relative departure and route times. +=item B<-s>, B<--strftime> I + +Format absolute times in I, appleas both to departures and route +data. See DateTime(3pm) for allowed patterns. + +=item B<-S>, B<--strfrel> I + +Format relative times in I, only applies when used with B<-r>. +See DateTime::Format::Duration(3pm) for allowed patterns. + =item B<-v>, B<--via> I Only show lines which will also service I. With B<-b>, I must be diff --git a/lib/Travel/Status/DE/ASEAG.pm b/lib/Travel/Status/DE/ASEAG.pm index ed17d98..4be2cd2 100644 --- a/lib/Travel/Status/DE/ASEAG.pm +++ b/lib/Travel/Status/DE/ASEAG.pm @@ -188,7 +188,7 @@ sub results { DateTime->from_epoch( epoch => $_->[0], time_zone => 'Europe/Berlin' - )->hms, + ), decode( 'UTF-8', $_->[1] ) ] } @route; diff --git a/lib/Travel/Status/DE/ASEAG/Result.pm b/lib/Travel/Status/DE/ASEAG/Result.pm index 6e09692..3d88261 100644 --- a/lib/Travel/Status/DE/ASEAG/Result.pm +++ b/lib/Travel/Status/DE/ASEAG/Result.pm @@ -94,8 +94,10 @@ The number of the line. =item $departure->route_timetable -Returns an arrayref of arrayrefs describing the entire route as departure, -stop objects. +Returns an arrayref of arrayrefs describing the entire route. I.e. +C<< [[$time1, $stop1], [$time2, $stop2], ...] >>. +The times are DateTime::Duration(3pm) objects, the stops are only names, +not IDs (subject to change). =item $departure->stop -- cgit v1.2.3