| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # $Id: Date.pm 7370 2012-04-09 01:17:33Z chris $ | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | =head1 NAME | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | WebService::IMDB::Date | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | Constructor always returns a L. | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =cut | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | package WebService::IMDB::Date; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 2 |  |  | 2 |  | 12 | use strict; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 71 |  | 
| 16 | 2 |  |  | 2 |  | 10 | use warnings; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 86 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | our $VERSION = '0.05'; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 2 |  |  | 2 |  | 11 | use Carp; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 157 |  | 
| 21 |  |  |  |  |  |  | our @CARP_NOT = qw(WebService::IMDB WebService::IMDB::Trivium); | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 2 |  |  | 2 |  | 2420 | use DateTime::Format::Strptime; | 
|  | 2 |  |  |  |  | 418071 |  | 
|  | 2 |  |  |  |  | 170 |  | 
| 24 | 2 |  |  | 2 |  | 2661 | use DateTime::Incomplete; | 
|  | 2 |  |  |  |  | 128825 |  | 
|  | 2 |  |  |  |  | 1497 |  | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub _new { | 
| 28 | 97 |  |  | 97 |  | 171 | my $class = shift; | 
| 29 | 97 |  |  |  |  | 112 | my $ws = shift; | 
| 30 | 97 | 50 |  |  |  | 207 | my $data = shift or die; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 97 | 100 | 100 |  |  | 843 | if (ref $data eq "HASH" && exists $data->{'normal'} && defined $data->{'normal'}) { | 
|  |  | 100 | 66 |  |  |  |  | 
|  |  | 50 | 66 |  |  |  |  | 
|  |  |  | 33 |  |  |  |  | 
| 33 |  |  |  |  |  |  | # TODO: Check whehter locale affects this. | 
| 34 | 75 |  |  |  |  | 131 | my $dt = $data->{'normal'}; | 
| 35 | 75 | 50 |  |  |  | 550 | my ($y, $m, $d) = $dt =~ m/^(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?$/ or die "Failed to parse '$dt'"; | 
| 36 | 75 | 100 | 66 |  |  | 556 | if (defined $y && defined $m && defined $d) { | 
|  |  | 100 | 100 |  |  |  |  | 
|  |  | 50 | 66 |  |  |  |  | 
| 37 | 68 |  |  |  |  | 283 | return DateTime::Incomplete->new('year' => $y, 'month' => $m, 'day' => $d); | 
| 38 |  |  |  |  |  |  | } elsif (defined $y && defined $m) { | 
| 39 | 2 |  |  |  |  | 21 | return DateTime::Incomplete->new('year' => $y, 'month' => $m); | 
| 40 |  |  |  |  |  |  | } elsif (defined $y) { | 
| 41 | 5 |  |  |  |  | 19 | return DateTime::Incomplete->new('year' => $y); | 
| 42 |  |  |  |  |  |  | } else { | 
| 43 | 0 |  |  |  |  | 0 | die ""; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | } elsif (ref $data eq "HASH" && exists $data->{'text'}) { | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 1 | 50 |  |  |  | 7 | if (!defined $data->{'text'}) { # A bit hacky, but not sure how else to deal with this. | 
| 49 | 0 |  |  |  |  | 0 | return undef; | 
| 50 |  |  |  |  |  |  | } else { | 
| 51 |  |  |  |  |  |  | # Some 'text' dates are actually parseable, e.g. those of the format "July 14, 2007".  Attempt to detect them | 
| 52 |  |  |  |  |  |  | # and parse to a DateTime::Incomplete | 
| 53 | 1 |  |  |  |  | 14 | my $d = DateTime::Format::Strptime->new('pattern' => "%B %d, %Y", 'on_error' => "undef")->parse_datetime($data->{'text'}); | 
| 54 | 1 | 50 |  |  |  | 787 | if (defined $d) { | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 55 | 0 |  |  |  |  | 0 | return DateTime::Incomplete->new('year' => $d->year(), 'month' => $d->month(), 'day' => $d->day()); | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | } elsif ($data->{'text'} =~ m/^\d{1,2}\/\d{2}$/) { | 
| 58 | 0 | 0 |  |  |  | 0 | my ($m, $y) = $data->{'text'} =~ m/^(\d{1,2})\/(\d{2})$/ or die "Failed to parse '$data->{'text'}'"; | 
| 59 | 0 |  |  |  |  | 0 | return DateTime::Incomplete->new('year' => $y, 'month' => $m); | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | } elsif ($data->{'text'} =~ m/^\d{1,2}\/\d{1,2}\/\d{2}$/) { | 
| 62 | 0 | 0 |  |  |  | 0 | my ($m, $d, $y) = $data->{'text'} =~ m/^(\d{1,2})\/(\d{1,2})\/(\d{2})$/ or die "Failed to parse '$data->{'text'}'"; | 
| 63 | 0 |  |  |  |  | 0 | return DateTime::Incomplete->new('year' => $y, 'month' => $m, 'day' => $d); | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | } else { | 
| 66 | 1 |  |  |  |  | 8 | return $data->{'text'}; | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | } elsif (ref $data eq "" && $data =~ m/^\d{4}-\d{2}-\d{2}$/) { | 
| 71 | 21 | 50 |  |  |  | 124 | my ($y, $m, $d) = $data =~ m/^(\d{4})-(\d{2})-(\d{2})$/ or die "Failed to parse '$data'"; | 
| 72 | 21 |  |  |  |  | 92 | return DateTime::Incomplete->new('year' => $y, 'month' => $m, 'day' => $d); | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | } else { | 
| 75 | 0 |  |  |  |  |  | croak "Unable to parse date"; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | 1; |