Fix and simplify datetime method selection
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / InflateColumn / DateTime.pm
index 7abf5ac..9bb7029 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 use base qw/DBIx::Class/;
 use DBIx::Class::Carp;
+use DBIx::Class::_Util 'dbic_internal_try';
 use Try::Tiny;
 use namespace::clean;
 
@@ -131,17 +132,12 @@ sub register_column {
 
   # _ic_dt_method will follow whatever the registration requests
   # thus = instead of ||=
-  if ($data_type eq 'timestamp with time zone' || $data_type eq 'timestamptz') {
-    $info->{_ic_dt_method} = 'timestamp_with_timezone';
-  }
-  elsif ($data_type eq 'timestamp without time zone') {
-    $info->{_ic_dt_method} = 'timestamp_without_timezone';
-  }
-  elsif ($data_type eq 'smalldatetime') {
-    $info->{_ic_dt_method} = 'smalldatetime';
-  }
-  elsif ($data_type =~ /^ (?: date | datetime | timestamp ) $/x) {
-    $info->{_ic_dt_method} = $data_type;
+  if ( $data_type =~ /\A (?:
+      timestamp \s+ with(?:out)? \s+ time \s+ zone |
+      date | (small)? datetime | timestamp(?:tz)?
+    ) \z/x
+  ) {
+    ($info->{_ic_dt_method} = $data_type) =~ s/\s+/_/g;
   }
   elsif ($requested_type) {
     $info->{_ic_dt_method} = $requested_type;
@@ -197,7 +193,7 @@ sub _flate_or_fallback
   my $preferred_method = sprintf($method_fmt, $info->{ _ic_dt_method });
   my $method = $parser->can($preferred_method) || sprintf($method_fmt, 'datetime');
 
-  return try {
+  return dbic_internal_try {
     $parser->$method($value);
   }
   catch {
@@ -218,7 +214,7 @@ sub _deflate_from_datetime {
 }
 
 sub _datetime_parser {
-  shift->result_source->storage->datetime_parser (@_);
+  shift->result_source->schema->storage->datetime_parser (@_);
 }
 
 sub _post_inflate_datetime {