Commit | Line | Data |
445e5e31 |
1 | package DBIx::Class::InflateColumn::DateTime; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use base qw/DBIx::Class/; |
6 | |
0b375b68 |
7 | =head1 NAME |
8 | |
5d0a2955 |
9 | DBIx::Class::InflateColumn::DateTime - Auto-create DateTime objects from date and datetime columns. |
0b375b68 |
10 | |
11 | =head1 SYNOPSIS |
12 | |
13 | Load this component and then declare one or more |
679a304a |
14 | columns to be of the datetime, timestamp or date datatype. |
0b375b68 |
15 | |
16 | package Event; |
517d5d64 |
17 | __PACKAGE__->load_components(qw/InflateColumn::DateTime Core/); |
0b375b68 |
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 | |
8d689133 |
27 | If you want to set a specific timezone and locale for that field, use: |
dda9af55 |
28 | |
29 | __PACKAGE__->add_columns( |
8d689133 |
30 | starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago", locale => "de_DE" } } |
dda9af55 |
31 | ); |
32 | |
a97fe7e0 |
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 | |
ff8a6e3b |
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 | |
0b375b68 |
50 | =head1 DESCRIPTION |
51 | |
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 |
5d0a2955 |
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). |
0b375b68 |
58 | |
943f93f2 |
59 | For more help with using components, see L<DBIx::Class::Manual::Component/USING>. |
de78905b |
60 | |
0b375b68 |
61 | =cut |
62 | |
445e5e31 |
63 | __PACKAGE__->load_components(qw/InflateColumn/); |
64 | |
65 | __PACKAGE__->mk_group_accessors('simple' => '__datetime_parser'); |
66 | |
9b83fccd |
67 | =head2 register_column |
68 | |
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. |
72 | |
33a126ef |
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: |
76 | |
77 | "broken_date", |
78 | { |
79 | data_type => "datetime", |
80 | default_value => '0000-00-00', |
81 | is_nullable => 1, |
82 | datetime_undef_if_invalid => 1 |
83 | } |
84 | |
9b83fccd |
85 | =cut |
86 | |
445e5e31 |
87 | sub register_column { |
88 | my ($self, $column, $info, @rest) = @_; |
89 | $self->next::method($column, $info, @rest); |
c209c4fd |
90 | return unless defined($info->{data_type}); |
bb90689c |
91 | |
92 | my $type; |
93 | |
94 | for (qw/date datetime/) { |
95 | my $key = "inflate_${_}"; |
96 | |
97 | next unless exists $info->{$key}; |
98 | return unless $info->{$key}; |
99 | |
100 | $type = $_; |
101 | last; |
102 | } |
103 | |
104 | unless ($type) { |
105 | $type = lc($info->{data_type}); |
106 | $type = 'datetime' if ($type =~ /^timestamp/); |
107 | } |
108 | |
dda9af55 |
109 | my $timezone; |
8d689133 |
110 | if ( defined $info->{extra}{timezone} ) { |
dda9af55 |
111 | $timezone = $info->{extra}{timezone}; |
112 | } |
113 | |
8d689133 |
114 | my $locale; |
115 | if ( defined $info->{extra}{locale} ) { |
116 | $locale = $info->{extra}{locale}; |
117 | } |
118 | |
dceeddc7 |
119 | my $undef_if_invalid = $info->{datetime_undef_if_invalid}; |
120 | |
bb90689c |
121 | if ($type eq 'datetime' || $type eq 'date') { |
c209c4fd |
122 | my ($parse, $format) = ("parse_${type}", "format_${type}"); |
45147005 |
123 | |
124 | # This assignment must happen here, otherwise Devel::Cycle treats |
125 | # the resulting deflator as a circular reference (go figure): |
126 | # |
127 | # Cycle #1 |
128 | # DBICTest::Schema A->{source_registrations} => %B |
129 | # %B->{Event} => DBIx::Class::ResultSource::Table C |
130 | # DBIx::Class::ResultSource::Table C->{_columns} => %D |
131 | # %D->{created_on} => %E |
132 | # %E->{_inflate_info} => %F |
133 | # %F->{deflate} => &G |
134 | # closure &G, $info => $H |
135 | # $H => %E |
136 | # |
137 | my $floating_tz_ok = $info->{extra}{floating_tz_ok}; |
138 | |
445e5e31 |
139 | $self->inflate_column( |
140 | $column => |
141 | { |
142 | inflate => sub { |
143 | my ($value, $obj) = @_; |
33a126ef |
144 | my $dt = eval { $obj->_datetime_parser->$parse($value); }; |
dceeddc7 |
145 | die "Error while inflating ${value} for ${column} on ${self}: $@" |
146 | if $@ and not $undef_if_invalid; |
dda9af55 |
147 | $dt->set_time_zone($timezone) if $timezone; |
8d689133 |
148 | $dt->set_locale($locale) if $locale; |
dda9af55 |
149 | return $dt; |
445e5e31 |
150 | }, |
151 | deflate => sub { |
152 | my ($value, $obj) = @_; |
47cd9169 |
153 | if ($timezone) { |
154 | warn "You're using a floating timezone, please see the documentation of" |
155 | . " DBIx::Class::InflateColumn::DateTime for an explanation" |
156 | if ref( $value->time_zone ) eq 'DateTime::TimeZone::Floating' |
157 | and not $floating_tz_ok |
158 | and not $ENV{DBIC_FLOATING_TZ_OK}; |
159 | $value->set_time_zone($timezone); |
8d689133 |
160 | $value->set_locale($locale) if $locale; |
47cd9169 |
161 | } |
c209c4fd |
162 | $obj->_datetime_parser->$format($value); |
445e5e31 |
163 | }, |
164 | } |
165 | ); |
166 | } |
167 | } |
168 | |
169 | sub _datetime_parser { |
170 | my $self = shift; |
171 | if (my $parser = $self->__datetime_parser) { |
172 | return $parser; |
173 | } |
174 | my $parser = $self->result_source->storage->datetime_parser(@_); |
175 | return $self->__datetime_parser($parser); |
176 | } |
177 | |
178 | 1; |
0b375b68 |
179 | __END__ |
180 | |
45147005 |
181 | =head1 USAGE NOTES |
182 | |
183 | If you have a datetime column with the C<timezone> extra setting, and subsenquently |
184 | create/update this column with a DateTime object in the L<DateTime::TimeZone::Floating> |
185 | timezone, you will get a warning (as there is a very good chance this will not have the |
186 | result you expect). For example: |
187 | |
188 | __PACKAGE__->add_columns( |
189 | starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago" } } |
190 | ); |
191 | |
192 | my $event = $schema->resultset('EventTZ')->create({ |
193 | starts_at => DateTime->new(year=>2007, month=>12, day=>31, ), |
194 | }); |
195 | |
196 | The warning can be avoided in several ways: |
197 | |
198 | =over |
199 | |
200 | =item Fix your broken code |
201 | |
202 | When calling C<set_time_zone> on a Floating DateTime object, the timezone is simply |
203 | set to the requested value, and B<no time conversion takes place>. It is always a good idea |
204 | to be supply explicit times to the database: |
205 | |
206 | my $event = $schema->resultset('EventTZ')->create({ |
207 | starts_at => DateTime->new(year=>2007, month=>12, day=>31, time_zone => "America/Chicago" ), |
208 | }); |
209 | |
210 | =item Suppress the check on per-column basis |
211 | |
212 | __PACKAGE__->add_columns( |
213 | starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago", floating_tz_ok => 1 } } |
214 | ); |
215 | |
216 | =item Suppress the check globally |
217 | |
218 | Set the environment variable DBIC_FLOATING_TZ_OK to some true value. |
219 | |
220 | =back |
221 | |
222 | |
223 | |
0b375b68 |
224 | =head1 SEE ALSO |
225 | |
226 | =over 4 |
227 | |
228 | =item More information about the add_columns method, and column metadata, |
229 | can be found in the documentation for L<DBIx::Class::ResultSource>. |
230 | |
45147005 |
231 | =item Further discussion of problems inherent to the Floating timezone: |
232 | L<Floating DateTimes|DateTime/Floating_DateTimes> |
233 | and L<< $dt->set_time_zone|DateTime/"Set" Methods >> |
234 | |
0b375b68 |
235 | =back |
236 | |
237 | =head1 AUTHOR |
238 | |
239 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
240 | |
241 | =head1 CONTRIBUTORS |
242 | |
243 | Aran Deltac <bluefeet@cpan.org> |
244 | |
245 | =head1 LICENSE |
246 | |
247 | You may distribute this code under the same terms as Perl itself. |
248 | |