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