| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Jifty::Filter::DateTime -- A Jifty::DBI filter to work with | ||
| 7 | Jifty::DateTime objects | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | # use it with Jifty::DBI::Filter::Date or J::D::F::DateTime | ||
| 12 | column created => | ||
| 13 | type is 'timestamp', | ||
| 14 | filters are qw( Jifty::Filter::DateTime Jifty::DBI::Filter::DateTime), | ||
| 15 | label is 'Created', | ||
| 16 | is immutable; | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Jifty::Filter::DateTime promotes DateTime objects to Jifty::DateTime (or your | ||
| 21 | application's DateTime subclass) objects on load. This has the side effect of | ||
| 22 | setting their time zone based on the record's current user's preferred time | ||
| 23 | zone, when available. | ||
| 24 | |||
| 25 | This is intended to be combined with C<Jifty::DBI::Filter::Date> or | ||
| 26 | C<Jifty::DBI::Filter::DateTime>, e.g. | ||
| 27 | |||
| 28 | column created => | ||
| 29 | type is 'timestamp', | ||
| 30 | filters are qw( Jifty::Filter::DateTime Jifty::DBI::Filter::DateTime), | ||
| 31 | label is 'Created', | ||
| 32 | is immutable; | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | package Jifty::Filter::DateTime; | ||
| 37 | use base qw(Jifty::DBI::Filter); | ||
| 38 | |||
| 39 | |||
| 40 | =head2 decode | ||
| 41 | |||
| 42 | If the value is a DateTime, replace it with a Jifty::DateTime (or your | ||
| 43 | application's subclass) representing the same time, setting the time zone in | ||
| 44 | the process. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | |||
| 49 | sub decode { | ||
| 50 | my $self = shift; | ||
| 51 | my $value_ref = $self->value_ref; | ||
| 52 | |||
| 53 | return unless ref($$value_ref) && $$value_ref->isa('DateTime'); | ||
| 54 | |||
| 55 | # XXX There has to be a better way to do this | ||
| 56 | my %args; | ||
| 57 | for (qw(year month day hour minute second nanosecond formatter time_zone)) { | ||
| 58 | $args{$_} = $$value_ref->$_ if(defined($$value_ref->$_)); | ||
| 59 | } | ||
| 60 | |||
| 61 | $args{input_time_zone} = delete $args{time_zone}; | ||
| 62 | |||
| 63 | my $class = Jifty->app_class('DateTime'); | ||
| 64 | $class = 'Jifty::DateTime' unless $class->can('new'); | ||
| 65 | my $dt = $class->new(%args); | ||
| 66 | |||
| 67 | $$value_ref = $dt; | ||
| 68 | } | ||
| 69 | |||
| 70 | =head1 SEE ALSO | ||
| 71 | |||
| 72 | L<Jifty::DBI::Filter::Date>, L<Jifty::DBI::Filter::DateTime>, | ||
| 73 | L<Jifty::DateTime> | ||
| 74 | |||
| 75 | =head1 LICENSE | ||
| 76 | |||
| 77 | Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. | ||
| 78 | Jifty is distributed under the same terms as Perl itself. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | 1; |