Hi,
I am trying to use DateTime to convert date and time between two
different locale, eg "04:55 PM" in en_US to "16:55" in fr_FR.
The DateTime object is created using DateTime::Format::Strptime
initialized with the short_time_format of a DateTime::Locale. Then
this DateTime received another DateTime::Format::Strptime as formatter
and the stringification occurs according the new format.
my $from_loc = DateTime::Locale->load($from_locale);
my $to_loc = DateTime::Locale->load($to_locale);
my $parser= DateTime::Format::Strptime->new(
pattern => $from_loc->short_time_format,
locale => $from_locale,
);
my $formatter = DateTime::Format::Strptime->new(
pattern => $to_loc->short_time_format,
locale => $to_locale,
);
my $dt = $parser->parse_datetime($str);
$dt->set_formatter($formatter);
print $dt;
For short_time_format, it's ok.
But for eg full_date_format, the destination locale of the formatter
is not used, the output is "Tuesday 20 November 2007" instead of
"mardi 20 novembre 2007".
I think that the problem is in DateTime::Format::Strptime : if the
formatter is set in DateTime, the stringification uses
format_datetime, which basically returns $dt->strftime($pattern);
But the strftime uses the locale set in the DateTime object, not the
locale defined in Strptime.
A solution could be to switch the locale before strftime and set i
back after, for consistency :
sub format_datetime {
my ( $self, $dt ) = @_;
my $pattern = $self->pattern;
$pattern =~ s/%O/$dt->time_zone->name/eg;
my $loc = $dt->{locale};
$dt->{locale} = $self->{locale};
my $str = $dt->strftime($pattern);
$dt->{locale} = $loc;
return $str;
}
btw, why there is no locale setter in DateTime ?
--
Julien.