Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the DateTime-Format-Strptime CPAN distribution.

Report information
The Basics
Id: 12071
Status: resolved
Priority: 0/
Queue: DateTime-Format-Strptime

People
Owner: rickm [...] cpan.org
Requestors: ferreira [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 1.0601
Fixed in: (no value)



Subject: format_datetime uses datetime locale rather than format locale
I found a minor problem when the formatter has a locale different from the one of the datetime object. format_datetime() is implemented via a simple $dt->strftime() and thus uses datetime locale rather than formatter locale. The following piece of code illustrates the problem: my $dmy_format = new DateTime::Format::Strptime( pattern => '%d/%m/%Y', locale => 'en'); # $dmy_format->locale has locale 'en' my $dt = $dmy_format->parse_datetime('03/08/2004'); # datetime inherits locale 'en' my $my_format = new DateTime::Format::Strptime( pattern => '%B/%Y', locale => 'pt'); print $my_format->format_datetime($dt); # should print "agosto/2004" but prints "august/2004" in 1.0601 A patch is attached with an expanded version of format_datetime() which corrects the problem and a test script. The patch was applied with $ patch -p1 -i strptime.diff inside the directory "DateTime-Format-Strptime-1.0601/" from the latest release (1.0601). Regards, Adriano. -- Adriano R. Ferreira
diff -r -N -u old/lib/DateTime/Format/Strptime.pm new/lib/DateTime/Format/Strptime.pm --- old/lib/DateTime/Format/Strptime.pm 2004-08-31 18:56:18.000000000 -0300 +++ new/lib/DateTime/Format/Strptime.pm 2005-03-30 09:52:22.000000000 -0300 @@ -11,7 +11,7 @@ use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK %ZONEMAP %FORMATS $CROAK $errmsg); @ISA = 'Exporter'; -$VERSION = '1.0601'; +$VERSION = '1.0602'; @EXPORT_OK = qw( &strftime &strptime ); @EXPORT = (); @@ -560,7 +560,19 @@ sub format_datetime { my ( $self, $dt ) = @_; - return $dt->strftime($self->pattern); + + my $dt_locale; + if ($dt->locale->id ne $self->locale) { # save dt locale (if not the same) + $dt_locale = $dt->locale; + $dt->set_locale($self->locale); + } + + my $str = $dt->strftime($self->pattern); + + # restore dt locale (if needed) + $dt->set_locale($dt_locale) if ($dt_locale); + + return $str; } sub format_duration { diff -r -N -u old/t/008_strftime.t new/t/008_strftime.t --- old/t/008_strftime.t 1969-12-31 21:00:00.000000000 -0300 +++ new/t/008_strftime.t 2005-03-30 10:18:58.000000000 -0300 @@ -0,0 +1,24 @@ +#!perl -w + +# t/008_strftime.t - test for a bug in format_datetime when the datetime +# has a locale other than the format locale + +use Test::More tests => 3; +use DateTime; +use DateTime::Format::Strptime; + +# note. there is nothing special about the locales 'en' or 'pt' + +my $dmy_format = new DateTime::Format::Strptime(pattern => '%d/%m/%Y', locale => 'en'); +is($dmy_format->locale, 'en', "has locale 'en'"); + +my $dt = $dmy_format->parse_datetime('03/08/2004'); +is($dt->locale->id, 'en', "datetime inherits locale 'en'"); + +my $my_format = new DateTime::Format::Strptime(pattern => '%B/%Y', locale => 'pt'); +is(lc $my_format->format_datetime($dt), 'agosto/2004', 'format_datetime uses format locale'); + +# 'lc' is needed here because month names in Portuguese +# have changed from capitalized ('Agosto') to lower case ('agosto') +# from DateTime::Locale 0.09 to 0.20 +
Resolved in 1.0900 using a different solution (cloning the DT object before changing the locale) In future, please log bugs in the google-code project: http://code.google.com/p/datetime-format-strptime/