Fix and simplify datetime method selection
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / InflateColumn / DateTime.pm
index 7b7e144..9bb7029 100644 (file)
@@ -3,7 +3,8 @@ package DBIx::Class::InflateColumn::DateTime;
 use strict;
 use warnings;
 use base qw/DBIx::Class/;
-use Carp::Clan qw/^DBIx::Class/;
+use DBIx::Class::Carp;
+use DBIx::Class::_Util 'dbic_internal_try';
 use Try::Tiny;
 use namespace::clean;
 
@@ -78,8 +79,8 @@ deflation/inflation as defined in the storage class. For instance, for
 a C<datetime> field the methods C<parse_datetime> and C<format_datetime>
 would be called on deflation/inflation. If the storage class does not
 provide a specialized inflator/deflator, C<[parse|format]_datetime> will
-be used as a fallback. See L<DateTime::Format> for more information on
-date formatting.
+be used as a fallback. See L<DateTime/Formatters And Stringification>
+for more information on date formatting.
 
 For more help with using components, see L<DBIx::Class::Manual::Component/USING>.
 
@@ -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;
@@ -169,13 +165,10 @@ sub register_column {
         inflate => sub {
           my ($value, $obj) = @_;
 
-          my $dt = try
-            { $obj->_inflate_to_datetime( $value, $infcopy ) }
-            catch {
-              $self->throw_exception ("Error while inflating ${value} for ${column} on ${self}: $_")
-                unless $infcopy->{datetime_undef_if_invalid};
-              undef;  # rv
-            };
+          # propagate for error reporting
+          $infcopy->{__dbic_colname} = $column;
+
+          my $dt = $obj->_inflate_to_datetime( $value, $infcopy );
 
           return (defined $dt)
             ? $obj->_post_inflate_datetime( $dt, $infcopy )
@@ -198,8 +191,16 @@ sub _flate_or_fallback
 
   my $parser = $self->_datetime_parser;
   my $preferred_method = sprintf($method_fmt, $info->{ _ic_dt_method });
-  my $method = $parser->can($preferred_method) ? $preferred_method : sprintf($method_fmt, 'datetime');
-  return $parser->$method($value);
+  my $method = $parser->can($preferred_method) || sprintf($method_fmt, 'datetime');
+
+  return dbic_internal_try {
+    $parser->$method($value);
+  }
+  catch {
+    $self->throw_exception ("Error while inflating '$value' for $info->{__dbic_colname} on ${self}: $_")
+      unless $info->{datetime_undef_if_invalid};
+    undef;  # rv
+  };
 }
 
 sub _inflate_to_datetime {
@@ -213,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 {
@@ -300,20 +301,18 @@ use the old way you'll see a warning - please fix your code then!
       can be found in the documentation for L<DBIx::Class::ResultSource>.
 
 =item Further discussion of problems inherent to the Floating timezone:
-      L<Floating DateTimes|DateTime/Floating_DateTimes>
+      L<Floating DateTimes|DateTime/Floating DateTimes>
       and L<< $dt->set_time_zone|DateTime/"Set" Methods >>
 
 =back
 
-=head1 AUTHOR
-
-Matt S. Trout <mst@shadowcatsystems.co.uk>
-
-=head1 CONTRIBUTORS
-
-Aran Deltac <bluefeet@cpan.org>
+=head1 FURTHER QUESTIONS?
 
-=head1 LICENSE
+Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
 
-You may distribute this code under the same terms as Perl itself.
+=head1 COPYRIGHT AND LICENSE
 
+This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
+by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
+redistribute it and/or modify it under the same terms as the
+L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.