@@ -, +, @@ --- Koha/Illrequest/Logger.pm | 24 ++++++++++++++++++++++-- t/db_dependent/Illrequest/Logger.t | 12 ++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) --- a/Koha/Illrequest/Logger.pm +++ a/Koha/Illrequest/Logger.pm @@ -19,7 +19,7 @@ package Koha::Illrequest::Logger; use Modern::Perl; use JSON qw( to_json from_json ); -use Time::Piece; +use Time::Local; use C4::Context; use C4::Templates; @@ -197,6 +197,26 @@ sub get_log_template { } } +=head3 get_epoch + + $epoch = Koha::Illrequest::Logger->get_epoch($timestamp); + +Given a timestamp string returned from GetLogs, get the epoch + +=cut + +sub get_epoch { + my $timestamp = shift; + + return if !$timestamp=~/\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}/; + + my ($date, $time) = split(/\s/, $timestamp); + my ($y, $mon, $d) = split(/-/, $date); + my ($h, $min, $s) = split(/:/, $time); + + return timelocal($s, $min, $h, $d, $mon-1, $y); +} + =head3 get_request_logs $requestlogs = Koha::IllRequest::Logger->get_request_logs($request_id); @@ -230,7 +250,7 @@ sub get_request_logs { } my $format = '%Y-%m-%d %H:%M:%S'; - my @sorted = sort {Time::Piece->strptime($$b{'timestamp'}, $format)->epoch <=> Time::Piece->strptime($$a{'timestamp'}, $format)->epoch} @{$logs}; + my @sorted = sort { get_epoch($$b{'timestamp'}) <=> get_epoch($$a{'timestamp'}) } @{$logs}; return \@sorted; } --- a/t/db_dependent/Illrequest/Logger.t +++ a/t/db_dependent/Illrequest/Logger.t @@ -32,17 +32,17 @@ my $logs = [ { info => '{"log_origin": "core"}', action => 'STATUS_CHANGE', - timestamp => 1538478742 + timestamp => '2018-10-02 11:12:22' }, { info => '{"log_origin": "core"}', action => 'STATUS_CHANGE', - timestamp => 1538478732 + timestamp => '2018-10-02 11:12:12' }, { info => '{"log_origin": "core"}', action => 'STATUS_CHANGE', - timestamp => 1538478752 + timestamp => '2018-10-02 11:12:32' } ]; # Mock the modules we use @@ -126,20 +126,20 @@ subtest 'Basics' => sub { { 'template' => 'mock', 'info' => { 'log_origin' => 'core' }, - 'timestamp' => 1538478752, + 'timestamp' => '2018-10-02 11:12:32', 'action' => 'STATUS_CHANGE' }, { 'template' => 'mock', 'action' => 'STATUS_CHANGE', - 'timestamp' => 1538478742, + 'timestamp' => '2018-10-02 11:12:22', 'info' => { 'log_origin' => 'core' } }, { 'template' => 'mock', 'action' => 'STATUS_CHANGE', 'info' => { 'log_origin' => 'core' }, - 'timestamp' => 1538478732 + 'timestamp' => '2018-10-02 11:12:12' } ]; my $me_mock = Test::MockModule->new('Koha::Illrequest::Logger'); --