Subject: | Fixed the %j conversion specification in ->strftime to return the correct value |
%j should be padded with zeros if the day of the year is < 100, this matches
the DateTime documentation and the POSIX specification which says: Replaced by
the day of the year as a decimal number [001,366]. [ tm_yday]
I have attached a patch that corrects this, alternatively the fix could also be
cherry picked from <https://github.com/chansen/DateTime.pm/commit/32464cf62dc9d8c1c9282cee33a30248bd3f8aa5>
--
chansen
Subject: | patch.diff |
diff --git a/lib/DateTime.pm b/lib/DateTime.pm
index 3d716b7..d854bcd 100644
--- a/lib/DateTime.pm
+++ b/lib/DateTime.pm
@@ -989,7 +989,7 @@ sub mjd { $_[0]->jd - 2_400_000.5 }
'G' => sub { $_[0]->week_year },
'H' => sub { sprintf( '%02d', $_[0]->hour ) },
'I' => sub { sprintf( '%02d', $_[0]->hour_12 ) },
- 'j' => sub { $_[0]->day_of_year },
+ 'j' => sub { sprintf( '%03d', $_[0]->day_of_year ) },
'k' => sub { sprintf( '%2d', $_[0]->hour ) },
'l' => sub { sprintf( '%2d', $_[0]->hour_12 ) },
'm' => sub { sprintf( '%02d', $_[0]->month ) },
diff --git a/t/13strftime.t b/t/13strftime.t
index 67b1f92..c39f3f5 100644
--- a/t/13strftime.t
+++ b/t/13strftime.t
@@ -307,10 +307,11 @@ it
year => 2012, month => 1, day => 1
%U => '01'
%W => '00'
+%j => '001'
year => 2012, month => 1, day => 10
%U => '02'
%W => '02'
-
+%j => '010'
year => 1999, month => 9, day => 7, hour => 13, minute => 2, second => 42, nanosecond => 00012345678
%N => '012345678'
%3N => '012'