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 }
44 It's also possible to explicitly skip inflation:
46 __PACKAGE__->add_columns(
47 starts_when => { data_type => 'datetime', inflate_datetime => 0 }
52 You'll notice some warning about floating timezone if you set timezone in your schema but
53 didn't set it when creating/updating a row:
55 __PACKAGE__->add_columns(
56 starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago" } }
59 my $event = $schema->resultset('EventTZ')->create({
60 starts_at => DateTime->new(year=>2007, month=>12, day=>31, ),
63 To avoid this, you have three options:
67 =item Fix your broken code
69 my $event = $schema->resultset('EventTZ')->create({
70 starts_at => DateTime->new(year=>2007, month=>12, day=>31, time_zone => "America/Chicago" ),
73 =item Suppress the warning by doing either ...
75 __PACKAGE__->add_columns(
76 starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago", floating_tz_ok => 1 } }
81 Set environment variable DBIC_FLOATING_TZ_OK to some true value.
85 Please take a look at L<DateTime/Floating_DateTimes> for further information about floating
90 This module figures out the type of DateTime::Format::* class to
91 inflate/deflate with based on the type of DBIx::Class::Storage::DBI::*
92 that you are using. If you switch from one database to a different
93 one your code should continue to work without modification (though note
94 that this feature is new as of 0.07, so it may not be perfect yet - bug
95 reports to the list very much welcome).
97 For more help with using components, see L<DBIx::Class::Manual::Component/USING>.
101 __PACKAGE__->load_components(qw/InflateColumn/);
103 __PACKAGE__->mk_group_accessors('simple' => '__datetime_parser');
105 =head2 register_column
107 Chains with the L<DBIx::Class::Row/register_column> method, and sets
108 up datetime columns appropriately. This would not normally be
109 directly called by end users.
111 In the case of an invalid date, L<DateTime> will throw an exception. To
112 bypass these exceptions and just have the inflation return undef, use
113 the C<datetime_undef_if_invalid> option in the column info:
117 data_type => "datetime",
118 default_value => '0000-00-00',
120 datetime_undef_if_invalid => 1
125 sub register_column {
126 my ($self, $column, $info, @rest) = @_;
127 $self->next::method($column, $info, @rest);
128 return unless defined($info->{data_type});
132 for (qw/date datetime/) {
133 my $key = "inflate_${_}";
135 next unless exists $info->{$key};
136 return unless $info->{$key};
143 $type = lc($info->{data_type});
144 $type = 'datetime' if ($type =~ /^timestamp/);
148 if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) {
149 $timezone = $info->{extra}{timezone};
152 my $floating_tz_ok = $info->{extra}{floating_tz_ok} ? 1 : 0;
153 my $undef_if_invalid = $info->{datetime_undef_if_invalid};
155 if ($type eq 'datetime' || $type eq 'date') {
156 my ($parse, $format) = ("parse_${type}", "format_${type}");
157 $self->inflate_column(
161 my ($value, $obj) = @_;
162 my $dt = eval { $obj->_datetime_parser->$parse($value); };
163 die "Error while inflating ${value} for ${column} on ${self}: $@"
164 if $@ and not $undef_if_invalid;
165 $dt->set_time_zone($timezone) if $timezone;
169 my ($value, $obj) = @_;
171 warn "You're using a floating timezone, please see the documentation of"
172 . " DBIx::Class::InflateColumn::DateTime for an explanation"
173 if ref( $value->time_zone ) eq 'DateTime::TimeZone::Floating'
174 and not $floating_tz_ok
175 and not $ENV{DBIC_FLOATING_TZ_OK};
176 $value->set_time_zone($timezone);
178 $obj->_datetime_parser->$format($value);
185 sub _datetime_parser {
187 if (my $parser = $self->__datetime_parser) {
190 my $parser = $self->result_source->storage->datetime_parser(@_);
191 return $self->__datetime_parser($parser);
201 =item More information about the add_columns method, and column metadata,
202 can be found in the documentation for L<DBIx::Class::ResultSource>.
208 Matt S. Trout <mst@shadowcatsystems.co.uk>
212 Aran Deltac <bluefeet@cpan.org>
216 You may distribute this code under the same terms as Perl itself.