| 1 | package Jifty::DBI::Filter::DateTime; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base qw|Jifty::DBI::Filter Class::Data::Inheritable|; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use DateTime (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use DateTime::Format::ISO8601 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use DateTime::Format::Strptime (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use constant _time_zone => 'UTC'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use constant _strptime => '%Y-%m-%d %H:%M:%S'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use constant _parser => DateTime::Format::ISO8601->new(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use constant date_only => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Jifty::DBI::Filter::DateTime - DateTime object wrapper around date columns | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | This filter allow you to work with DateTime objects instead of | ||||||||||||||||||||||||||||
| 24 | plain text dates. If the column type is "date", then the hour, | ||||||||||||||||||||||||||||
| 25 | minute, and second information is discarded when encoding. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Both input and output will always be coerced into UTC (or, in the case of | ||||||||||||||||||||||||||||
| 28 | Dates, the Floating timezone) for consistency. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head2 formatter | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | This is an instance of the DateTime::Format object used for inflating the | ||||||||||||||||||||||||||||
| 33 | string in the database to a DateTime object. By default it is a | ||||||||||||||||||||||||||||
| 34 | L<DateTime::Format::Strptime> object that uses the C<_strptime> method as its | ||||||||||||||||||||||||||||
| 35 | pattern. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | You can use the _formatter classdata storage as a cache so you don't need | ||||||||||||||||||||||||||||
| 38 | to re-instantiate your format object every C<decode>. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =cut | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3 | __PACKAGE__->mk_classdata("_formatter"); | |||||||||||||||||||||||||||
| 43 | sub formatter { | ||||||||||||||||||||||||||||
| 44 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
formatter: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 12 | ▶ | if ( not $self->_formatter | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->_formatter or $self->_formatter->pattern ne $self->_strptime
MC/DC100%2/2
not $self->_formatter or $self->_formatter->pattern ne $self->_strptime
not $self->_formatter
$self->_formatter->pattern ne $self->_strptime
Truth table100%3/3
not(A) or B
A= not $self->_formatterB= $self->_formatter->pattern ne $self->_strptime
| |||||||||||||||||||||||||||||
| 46 | or $self->_formatter->pattern ne $self->_strptime ) | ||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 9 | $self->_formatter(DateTime::Format::Strptime->new(pattern => $self->_strptime)); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 12 | return $self->_formatter; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head2 encode | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | If value is DateTime object then converts it into ISO format | ||||||||||||||||||||||||||||
| 56 | C<YYYY-MM-DD hh:mm:ss>. Does nothing if value is not defined. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Sets the value to undef if the value is a string and doesn't match an ISO date (at least). | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub encode { | ||||||||||||||||||||||||||||
| 64 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 15 | my $value_ref = $self->value_ref; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 15 | ▶ | return if !defined $$value_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 12 | ▶ | if ( ! UNIVERSAL::isa( $$value_ref, 'DateTime' )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | if ($$value_ref !~ /^\d{4}[ -]?\d{2}[ -]?\d{2}/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $$value_ref = undef; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 3 | return undef; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 9 | ▶ | return unless $$value_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 9 | ▶ | if (my $tz = $self->_time_zone) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 9 | $$value_ref = $$value_ref->clone; | |||||||||||||||||||||||||||
| 80 | 9 | $$value_ref->set_time_zone($tz); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 9 | $$value_ref = $$value_ref->DateTime::strftime($self->_strptime); | |||||||||||||||||||||||||||
| 83 | 9 | return 1; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 decode | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | If value is defined then converts it into DateTime object otherwise do | ||||||||||||||||||||||||||||
| 89 | nothing. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub decode { | ||||||||||||||||||||||||||||
| 94 | 16 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 16 | my $value_ref = $self->value_ref; | |||||||||||||||||||||||||||
| 97 | 16 | ▶ | return unless defined $$value_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # XXX: Looks like we should use special modules for parsing DT because | ||||||||||||||||||||||||||||
| 100 | # different MySQL versions can return DT in different formats(none strict ISO) | ||||||||||||||||||||||||||||
| 101 | # Pg has also special format that depends on "european" and | ||||||||||||||||||||||||||||
| 102 | # server time_zone, by default ISO | ||||||||||||||||||||||||||||
| 103 | # other DBs may have own formats(Interbase for example can be forced to use special format) | ||||||||||||||||||||||||||||
| 104 | # but we need Jifty::DBI::Handle here to get DB type | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 12 | my $str = join('T', split ' ', $$value_ref, 2); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # The ISO8601 parser accepts 2012-11-04T12:34:56+00 | ||||||||||||||||||||||||||||
| 109 | # and 2012-11-04T12:34:56.789+00:00 | ||||||||||||||||||||||||||||
| 110 | # but not 2012-11-04T12:34:56.789+00 | ||||||||||||||||||||||||||||
| 111 | # Postgres returns sub-second times as the last one; append ":00" to | ||||||||||||||||||||||||||||
| 112 | # change it into the acceptable second option. | ||||||||||||||||||||||||||||
| 113 | 12 | ▶ | $str .= ":00" if $str =~ /\d\.\d+[+-]\d\d$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 12 | my $dt; | |||||||||||||||||||||||||||
| 116 | 12 | eval { $dt = $self->_parser->parse_datetime($str) }; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 12 | ▶ | if ($@) { # if datetime can't decode this, scream loudly with a useful error message | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | Carp::cluck("Unable to decode $str: $@"); | |||||||||||||||||||||||||||
| 120 | 0 | return; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 12 | ▶ | return if !$dt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 12 | my $tz = $self->_time_zone; | |||||||||||||||||||||||||||
| 126 | 12 | ▶ | $dt->set_time_zone($tz) if $tz; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 12 | ▶ | if ($self->date_only) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 3 | $dt->set_hour(0); | |||||||||||||||||||||||||||
| 130 | 3 | $dt->set_minute(0); | |||||||||||||||||||||||||||
| 131 | 3 | $dt->set_second(0); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 12 | $dt->set_formatter($self->formatter); | |||||||||||||||||||||||||||
| 135 | 12 | $$value_ref = $dt; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | L<Jifty::DBI::Filter>, L<DateTime> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 3 | 1; | |||||||||||||||||||||||||||