From: Johannes Plunien Date: Sat, 7 Feb 2009 13:40:41 +0000 (+0000) Subject: Possible to set locale in IC::DateTime extra => {} config X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8d689133f7e3c485bddbd229148487b03732e2e1;p=dbsrgits%2FDBIx-Class-Historic.git Possible to set locale in IC::DateTime extra => {} config --- diff --git a/Changes b/Changes index 82843e9..6ad0a6d 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,5 @@ Revision history for DBIx::Class + - Possible to set locale in IC::DateTime extra => {} config 0.08099_06 2009-01-23 07:30:00 (UTC) - Allow a scalarref to be supplied to the 'from' resultset attribute diff --git a/lib/DBIx/Class/InflateColumn/DateTime.pm b/lib/DBIx/Class/InflateColumn/DateTime.pm index 8e2e2d5..3024241 100644 --- a/lib/DBIx/Class/InflateColumn/DateTime.pm +++ b/lib/DBIx/Class/InflateColumn/DateTime.pm @@ -27,10 +27,10 @@ Then you can treat the specified column as a L object. print "This event starts the month of ". $event->starts_when->month_name(); -If you want to set a specific timezone for that field, use: +If you want to set a specific timezone and locale for that field, use: __PACKAGE__->add_columns( - starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago" } } + starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago", locale => "de_DE" } } ); If you want to inflate no matter what data_type your column is, @@ -110,10 +110,15 @@ sub register_column { } my $timezone; - if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) { + if ( defined $info->{extra}{timezone} ) { $timezone = $info->{extra}{timezone}; } + my $locale; + if ( defined $info->{extra}{locale} ) { + $locale = $info->{extra}{locale}; + } + my $undef_if_invalid = $info->{datetime_undef_if_invalid}; if ($type eq 'datetime' || $type eq 'date') { @@ -143,6 +148,7 @@ sub register_column { die "Error while inflating ${value} for ${column} on ${self}: $@" if $@ and not $undef_if_invalid; $dt->set_time_zone($timezone) if $timezone; + $dt->set_locale($locale) if $locale; return $dt; }, deflate => sub { @@ -154,6 +160,7 @@ sub register_column { and not $floating_tz_ok and not $ENV{DBIC_FLOATING_TZ_OK}; $value->set_time_zone($timezone); + $value->set_locale($locale) if $locale; } $obj->_datetime_parser->$format($value); }, diff --git a/t/89inflate_datetime.t b/t/89inflate_datetime.t index e67e1c0..baf8ef8 100644 --- a/t/89inflate_datetime.t +++ b/t/89inflate_datetime.t @@ -10,7 +10,7 @@ my $schema = DBICTest->init_schema(); eval { require DateTime::Format::MySQL }; plan skip_all => "Need DateTime::Format::MySQL for inflation tests" if $@; -plan tests => 28; +plan tests => 32; # inflation test my $event = $schema->resultset("Event")->find(1); @@ -58,6 +58,11 @@ my $event_tz = $schema->resultset('EventTZ')->create({ hour => 13, minute => 34, second => 56, time_zone => "America/New_York" ), }); +is ($event_tz->starts_at->day_name, "Montag", 'Locale de_DE loaded: day_name'); +is ($event_tz->starts_at->month_name, "Dezember", 'Locale de_DE loaded: month_name'); +is ($event_tz->created_on->day_name, "Tuesday", 'Default locale loaded: day_name'); +is ($event_tz->created_on->month_name, "January", 'Default locale loaded: month_name'); + my $starts_at = $event_tz->starts_at; is("$starts_at", '2007-12-31T00:00:00', 'Correct date/time using timezone'); diff --git a/t/lib/DBICTest/Schema/EventTZ.pm b/t/lib/DBICTest/Schema/EventTZ.pm index 8445aa1..d19980c 100644 --- a/t/lib/DBICTest/Schema/EventTZ.pm +++ b/t/lib/DBICTest/Schema/EventTZ.pm @@ -10,7 +10,7 @@ __PACKAGE__->table('event'); __PACKAGE__->add_columns( id => { data_type => 'integer', is_auto_increment => 1 }, - starts_at => { data_type => 'datetime', extra => { timezone => "America/Chicago" } }, + starts_at => { data_type => 'datetime', extra => { timezone => "America/Chicago", locale => 'de_DE' } }, created_on => { data_type => 'timestamp', extra => { timezone => "America/Chicago", floating_tz_ok => 1 } }, );