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 This module figures out the type of DateTime::Format::* class to
53 inflate/deflate with based on the type of DBIx::Class::Storage::DBI::*
54 that you are using. If you switch from one database to a different
55 one your code should continue to work without modification (though note
56 that this feature is new as of 0.07, so it may not be perfect yet - bug
57 reports to the list very much welcome).
59 For more help with using components, see L<DBIx::Class::Manual::Component/USING>.
63 __PACKAGE__->load_components(qw/InflateColumn/);
65 __PACKAGE__->mk_group_accessors('simple' => '__datetime_parser');
67 =head2 register_column
69 Chains with the L<DBIx::Class::Row/register_column> method, and sets
70 up datetime columns appropriately. This would not normally be
71 directly called by end users.
73 In the case of an invalid date, L<DateTime> will throw an exception. To
74 bypass these exceptions and just have the inflation return undef, use
75 the C<datetime_undef_if_invalid> option in the column info:
79 data_type => "datetime",
80 default_value => '0000-00-00',
82 datetime_undef_if_invalid => 1
88 my ($self, $column, $info, @rest) = @_;
89 $self->next::method($column, $info, @rest);
90 return unless defined($info->{data_type});
94 for (qw/date datetime/) {
95 my $key = "inflate_${_}";
97 next unless exists $info->{$key};
98 return unless $info->{$key};
105 $type = lc($info->{data_type});
106 $type = 'datetime' if ($type =~ /^timestamp/);
110 if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) {
111 $timezone = $info->{extra}{timezone};
114 my $undef_if_invalid = $info->{datetime_undef_if_invalid};
116 if ($type eq 'datetime' || $type eq 'date') {
117 my ($parse, $format) = ("parse_${type}", "format_${type}");
119 # This assignment must happen here, otherwise Devel::Cycle treats
120 # the resulting deflator as a circular reference (go figure):
123 # DBICTest::Schema A->{source_registrations} => %B
124 # %B->{Event} => DBIx::Class::ResultSource::Table C
125 # DBIx::Class::ResultSource::Table C->{_columns} => %D
126 # %D->{created_on} => %E
127 # %E->{_inflate_info} => %F
128 # %F->{deflate} => &G
129 # closure &G, $info => $H
132 my $floating_tz_ok = $info->{extra}{floating_tz_ok};
134 $self->inflate_column(
138 my ($value, $obj) = @_;
139 my $dt = eval { $obj->_datetime_parser->$parse($value); };
140 die "Error while inflating ${value} for ${column} on ${self}: $@"
141 if $@ and not $undef_if_invalid;
142 $dt->set_time_zone($timezone) if $timezone;
146 my ($value, $obj) = @_;
148 warn "You're using a floating timezone, please see the documentation of"
149 . " DBIx::Class::InflateColumn::DateTime for an explanation"
150 if ref( $value->time_zone ) eq 'DateTime::TimeZone::Floating'
151 and not $floating_tz_ok
152 and not $ENV{DBIC_FLOATING_TZ_OK};
153 $value->set_time_zone($timezone);
155 $obj->_datetime_parser->$format($value);
162 sub _datetime_parser {
164 if (my $parser = $self->__datetime_parser) {
167 my $parser = $self->result_source->storage->datetime_parser(@_);
168 return $self->__datetime_parser($parser);
176 If you have a datetime column with the C<timezone> extra setting, and subsenquently
177 create/update this column with a DateTime object in the L<DateTime::TimeZone::Floating>
178 timezone, you will get a warning (as there is a very good chance this will not have the
179 result you expect). For example:
181 __PACKAGE__->add_columns(
182 starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago" } }
185 my $event = $schema->resultset('EventTZ')->create({
186 starts_at => DateTime->new(year=>2007, month=>12, day=>31, ),
189 The warning can be avoided in several ways:
193 =item Fix your broken code
195 When calling C<set_time_zone> on a Floating DateTime object, the timezone is simply
196 set to the requested value, and B<no time conversion takes place>. It is always a good idea
197 to be supply explicit times to the database:
199 my $event = $schema->resultset('EventTZ')->create({
200 starts_at => DateTime->new(year=>2007, month=>12, day=>31, time_zone => "America/Chicago" ),
203 =item Suppress the check on per-column basis
205 __PACKAGE__->add_columns(
206 starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago", floating_tz_ok => 1 } }
209 =item Suppress the check globally
211 Set the environment variable DBIC_FLOATING_TZ_OK to some true value.
221 =item More information about the add_columns method, and column metadata,
222 can be found in the documentation for L<DBIx::Class::ResultSource>.
224 =item Further discussion of problems inherent to the Floating timezone:
225 L<Floating DateTimes|DateTime/Floating_DateTimes>
226 and L<< $dt->set_time_zone|DateTime/"Set" Methods >>
232 Matt S. Trout <mst@shadowcatsystems.co.uk>
236 Aran Deltac <bluefeet@cpan.org>
240 You may distribute this code under the same terms as Perl itself.