floating timezone warning
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / InflateColumn / DateTime.pm
1 package DBIx::Class::InflateColumn::DateTime;
2
3 use strict;
4 use warnings;
5 use base qw/DBIx::Class/;
6
7 =head1 NAME
8
9 DBIx::Class::InflateColumn::DateTime - Auto-create DateTime objects from date and datetime columns.
10
11 =head1 SYNOPSIS
12
13 Load this component and then declare one or more 
14 columns to be of the datetime, timestamp or date datatype.
15
16   package Event;
17   __PACKAGE__->load_components(qw/InflateColumn::DateTime Core/);
18   __PACKAGE__->add_columns(
19     starts_when => { data_type => 'datetime' }
20   );
21
22 Then you can treat the specified column as a L<DateTime> object.
23
24   print "This event starts the month of ".
25     $event->starts_when->month_name();
26
27 If you want to set a specific timezone for that field, use:
28
29   __PACKAGE__->add_columns(
30     starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago" } }
31   );
32
33 If you want to inflate no matter what data_type your column is,
34 use inflate_datetime or inflate_date:
35
36   __PACKAGE__->add_columns(
37     starts_when => { data_type => 'varchar', inflate_datetime => 1 }
38   );
39   
40   __PACKAGE__->add_columns(
41     starts_when => { data_type => 'varchar', inflate_date => 1 }
42   );
43
44 It's also possible to explicitly skip inflation:
45   
46   __PACKAGE__->add_columns(
47     starts_when => { data_type => 'datetime', inflate_datetime => 0 }
48   );
49
50 =head1 WARNING
51
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:
54
55   __PACKAGE__->add_columns(
56     starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago" } }
57   );
58
59   my $event = $schema->resultset('EventTZ')->create({
60     starts_at => DateTime->new(year=>2007, month=>12, day=>31, ),
61   });
62
63 To avoid this, you have three options:
64
65 =over
66
67 =item Fix your broken code
68
69   my $event = $schema->resultset('EventTZ')->create({
70     starts_at => DateTime->new(year=>2007, month=>12, day=>31, time_zone => "America/Chicago" ),
71   });
72
73 =item Suppress the warning by doing either ...
74
75   __PACKAGE__->add_columns(
76     starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago", floating_tz_ok => 1 } }
77   );
78
79 =item ... or ...
80
81 Set environment variable DBIC_FLOATING_TZ_OK to some true value.
82
83 =back
84
85
86 =head1 DESCRIPTION
87
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).
94
95 For more help with using components, see L<DBIx::Class::Manual::Component/USING>.
96
97 =cut
98
99 __PACKAGE__->load_components(qw/InflateColumn/);
100
101 __PACKAGE__->mk_group_accessors('simple' => '__datetime_parser');
102
103 =head2 register_column
104
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.
108
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:
112   
113     "broken_date",
114     {
115         data_type => "datetime",
116         default_value => '0000-00-00',
117         is_nullable => 1,
118         datetime_undef_if_invalid => 1
119     }
120
121 =cut
122
123 sub register_column {
124   my ($self, $column, $info, @rest) = @_;
125   $self->next::method($column, $info, @rest);
126   return unless defined($info->{data_type});
127
128   my $type;
129
130   for (qw/date datetime/) {
131     my $key = "inflate_${_}";
132
133     next unless exists $info->{$key};
134     return unless $info->{$key};
135
136     $type = $_;
137     last;
138   }
139
140   unless ($type) {
141     $type = lc($info->{data_type});
142     $type = 'datetime' if ($type =~ /^timestamp/);
143   }
144
145   my $timezone;
146   if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) {
147     $timezone = $info->{extra}{timezone};
148   }
149
150   my $floating_tz_ok   = $info->{extra}{floating_tz_ok} ? 1 : 0;
151   my $undef_if_invalid = $info->{datetime_undef_if_invalid};
152
153   if ($type eq 'datetime' || $type eq 'date') {
154     my ($parse, $format) = ("parse_${type}", "format_${type}");
155     $self->inflate_column(
156       $column =>
157         {
158           inflate => sub {
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;
164             return $dt;
165           },
166           deflate => sub {
167             my ($value, $obj) = @_;
168             if ($timezone) {
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);
175             }
176             $obj->_datetime_parser->$format($value);
177           },
178         }
179     );
180   }
181 }
182
183 sub _datetime_parser {
184   my $self = shift;
185   if (my $parser = $self->__datetime_parser) {
186     return $parser;
187   }
188   my $parser = $self->result_source->storage->datetime_parser(@_);
189   return $self->__datetime_parser($parser);
190 }
191
192 1;
193 __END__
194
195 =head1 SEE ALSO
196
197 =over 4
198
199 =item More information about the add_columns method, and column metadata, 
200       can be found in the documentation for L<DBIx::Class::ResultSource>.
201
202 =back
203
204 =head1 AUTHOR
205
206 Matt S. Trout <mst@shadowcatsystems.co.uk>
207
208 =head1 CONTRIBUTORS
209
210 Aran Deltac <bluefeet@cpan.org>
211
212 =head1 LICENSE
213
214 You may distribute this code under the same terms as Perl itself.
215