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.
88 This module figures out the type of DateTime::Format::* class to
89 inflate/deflate with based on the type of DBIx::Class::Storage::DBI::*
90 that you are using. If you switch from one database to a different
91 one your code should continue to work without modification (though note
92 that this feature is new as of 0.07, so it may not be perfect yet - bug
93 reports to the list very much welcome).
95 For more help with using components, see L<DBIx::Class::Manual::Component/USING>.
99 __PACKAGE__->load_components(qw/InflateColumn/);
101 __PACKAGE__->mk_group_accessors('simple' => '__datetime_parser');
103 =head2 register_column
105 Chains with the L<DBIx::Class::Row/register_column> method, and sets
106 up datetime columns appropriately. This would not normally be
107 directly called by end users.
109 In the case of an invalid date, L<DateTime> will throw an exception. To
110 bypass these exceptions and just have the inflation return undef, use
111 the C<datetime_undef_if_invalid> option in the column info:
115 data_type => "datetime",
116 default_value => '0000-00-00',
118 datetime_undef_if_invalid => 1
123 sub register_column {
124 my ($self, $column, $info, @rest) = @_;
125 $self->next::method($column, $info, @rest);
126 return unless defined($info->{data_type});
130 for (qw/date datetime/) {
131 my $key = "inflate_${_}";
133 next unless exists $info->{$key};
134 return unless $info->{$key};
141 $type = lc($info->{data_type});
142 $type = 'datetime' if ($type =~ /^timestamp/);
146 if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) {
147 $timezone = $info->{extra}{timezone};
150 my $floating_tz_ok = $info->{extra}{floating_tz_ok} ? 1 : 0;
151 my $undef_if_invalid = $info->{datetime_undef_if_invalid};
153 if ($type eq 'datetime' || $type eq 'date') {
154 my ($parse, $format) = ("parse_${type}", "format_${type}");
155 $self->inflate_column(
159 my ($value, $obj) = @_;
160 my $dt = eval { $obj->_datetime_parser->$parse($value); };
161 die "Error while inflating ${value} for ${column} on ${self}: $@"
162 if $@ and not $undef_if_invalid;
163 $dt->set_time_zone($timezone) if $timezone;
167 my ($value, $obj) = @_;
169 warn "You're using a floating timezone, please see the documentation of"
170 . " DBIx::Class::InflateColumn::DateTime for an explanation"
171 if ref( $value->time_zone ) eq 'DateTime::TimeZone::Floating'
172 and not $floating_tz_ok
173 and not $ENV{DBIC_FLOATING_TZ_OK};
174 $value->set_time_zone($timezone);
176 $obj->_datetime_parser->$format($value);
183 sub _datetime_parser {
185 if (my $parser = $self->__datetime_parser) {
188 my $parser = $self->result_source->storage->datetime_parser(@_);
189 return $self->__datetime_parser($parser);
199 =item More information about the add_columns method, and column metadata,
200 can be found in the documentation for L<DBIx::Class::ResultSource>.
206 Matt S. Trout <mst@shadowcatsystems.co.uk>
210 Aran Deltac <bluefeet@cpan.org>
214 You may distribute this code under the same terms as Perl itself.