| 1 | package Jifty::DBI::Filter::DateTime; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use base qw|Jifty::DBI::Filter|; | ||
| 7 | use DateTime (); | ||
| 8 | use DateTime::Format::ISO8601 (); | ||
| 9 | use DateTime::Format::Strptime (); | ||
| 10 | use Carp (); | ||
| 11 | |||
| 12 | use constant _time_zone => ''; | ||
| 13 | use constant _strptime => '%Y-%m-%d %H:%M:%S'; | ||
| 14 | |||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Jifty::DBI::Filter::DateTime - DateTime object wrapper around date columns | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | This filter allow you to work with DateTime objects instead of | ||
| 23 | plain text dates. If the column type is "date", then the hour, | ||
| 24 | minute, and second information is discarded when encoding. | ||
| 25 | |||
| 26 | =head2 encode | ||
| 27 | |||
| 28 | If value is DateTime object then converts it into ISO format | ||
| 29 | C<YYYY-MM-DD hh:mm:ss>. Does nothing if value is not defined or | ||
| 30 | string. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | sub encode { | ||
| 35 | my $self = shift; | ||
| 36 | |||
| 37 | my $value_ref = $self->value_ref; | ||
| 38 | return unless $$value_ref; | ||
| 39 | |||
| 40 | return unless UNIVERSAL::isa( $$value_ref, 'DateTime' ); | ||
| 41 | |||
| 42 | if (my $tz = $self->_time_zone) { | ||
| 43 | $$value_ref = $$value_ref->clone; | ||
| 44 | $$value_ref->time_zone('floating'); | ||
| 45 | } | ||
| 46 | $$value_ref = $$value_ref->strftime($self->_strptime); | ||
| 47 | return 1; | ||
| 48 | } | ||
| 49 | |||
| 50 | =head2 decode | ||
| 51 | |||
| 52 | If value is defined then converts it into DateTime object otherwise do | ||
| 53 | nothing. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub decode { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | my $value_ref = $self->value_ref; | ||
| 61 | return unless defined $$value_ref; | ||
| 62 | |||
| 63 | # XXX: Looks like we should use special modules for parsing DT because | ||
| 64 | # different MySQL versions can return DT in different formats(none strict ISO) | ||
| 65 | # Pg has also special format that depends on "european" and | ||
| 66 | # server time_zone, by default ISO | ||
| 67 | # other DBs may have own formats(Interbase for example can be forced to use special format) | ||
| 68 | # but we need Jifty::DBI::Handle here to get DB type | ||
| 69 | |||
| 70 | my $str = join('T', split ' ', $$value_ref, 2); | ||
| 71 | my $dt; | ||
| 72 | eval { $dt = DateTime::Format::ISO8601->parse_datetime($str) }; | ||
| 73 | |||
| 74 | if ($@) { # if datetime can't decode this, scream loudly with a useful error message | ||
| 75 | Carp::cluck($@); | ||
| 76 | return; | ||
| 77 | } | ||
| 78 | |||
| 79 | if ($dt) { | ||
| 80 | my $tz = $self->_time_zone; | ||
| 81 | $dt->time_zone($tz) if $tz; | ||
| 82 | |||
| 83 | $dt->set_formatter(DateTime::Format::Strptime->new(pattern => $self->_strptime)); | ||
| 84 | $$value_ref = $dt; | ||
| 85 | } else { | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | =head1 SEE ALSO | ||
| 91 | |||
| 92 | L<Jifty::DBI::Filter>, L<DateTime> | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | 1; |