summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2014-01-17 23:42:04 +0100
committerDaniel Friesel <derf@finalrewind.org>2014-01-17 23:42:04 +0100
commitf0fba026a14cbee4af4d033244237d257aeaa15c (patch)
tree6c92b5f0f6149ec8df6939443a519fc6504c67a4 /lib
parenta88abfec0080fdb4ade8db22b8660968d785c546 (diff)
Only do DateTime math when needed (makes code ~2x as fast)
Diffstat (limited to 'lib')
-rw-r--r--lib/Travel/Status/DE/URA.pm17
-rw-r--r--lib/Travel/Status/DE/URA/Result.pm35
2 files changed, 38 insertions, 14 deletions
diff --git a/lib/Travel/Status/DE/URA.pm b/lib/Travel/Status/DE/URA.pm
index f48d7eb..71981c6 100644
--- a/lib/Travel/Status/DE/URA.pm
+++ b/lib/Travel/Status/DE/URA.pm
@@ -202,16 +202,11 @@ sub results {
push(
@results,
Travel::Status::DE::URA::Result->new(
- date => $dt_dep->strftime('%d.%m.%Y'),
- time => $dt_dep->strftime('%H:%M:%S'),
- datetime => $dt_dep,
- line => $linename,
- line_id => $lineid,
- destination => decode( 'UTF-8', $dest ),
- countdown =>
- $dt_dep->subtract_datetime($dt_now)->in_units('minutes'),
- countdown_sec =>
- $dt_dep->subtract_datetime($dt_now)->in_units('seconds'),
+ datetime => $dt_dep,
+ dt_now => $dt_now,
+ line => $linename,
+ line_id => $lineid,
+ destination => decode( 'UTF-8', $dest ),
route_timetable => [@route],
stop => $stopname,
stop_id => $stopid,
@@ -221,7 +216,7 @@ sub results {
@results = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
- map { [ $_, $_->countdown ] } @results;
+ map { [ $_, $_->datetime->epoch ] } @results;
$self->{results} = \@results;
diff --git a/lib/Travel/Status/DE/URA/Result.pm b/lib/Travel/Status/DE/URA/Result.pm
index bb1c0a9..e8cb1e8 100644
--- a/lib/Travel/Status/DE/URA/Result.pm
+++ b/lib/Travel/Status/DE/URA/Result.pm
@@ -9,9 +9,7 @@ use parent 'Class::Accessor';
our $VERSION = '0.02';
Travel::Status::DE::URA::Result->mk_ro_accessors(
- qw(countdown countdown_sec date datetime destination line line_id
- stop stop_id time)
-);
+ qw(datetime destination line line_id stop stop_id));
sub new {
my ( $obj, %conf ) = @_;
@@ -21,6 +19,37 @@ sub new {
return bless( $ref, $obj );
}
+sub countdown {
+ my ($self) = @_;
+
+ $self->{countdown} //= $self->datetime->subtract_datetime( $self->{dt_now} )
+ ->in_units('minutes');
+
+ return $self->{countdown};
+}
+
+sub countdown_sec {
+ my ($self) = @_;
+
+ $self->{countdown_sec}
+ //= $self->datetime->subtract_datetime( $self->{dt_now} )
+ ->in_units('seconds');
+
+ return $self->{countdown_sec};
+}
+
+sub date {
+ my ($self) = @_;
+
+ return $self->datetime->strftime('%d.%m.%Y');
+}
+
+sub time {
+ my ($self) = @_;
+
+ return $self->datetime->strftime('%H:%M:%S');
+}
+
sub route_timetable {
my ($self) = @_;