[Rt-commit] rt branch, 4.0/check-parsedate-result, created. rt-4.0.11rc1-28-gbec4aa9
? sunnavy
sunnavy at bestpractical.com
Thu Apr 11 10:46:26 EDT 2013
The branch, 4.0/check-parsedate-result has been created
at bec4aa9f7ab0131595a61fcf3e027a713a170893 (commit)
- Log -----------------------------------------------------------------
commit bec4aa9f7ab0131595a61fcf3e027a713a170893
Author: sunnavy <sunnavy at bestpractical.com>
Date: Thu Apr 11 22:40:32 2013 +0800
Time::ParseDate::parsedate could return error message and we shall log it
diff --git a/lib/RT/Articles.pm b/lib/RT/Articles.pm
index d69eabf..d2583a8 100644
--- a/lib/RT/Articles.pm
+++ b/lib/RT/Articles.pm
@@ -597,7 +597,11 @@ sub Search {
require Time::ParseDate;
foreach my $date (qw(Created< Created> LastUpdated< LastUpdated>)) {
next unless ( $args{$date} );
- my $seconds = Time::ParseDate::parsedate( $args{$date}, FUZZY => 1, PREFER_PAST => 1 );
+ my ($seconds, $error) = Time::ParseDate::parsedate( $args{$date}, FUZZY => 1, PREFER_PAST => 1 );
+ unless ( defined $seconds ) {
+ $RT::Logger->warning(
+ "Couldn't parse date '$args{$date}' by Time::ParseDate" );
+ }
my $date_obj = RT::Date->new( $self->CurrentUser );
$date_obj->Set( Format => 'unix', Value => $seconds );
$dates->{$date} = $date_obj;
diff --git a/lib/RT/Date.pm b/lib/RT/Date.pm
index d362197..054fcb4 100644
--- a/lib/RT/Date.pm
+++ b/lib/RT/Date.pm
@@ -208,7 +208,7 @@ sub Set {
# should be applied to absolute times, so compensate shift in NOW
my $now = time;
$now += ($self->Localtime( $args{Timezone}, $now ))[9];
- my $date = Time::ParseDate::parsedate(
+ my ($date, $error) = Time::ParseDate::parsedate(
$args{'Value'},
GMT => 1,
NOW => $now,
@@ -216,14 +216,23 @@ sub Set {
PREFER_PAST => RT->Config->Get('AmbiguousDayInPast'),
PREFER_FUTURE => RT->Config->Get('AmbiguousDayInFuture'),
);
- # apply timezone offset
- $date -= ($self->Localtime( $args{Timezone}, $date ))[9];
- $RT::Logger->debug(
- "RT::Date used Time::ParseDate to make '$args{'Value'}' $date\n"
- );
+ if ( defined $date ) {
+ # apply timezone offset
+ $date -= ( $self->Localtime( $args{Timezone}, $date ) )[9];
+
+ $RT::Logger->debug(
+ "RT::Date used Time::ParseDate to make '$args{'Value'}' $date\n"
+ );
- return $self->Set( Format => 'unix', Value => $date);
+ return $self->Set( Format => 'unix', Value => $date );
+ }
+ else {
+ $RT::Logger->warning(
+ "Couldn't parse date '$args{'Value'}' by Time::ParseDate"
+ );
+ return $self->Unix(0);
+ }
}
else {
$RT::Logger->error(
diff --git a/t/api/date.t b/t/api/date.t
index 728a4a2..f5eb172 100644
--- a/t/api/date.t
+++ b/t/api/date.t
@@ -306,8 +306,6 @@ my $year = (localtime(time))[5] + 1900;
{ # set+datemanip format(Time::ParseDate)
my $date = RT::Date->new(RT->SystemUser);
- $date->Set(Format => 'unknown', Value => 'weird date');
- is($date->Unix, 0, "date was wrong");
RT->Config->Set( Timezone => 'Europe/Moscow' );
$date->Set(Format => 'datemanip', Value => '2005-11-28 15:10:00');
@@ -325,7 +323,9 @@ my $year = (localtime(time))[5] + 1900;
{ # set+unknown format(Time::ParseDate)
my $date = RT::Date->new(RT->SystemUser);
- $date->Set(Format => 'unknown', Value => 'weird date');
+ warnings_like {
+ $date->Set(Format => 'unknown', Value => 'weird date');
+ } qr{Couldn't parse date 'weird date' by Time::ParseDate};
is($date->Unix, 0, "date was wrong");
RT->Config->Set( Timezone => 'Europe/Moscow' );
@@ -544,10 +544,10 @@ my $year = (localtime(time))[5] + 1900;
$date->Set( Value => 0, Format => 'unknown' );
is( $date->Unix(), 0, "unix is 0 with Value => 0, Format => 'unknown'" );
- $date->Set( Value => '', Format => 'unknown' );
+ $date->Set( Value => '', Format => 'unknown' );
is( $date->Unix(), 0, "unix is 0 with Value => '', Format => 'unknown'" );
- $date->Set( Value => ' ', Format => 'unknown' );
+ $date->Set( Value => ' ', Format => 'unknown' );
is( $date->Unix(), 0, "unix is 0 with Value => ' ', Format => 'unknown'" );
}
diff --git a/t/web/cf_date.t b/t/web/cf_date.t
index 4ea93e4..e69833c 100644
--- a/t/web/cf_date.t
+++ b/t/web/cf_date.t
@@ -2,7 +2,7 @@
use strict;
use warnings;
-use RT::Test tests => 35;
+use RT::Test tests => undef;
my ( $baseurl, $m ) = RT::Test->started_ok;
ok $m->login, 'logged in as root';
@@ -183,4 +183,10 @@ diag 'check invalid inputs';
$m->content_contains('test cf date:', 'has no cf date field on the page' );
$m->content_lacks('foodate', 'invalid dates not set' );
+
+ my @warnings = $m->get_warnings;
+ chomp @warnings;
+ is_deeply( @warnings, q{Couldn't parse date 'foodate' by Time::ParseDate} );
}
+
+done_testing;
diff --git a/t/web/cf_datetime.t b/t/web/cf_datetime.t
index 8cc099d..4580c4a 100644
--- a/t/web/cf_datetime.t
+++ b/t/web/cf_datetime.t
@@ -2,7 +2,7 @@
use strict;
use warnings;
-use RT::Test tests => 51;
+use RT::Test tests => undef;
RT->Config->Set( 'Timezone' => 'EST5EDT' ); # -04:00
my ($baseurl, $m) = RT::Test->started_ok;
@@ -209,6 +209,10 @@ diag 'check invalid inputs';
$m->content_contains('test cf datetime:', 'has cf datetime field on the page');
$m->content_lacks('foodate', 'invalid dates not set');
+
+ my @warnings = $m->get_warnings;
+ chomp @warnings;
+ is_deeply( @warnings, q{Couldn't parse date 'foodate' by Time::ParseDate} );
}
sub is_results_number {
@@ -232,3 +236,4 @@ sub is_results_number {
# to make $m->DESTROY happy
undef $m;
+done_testing;
-----------------------------------------------------------------------
More information about the Rt-commit
mailing list