X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FInflateColumn%2FDateTime.pm;h=35aa3e65b4e1f0f7b5d9c99e0515a3e853b99938;hb=88ac5c8b0eaabf0e0b2e3cc439d8c8635a325b36;hp=54e66f5c454e6bd5448480ca7e3f41ed0ef0ec87;hpb=6c99a3eea0da8f254e67fdda21e27f9ef9fbeec3;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/InflateColumn/DateTime.pm b/lib/DBIx/Class/InflateColumn/DateTime.pm index 54e66f5..35aa3e6 100644 --- a/lib/DBIx/Class/InflateColumn/DateTime.pm +++ b/lib/DBIx/Class/InflateColumn/DateTime.pm @@ -18,6 +18,7 @@ columns to be of the datetime, timestamp or date datatype. __PACKAGE__->load_components(qw/InflateColumn::DateTime Core/); __PACKAGE__->add_columns( starts_when => { data_type => 'datetime' } + create_date => { data_type => 'date' } ); NOTE: You B load C B C. See @@ -40,17 +41,26 @@ use inflate_datetime or inflate_date: __PACKAGE__->add_columns( starts_when => { data_type => 'varchar', inflate_datetime => 1 } ); - + __PACKAGE__->add_columns( starts_when => { data_type => 'varchar', inflate_date => 1 } ); It's also possible to explicitly skip inflation: - + __PACKAGE__->add_columns( starts_when => { data_type => 'datetime', inflate_datetime => 0 } ); +NOTE: Don't rely on C to parse date strings for you. +The column is set directly for any non-references and C +is completely bypassed. Instead, use an input parser to create a DateTime +object. For instance, if your user input comes as a 'YYYY-MM-DD' string, you can +use C thusly: + + use DateTime::Format::ISO8601; + my $dt = DateTime::Format::ISO8601->parse_datetime('YYYY-MM-DD'); + =head1 DESCRIPTION This module figures out the type of DateTime::Format::* class to @@ -60,6 +70,16 @@ one your code should continue to work without modification (though note that this feature is new as of 0.07, so it may not be perfect yet - bug reports to the list very much welcome). +If the data_type of a field is C, C or C (or +a derivative of these datatypes, e.g. C), this +module will automatically call the appropriate parse/format method for +deflation/inflation as defined in the storage class. For instance, for +a C field the methods C and C +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 for more information on +date formatting. + For more help with using components, see L. =cut @@ -77,7 +97,7 @@ directly called by end users. In the case of an invalid date, L will throw an exception. To bypass these exceptions and just have the inflation return undef, use the C option in the column info: - + "broken_date", { data_type => "datetime", @@ -110,23 +130,29 @@ sub register_column { if ($type eq "timestamp with time zone" || $type eq "timestamptz") { $type = "timestamp"; $info->{_ic_dt_method} ||= "timestamp_with_timezone"; + } elsif ($type eq "timestamp without time zone") { + $type = "timestamp"; + $info->{_ic_dt_method} ||= "timestamp_without_timezone"; + } elsif ($type eq "smalldatetime") { + $type = "datetime"; + $info->{_ic_dt_method} ||= "datetime"; } } my $timezone; if ( defined $info->{extra}{timezone} ) { carp "Putting timezone into extra => { timezone => '...' } has been deprecated, ". - "please put it directly into the columns definition."; + "please put it directly into the '$column' column definition."; $timezone = $info->{extra}{timezone}; } my $locale; if ( defined $info->{extra}{locale} ) { carp "Putting locale into extra => { locale => '...' } has been deprecated, ". - "please put it directly into the columns definition."; + "please put it directly into the '$column' column definition."; $locale = $info->{extra}{locale}; } - + $locale = $info->{locale} if defined $info->{locale}; $timezone = $info->{timezone} if defined $info->{timezone}; @@ -139,7 +165,7 @@ sub register_column { if (defined $info->{extra}{floating_tz_ok}) { carp "Putting floating_tz_ok into extra => { floating_tz_ok => 1 } has been deprecated, ". - "please put it directly into the columns definition."; + "please put it directly into the '$column' column definition."; $info{floating_tz_ok} = $info->{extra}{floating_tz_ok}; } @@ -148,9 +174,13 @@ sub register_column { { inflate => sub { my ($value, $obj) = @_; + my $dt = eval { $obj->_inflate_to_datetime( $value, \%info ) }; - $self->throw_exception ("Error while inflating ${value} for ${column} on ${self}: $@") - if $@ and not $undef_if_invalid; + if (my $err = $@ ) { + return undef if ($undef_if_invalid); + $self->throw_exception ("Error while inflating ${value} for ${column} on ${self}: $err"); + } + $dt->set_time_zone($timezone) if $timezone; $dt->set_locale($locale) if $locale; return $dt; @@ -207,7 +237,7 @@ __END__ =head1 USAGE NOTES -If you have a datetime column with the C extra setting, and subsenquently +If you have a datetime column with an associated C, and subsequently create/update this column with a DateTime object in the L timezone, you will get a warning (as there is a very good chance this will not have the result you expect). For example: