From 46d5acf18d190732131ff4a8cb53106538b744be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Demians?= Date: Sat, 15 Nov 2014 13:20:31 +0100 Subject: [PATCH] Bug 13242: Add a UT to t/DateUtils.t for testing DateTime bug A bug in DateTime slow down drastically date parsing when the dates are in the far distant future: https://metacpan.org/pod/DateTime#Determining-the-Local-Time-Zone-Can-Be-Slow This UT tests this situation which affects Koha::DateUtils function dt_from_string() and output_pref(). TO TEST: - Apply the patch containing the UT - prove -v t/DateUtils.t - You see that parsing a 9999-01-01 that take forever (ie more than 1s) - Apply the patch containing the fix - prove -v t/DateUtils.t - No more complain. --- t/DateUtils.t | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/t/DateUtils.t b/t/DateUtils.t index 9afddc1..2439127 100755 --- a/t/DateUtils.t +++ b/t/DateUtils.t @@ -1,12 +1,11 @@ -use strict; -use warnings; -use 5.010; +use Modern::Perl; use DateTime; use DateTime::TimeZone; use C4::Context; -use Test::More tests => 34; +use Test::More tests => 41; use Test::MockModule; +use Time::HiRes qw/ gettimeofday /; BEGIN { use_ok('Koha::DateUtils'); } @@ -77,6 +76,17 @@ isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' ); cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'Returned Dublin object matches input' ); +for ( qw/ 2014-01-01 2100-01-01 9999-01-01 / ) { + my $duration = gettimeofday(); + $new_dt = dt_from_string($_, 'iso', $dear_dirty_dublin); + $duration = gettimeofday() - $duration; + cmp_ok $duration, '<', 1, "Create DateTime with dt_from_string() for $_ with TZ in less than 1s"; + $duration = gettimeofday(); + output_pref( { dt => $new_dt } ); + $duration = gettimeofday() - $duration; + cmp_ok $duration, '<', 1, "Create DateTime with output_pref() for $_ with TZ in less than 1s"; +} + $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' ); isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' ); cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' ); @@ -106,6 +116,10 @@ cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' ); $dt0 = dt_from_string( '0000-00-00', 'iso' ); is( $dt0, undef, "undefined returned for 0 iso date" ); +# Return undef if passed mysql 9999-* date +my $dt9999 = dt_from_string( '9999-12-31' ); +is( $dt9999->ymd(), '9999-12-31', "dt_from_string should return a DateTime object for 9999-12-31" ); + my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' ); cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' ); -- 2.1.2