Subject: | [PATCH] '%e' and friends can include a leading space, which DateTime objects to |
Date: | Wed, 16 Jun 2010 13:27:21 -0400 |
To: | bug-datetime-format-strptime [...] rt.cpan.org |
From: | Alex Vandiver <alex [...] chmrr.net> |
When parsing, '%e' accepts a leading space; however, when ' 3' is
passed as an argument to DateTime->new, it fails. Resolve this by
forcing the arguemnts to DateTime->new to be purely numeric, by adding
0.
---
lib/DateTime/Format/Strptime.pm | 18 +++++++++---------
t/002_dates.t | 6 +++++-
2 files changed, 14 insertions(+), 10 deletions(-)
diff --git a/lib/DateTime/Format/Strptime.pm b/lib/DateTime/Format/Strptime.pm
index fcf1700..1e4a2fc 100644
--- a/lib/DateTime/Format/Strptime.pm
+++ b/lib/DateTime/Format/Strptime.pm
@@ -542,7 +542,7 @@ iso_week_year_100 = $iso_week_year_100
and return undef
unless $Year and $Month;
my $dt = eval {
- DateTime->new( year => $Year, month => $Month, day => $Day,
+ DateTime->new( year => $Year+0, month => $Month+0, day => $Day+0,
hour => 12, time_zone => $use_timezone );
};
$self->local_croak("Datetime $Year-$Month-$Day is not a valid date")
@@ -646,14 +646,14 @@ iso_week_year_100 = $iso_week_year_100
my $potential_return = eval {
DateTime->new(
- year => ( $Year || 1 ),
- month => ( $Month || 1 ),
- day => ( $Day || 1 ),
-
- hour => ( $Hour || 0 ),
- minute => ( $Minute || 0 ),
- second => ( $Second || 0 ),
- nanosecond => ( $Nanosecond || 0 ),
+ year => ( $Year || 1 )+0,
+ month => ( $Month || 1 )+0,
+ day => ( $Day || 1 )+0,
+
+ hour => ( $Hour || 0 )+0,
+ minute => ( $Minute || 0 )+0,
+ second => ( $Second || 0 )+0,
+ nanosecond => ( $Nanosecond || 0 )+0,
locale => $self->{_locale},
time_zone => $use_timezone,
diff --git a/t/002_dates.t b/t/002_dates.t
index e15fda3..f1c1e03 100644
--- a/t/002_dates.t
+++ b/t/002_dates.t
@@ -2,7 +2,7 @@
# t/002_basic.t - check module dates in various formats
-use Test::More tests => 17;
+use Test::More tests => 19;
use DateTime::Format::Strptime;
use DateTime;
use DateTime::TimeZone;
@@ -36,6 +36,10 @@ my @tests = (
# Complex dates
[ '%Y;%j = %Y-%m-%d', '2003;56 = 2003-02-25' ],
[ q|%d %b '%y = %Y-%m-%d|, q|25 Feb '03 = 2003-02-25| ],
+
+ # Leading spaces
+ [ '%e-%b-%Y %T %z', '13-Jun-2010 09:20:47 -0400' ],
+ [ '%e-%b-%Y %T %z', ' 3-Jun-2010 09:20:47 -0400' ],
);
foreach (@tests) {
--
1.7.0.4