[Bast-commits] r3809 - in DBIx-Class/0.08/trunk: . lib/DBIx/Class/InflateColumn t t/lib/DBICTest t/lib/DBICTest/Schema

ash at dev.catalyst.perl.org ash at dev.catalyst.perl.org
Sun Oct 7 22:50:34 GMT 2007


Author: ash
Date: 2007-10-07 22:50:33 +0100 (Sun, 07 Oct 2007)
New Revision: 3809

Added:
   DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema/EventTZ.pm
Modified:
   DBIx-Class/0.08/trunk/Changes
   DBIx-Class/0.08/trunk/lib/DBIx/Class/InflateColumn/DateTime.pm
   DBIx-Class/0.08/trunk/t/89inflate_datetime.t
   DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema.pm
Log:
Timezone support for InflateColumn::DateTime (sergio)

Modified: DBIx-Class/0.08/trunk/Changes
===================================================================
--- DBIx-Class/0.08/trunk/Changes	2007-10-07 18:49:39 UTC (rev 3808)
+++ DBIx-Class/0.08/trunk/Changes	2007-10-07 21:50:33 UTC (rev 3809)
@@ -6,6 +6,8 @@
           context; this makes things like func('DISTINCT') work as expected
         - Many-to-many relationships now warn if the utility methods would 
           clash
+        - InflateColumn::DateTime now accepts an extra parameter of timezone
+          to set timezone on the DT object (thanks Sergio Salvi)
 
 0.08007 2007-09-04 19:36:00
         - patch for Oracle datetime inflation (abram at arin.net)

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/InflateColumn/DateTime.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/InflateColumn/DateTime.pm	2007-10-07 18:49:39 UTC (rev 3808)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/InflateColumn/DateTime.pm	2007-10-07 21:50:33 UTC (rev 3809)
@@ -24,6 +24,12 @@
   print "This event starts the month of ".
     $event->starts_when->month_name();
 
+If you want to set a specific timezone for that field, use:
+
+  __PACKAGE__->add_columns(
+    starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago" } }
+  );
+
 =head1 DESCRIPTION
 
 This module figures out the type of DateTime::Format::* class to 
@@ -55,6 +61,11 @@
   return unless defined($info->{data_type});
   my $type = lc($info->{data_type});
   $type = 'datetime' if ($type =~ /^timestamp/);
+  my $timezone;
+  if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) {
+    $timezone = $info->{extra}{timezone};
+  }
+
   if ($type eq 'datetime' || $type eq 'date') {
     my ($parse, $format) = ("parse_${type}", "format_${type}");
     $self->inflate_column(
@@ -62,10 +73,13 @@
         {
           inflate => sub {
             my ($value, $obj) = @_;
-            $obj->_datetime_parser->$parse($value);
+            my $dt = $obj->_datetime_parser->$parse($value);
+            $dt->set_time_zone($timezone) if $timezone;
+            return $dt;
           },
           deflate => sub {
             my ($value, $obj) = @_;
+            $value->set_time_zone($timezone) if $timezone;
             $obj->_datetime_parser->$format($value);
           },
         }

Modified: DBIx-Class/0.08/trunk/t/89inflate_datetime.t
===================================================================
--- DBIx-Class/0.08/trunk/t/89inflate_datetime.t	2007-10-07 18:49:39 UTC (rev 3808)
+++ DBIx-Class/0.08/trunk/t/89inflate_datetime.t	2007-10-07 21:50:33 UTC (rev 3809)
@@ -10,7 +10,7 @@
 eval { require DateTime::Format::MySQL };
 plan skip_all => "Need DateTime::Format::MySQL for inflation tests" if $@;
 
-plan tests => 8;
+plan tests => 17;
 
 # inflation test
 my $event = $schema->resultset("Event")->find(1);
@@ -42,3 +42,31 @@
 
 isa_ok($created->created_on, 'DateTime', 'DateTime returned');
 is("$created_cron", '2006-06-23T00:00:00', 'Correct date/time');
+
+
+# Test "timezone" parameter
+my $event_tz = $schema->resultset('EventTZ')->create({
+    starts_at => DateTime->new(year=>2007, month=>12, day=>31, time_zone => "America/Chicago" ),
+    created_on => DateTime->new(year=>2006, month=>1, day=>31,
+        hour => 13, minute => 34, second => 56, time_zone => "America/New_York" ),
+});
+
+my $starts_at = $event_tz->starts_at;
+is("$starts_at", '2007-12-31T00:00:00', 'Correct date/time using timezone');
+
+my $created_on = $event_tz->created_on;
+is("$created_on", '2006-01-31T12:34:56', 'Correct timestamp using timezone');
+is($event_tz->created_on->time_zone->name, "America/Chicago", "Correct timezone");
+
+my $loaded_event = $schema->resultset('EventTZ')->find( $event_tz->id );
+
+isa_ok($loaded_event->starts_at, 'DateTime', 'DateTime returned');
+$starts_at = $loaded_event->starts_at;
+is("$starts_at", '2007-12-31T00:00:00', 'Loaded correct date/time using timezone');
+is($starts_at->time_zone->name, 'America/Chicago', 'Correct timezone');
+
+isa_ok($loaded_event->created_on, 'DateTime', 'DateTime returned');
+$created_on = $loaded_event->created_on;
+is("$created_on", '2006-01-31T12:34:56', 'Loaded correct timestamp using timezone');
+is($created_on->time_zone->name, 'America/Chicago', 'Correct timezone');
+

Copied: DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema/EventTZ.pm (from rev 3607, DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema/Event.pm)
===================================================================
--- DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema/EventTZ.pm	                        (rev 0)
+++ DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema/EventTZ.pm	2007-10-07 21:50:33 UTC (rev 3809)
@@ -0,0 +1,19 @@
+package DBICTest::Schema::EventTZ;
+
+use strict;
+use warnings;
+use base qw/DBIx::Class::Core/;
+
+__PACKAGE__->load_components(qw/InflateColumn::DateTime/);
+
+__PACKAGE__->table('event');
+
+__PACKAGE__->add_columns(
+  id => { data_type => 'integer', is_auto_increment => 1 },
+  starts_at => { data_type => 'datetime', extra => { timezone => "America/Chicago" } },
+  created_on => { data_type => 'timestamp', extra => { timezone => "America/Chicago" } },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+1;

Modified: DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema.pm
===================================================================
--- DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema.pm	2007-10-07 18:49:39 UTC (rev 3808)
+++ DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema.pm	2007-10-07 21:50:33 UTC (rev 3809)
@@ -34,7 +34,7 @@
     'Producer',
     'CD_to_Producer',
   ),
-  qw/SelfRefAlias TreeLike TwoKeyTreeLike Event NoPrimaryKey/,
+  qw/SelfRefAlias TreeLike TwoKeyTreeLike Event EventTZ NoPrimaryKey/,
   qw/Collection CollectionObject TypedObject/,
   qw/Owners BooksInLibrary/
 );




More information about the Bast-commits mailing list