1 package DBIx::Class::InflateColumn::DateTime;
5 use base qw/DBIx::Class/;
9 DBIx::Class::InflateColumn::DateTime - Auto-create DateTime objects from date and datetime columns.
13 Load this component and then declare one or more
14 columns to be of the datetime, timestamp or date datatype.
17 __PACKAGE__->load_components(qw/InflateColumn::DateTime Core/);
18 __PACKAGE__->add_columns(
19 starts_when => { data_type => 'datetime' }
22 Then you can treat the specified column as a L<DateTime> object.
24 print "This event starts the month of ".
25 $event->starts_when->month_name();
27 If you want to set a specific timezone for that field, use:
29 __PACKAGE__->add_columns(
30 starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago" } }
33 If you want to inflate no matter what data_type your column is,
34 use inflate_datetime or inflate_date:
36 __PACKAGE__->add_columns(
37 starts_when => { data_type => 'varchar', inflate_datetime => 1 }
40 __PACKAGE__->add_columns(
41 starts_when => { data_type => 'varchar', inflate_date => 1 }
46 This module figures out the type of DateTime::Format::* class to
47 inflate/deflate with based on the type of DBIx::Class::Storage::DBI::*
48 that you are using. If you switch from one database to a different
49 one your code should continue to work without modification (though note
50 that this feature is new as of 0.07, so it may not be perfect yet - bug
51 reports to the list very much welcome).
53 For more help with using components, see L<DBIx::Class::Manual::Component/USING>.
57 __PACKAGE__->load_components(qw/InflateColumn/);
59 __PACKAGE__->mk_group_accessors('simple' => '__datetime_parser');
61 =head2 register_column
63 Chains with the L<DBIx::Class::Row/register_column> method, and sets
64 up datetime columns appropriately. This would not normally be
65 directly called by end users.
67 In the case of an invalid date, L<DateTime> will throw an exception. To
68 bypass these exceptions and just have the inflation return undef, use
69 the C<datetime_undef_if_invalid> option in the column info:
73 data_type => "datetime",
74 default_value => '0000-00-00',
76 datetime_undef_if_invalid => 1
82 my ($self, $column, $info, @rest) = @_;
83 $self->next::method($column, $info, @rest);
84 return unless defined($info->{data_type});
85 my $type = lc($info->{data_type});
86 $type = 'datetime' if ($type =~ /^timestamp/);
87 $type = 'datetime' if exists $info->{inflate_datetime} and $info->{inflate_datetime};
88 $type = 'date' if exists $info->{inflate_date} and $info->{inflate_date};
90 if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) {
91 $timezone = $info->{extra}{timezone};
94 my $undef_if_invalid = $info->{datetime_undef_if_invalid};
96 if ($type eq 'datetime' || $type eq 'date') {
97 my ($parse, $format) = ("parse_${type}", "format_${type}");
98 $self->inflate_column(
102 my ($value, $obj) = @_;
103 my $dt = eval { $obj->_datetime_parser->$parse($value); };
104 die "Error while inflating ${value} for ${column} on ${self}: $@"
105 if $@ and not $undef_if_invalid;
106 $dt->set_time_zone($timezone) if $timezone;
110 my ($value, $obj) = @_;
111 $value->set_time_zone($timezone) if $timezone;
112 $obj->_datetime_parser->$format($value);
119 sub _datetime_parser {
121 if (my $parser = $self->__datetime_parser) {
124 my $parser = $self->result_source->storage->datetime_parser(@_);
125 return $self->__datetime_parser($parser);
135 =item More information about the add_columns method, and column metadata,
136 can be found in the documentation for L<DBIx::Class::ResultSource>.
142 Matt S. Trout <mst@shadowcatsystems.co.uk>
146 Aran Deltac <bluefeet@cpan.org>
150 You may distribute this code under the same terms as Perl itself.