added myself to CONTRIBUTORS
[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
22Then 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
dda9af55 27If 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
a97fe7e0 33If you want to inflate no matter what data_type your column is,
34use 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 44It'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
52This module figures out the type of DateTime::Format::* class to
53inflate/deflate with based on the type of DBIx::Class::Storage::DBI::*
54that you are using. If you switch from one database to a different
5d0a2955 55one your code should continue to work without modification (though note
56that this feature is new as of 0.07, so it may not be perfect yet - bug
57reports to the list very much welcome).
0b375b68 58
943f93f2 59For 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
69Chains with the L<DBIx::Class::Row/register_column> method, and sets
70up datetime columns appropriately. This would not normally be
71directly called by end users.
72
33a126ef 73In the case of an invalid date, L<DateTime> will throw an exception. To
74bypass these exceptions and just have the inflation return undef, use
75the 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 87sub register_column {
88 my ($self, $column, $info, @rest) = @_;
89 $self->next::method($column, $info, @rest);
c209c4fd 90 return unless defined($info->{data_type});
91 my $type = lc($info->{data_type});
f011970e 92 $type = 'datetime' if ($type =~ /^timestamp/);
ff8a6e3b 93 $type = 'datetime' if $info->{inflate_datetime};
94 $type = 'date' if $info->{inflate_date};
dda9af55 95 my $timezone;
96 if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) {
97 $timezone = $info->{extra}{timezone};
98 }
99
dceeddc7 100 my $undef_if_invalid = $info->{datetime_undef_if_invalid};
101
ff8a6e3b 102 my $do_inflate = 1;
103 $do_inflate = 0 if exists $info->{inflate_datetime} and $info->{inflate_datetime} == 0;
104 $do_inflate = 0 if exists $info->{inflate_date} and $info->{inflate_date} == 0;
105
106 if ($do_inflate and ($type eq 'datetime' || $type eq 'date')) {
c209c4fd 107 my ($parse, $format) = ("parse_${type}", "format_${type}");
445e5e31 108 $self->inflate_column(
109 $column =>
110 {
111 inflate => sub {
112 my ($value, $obj) = @_;
33a126ef 113 my $dt = eval { $obj->_datetime_parser->$parse($value); };
dceeddc7 114 die "Error while inflating ${value} for ${column} on ${self}: $@"
115 if $@ and not $undef_if_invalid;
dda9af55 116 $dt->set_time_zone($timezone) if $timezone;
117 return $dt;
445e5e31 118 },
119 deflate => sub {
120 my ($value, $obj) = @_;
dda9af55 121 $value->set_time_zone($timezone) if $timezone;
c209c4fd 122 $obj->_datetime_parser->$format($value);
445e5e31 123 },
124 }
125 );
126 }
127}
128
129sub _datetime_parser {
130 my $self = shift;
131 if (my $parser = $self->__datetime_parser) {
132 return $parser;
133 }
134 my $parser = $self->result_source->storage->datetime_parser(@_);
135 return $self->__datetime_parser($parser);
136}
137
1381;
0b375b68 139__END__
140
141=head1 SEE ALSO
142
143=over 4
144
145=item More information about the add_columns method, and column metadata,
146 can be found in the documentation for L<DBIx::Class::ResultSource>.
147
148=back
149
150=head1 AUTHOR
151
152Matt S. Trout <mst@shadowcatsystems.co.uk>
153
154=head1 CONTRIBUTORS
155
156Aran Deltac <bluefeet@cpan.org>
157
158=head1 LICENSE
159
160You may distribute this code under the same terms as Perl itself.
161