Skip to content

Commit

Permalink
The %F pattern is not the same as ->ymd
Browse files Browse the repository at this point in the history
With %F we should not zero pad years less than 4 digits in order to match what
POSIX does.
  • Loading branch information
autarch committed Aug 1, 2018
1 parent 8836367 commit 638e991
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 26 deletions.
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{{$NEXT}}

- The %F strftime pattern incorrectly zero-padded numbers less than four
digits. According to POSIX::strftime, this should output the year as-is
without padding. Reported by Andy Lester. GH #83.


1.49 2018-05-20

- Updated the ppport.h with the latest version of Devel::PPPort. This fixes a
Expand Down
2 changes: 1 addition & 1 deletion lib/DateTime.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ sub jd { $_[0]->mjd + 2_400_000.5 }
'd' => sub { sprintf( '%02d', $_[0]->day_of_month ) },
'D' => sub { $_[0]->strftime('%m/%d/%y') },
'e' => sub { sprintf( '%2d', $_[0]->day_of_month ) },
'F' => sub { $_[0]->ymd('-') },
'F' => sub { $_[0]->strftime('%Y-%m-%d') },
'g' => sub { substr( $_[0]->week_year, -2 ) },
'G' => sub { $_[0]->week_year },
'H' => sub { sprintf( '%02d', $_[0]->hour ) },
Expand Down
12 changes: 12 additions & 0 deletions t/03components.t
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ use DateTime;
is( $dt->hms('%s'), '03%s20%s05', 'use %s as separator in hms' );
}

{
my $dt = DateTime->new(
year => 12345,
month => 2,
day => 3,
);

is( $dt->ymd, '12345-02-03', '5 digit year in ymd' );
is( $dt->mdy, '02-03-12345', '5 digit year in mdy' );
is( $dt->dmy, '03-02-12345', '5 digit year in dmy' );
}

# test doy in leap year
{
my $dt = DateTime->new(
Expand Down
68 changes: 43 additions & 25 deletions t/13strftime.t
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ subtest(
'%6N' => '012345',
'%10N' => '0123456780',
);
for my $fmt ( sort keys %tests ) {
for my $fmt ( sort { lc $a cmp lc $b } keys %tests ) {
is(
$dt->strftime($fmt), $tests{$fmt},
"$fmt is $tests{$fmt}"
Expand Down Expand Up @@ -243,7 +243,7 @@ subtest(
'%W' => '00',
'%j' => '001',
);
for my $fmt ( sort keys %tests ) {
for my $fmt ( sort { lc $a cmp lc $b } keys %tests ) {
is(
$dt->strftime($fmt), $tests{$fmt},
"$fmt is $tests{$fmt}"
Expand All @@ -261,7 +261,7 @@ subtest(
'%W' => '02',
'%j' => '010',
);
for my $fmt ( sort keys %tests ) {
for my $fmt ( sort { lc $a cmp lc $b } keys %tests ) {
is(
$dt->strftime($fmt), $tests{$fmt},
"$fmt is $tests{$fmt}"
Expand All @@ -272,6 +272,18 @@ subtest(
}
);

subtest(
'%F with 3 digit years',
sub {
my $dt = DateTime->new( year => 123 );
is(
$dt->strftime('%F'),
'123-01-01',
'%F does not zero-pad 3 digit year'
);
}
);

done_testing();

sub test_strftime_for_locale {
Expand All @@ -293,7 +305,7 @@ sub test_strftime_for_locale {
subtest(
$locale,
sub {
for my $fmt ( sort keys %{$tests} ) {
for my $fmt ( sort { lc $a cmp lc $b } keys %{$tests} ) {
is(
$dt->strftime($fmt),
$tests->{$fmt},
Expand All @@ -314,14 +326,22 @@ sub en_tests {

return {
'%%' => '%',
'%10N' => '1234567890',
'%3N' => '123',
'%6N' => '123456',
'%a' => $en_locale->day_format_abbreviated->[1],
'%A' => $en_locale->day_format_wide->[1],
'%b' => $en_locale->month_format_abbreviated->[8],
'%B' => $en_locale->month_format_wide->[8],
'%c' => $c_format,
'%C' => '19',
'%d' => '07',
'%e' => ' 7',
'%D' => '09/07/99',
'%e' => ' 7',
'%E' => '%E',
'%F' => '1999-09-07',
'%G' => '1999',
'%g' => '99',
'%h' => $en_locale->month_format_abbreviated->[8],
'%H' => '13',
'%I' => '01',
Expand All @@ -330,31 +350,30 @@ sub en_tests {
'%l' => ' 1',
'%m' => '09',
'%M' => '02',
'%n' => "\n",
'%N' => '123456789',
'%3N' => '123',
'%6N' => '123456',
'%10N' => '1234567890',
'%p' => $en_locale->am_pm_abbreviated->[1],
'%P' => lc $en_locale->am_pm_abbreviated->[1],
'%r' => '01:02:42 ' . $en_locale->am_pm_abbreviated->[1],
'%R' => '13:02',
'%s' => '936709362',
'%S' => '42',
'%t' => "\t",
'%T' => '13:02:42',
'%u' => '2',
'%U' => '36',
'%V' => '36',
'%w' => '2',
'%W' => '36',
'%x' => $en_locale->month_format_abbreviated->[8] . ' 7, 1999',
'%X' => '1:02:42 ' . $en_locale->am_pm_abbreviated->[1],
'%y' => '99',
'%Y' => '1999',
'%Z' => 'UTC',
'%z' => '+0000',
'%E' => '%E',
'%Z' => 'UTC',
'%{foobar}' => '%{foobar}',
'%{month}' => '9',
'%{year}' => '1999',
'%x' => $en_locale->month_format_abbreviated->[8] . ' 7, 1999',
'%X' => '1:02:42 ' . $en_locale->am_pm_abbreviated->[1],
'%c' => $c_format,
};
}

Expand All @@ -368,30 +387,29 @@ sub de_tests {
'%B' => $de_locale->month_format_wide->[8],
'%C' => '19',
'%d' => '07',
'%e' => ' 7',
'%D' => '09/07/99',
'%b' => $de_locale->month_format_abbreviated->[8],
'%e' => ' 7',
'%H' => '13',
'%I' => '01',
'%j' => '250',
'%k' => '13',
'%l' => ' 1',
'%m' => '09',
'%M' => '02',
'%m' => '09',
'%p' => $de_locale->am_pm_abbreviated->[1],
'%r' => '01:02:42 ' . $de_locale->am_pm_abbreviated->[1],
'%R' => '13:02',
'%s' => '936709362',
'%S' => '42',
'%s' => '936709362',
'%T' => '13:02:42',
'%U' => '36',
'%V' => '36',
'%w' => '2',
'%W' => '36',
'%y' => '99',
'%Y' => '1999',
'%Z' => 'UTC',
'%y' => '99',
'%z' => '+0000',
'%Z' => 'UTC',
'%{month}' => '9',
'%{year}' => '1999',
};
Expand All @@ -404,33 +422,33 @@ sub it_tests {
'%a' => $it_locale->day_format_abbreviated->[1],
'%A' => $it_locale->day_format_wide->[1],
'%b' => $it_locale->month_format_abbreviated->[8],
'%b' => $it_locale->month_format_abbreviated->[8],
'%B' => $it_locale->month_format_wide->[8],
'%C' => '19',
'%d' => '07',
'%e' => ' 7',
'%D' => '09/07/99',
'%b' => $it_locale->month_format_abbreviated->[8],
'%e' => ' 7',
'%H' => '13',
'%I' => '01',
'%j' => '250',
'%k' => '13',
'%l' => ' 1',
'%m' => '09',
'%M' => '02',
'%m' => '09',
'%p' => $it_locale->am_pm_abbreviated->[1],
'%r' => '01:02:42 ' . $it_locale->am_pm_abbreviated->[1],
'%R' => '13:02',
'%s' => '936709362',
'%S' => '42',
'%s' => '936709362',
'%T' => '13:02:42',
'%U' => '36',
'%V' => '36',
'%w' => '2',
'%W' => '36',
'%y' => '99',
'%Y' => '1999',
'%Z' => 'UTC',
'%y' => '99',
'%z' => '+0000',
'%Z' => 'UTC',
'%{month}' => '9',
'%{year}' => '1999',
};
Expand Down

0 comments on commit 638e991

Please sign in to comment.