| 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<DateTime::Incomplete>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | package WebService::IMDB::Date; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | our @CARP_NOT = qw(WebService::IMDB WebService::IMDB::Trivium); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use DateTime::Format::Strptime; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use DateTime::Incomplete; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub _new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_new: not called CC=23 SCAR=63.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $ws = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | my $data = shift or die; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | if (ref $data eq "HASH" && exists $data->{'normal'} && defined $data->{'normal'}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/6
ref $data eq "HASH" and exists $data->{"normal"} and defined $data->{"normal"}
ref $data eq "HASH" and exists $data->{"normal"}
MC/DC0%0/3
ref $data eq "HASH" and exists $data->{"normal"} and defined $data->{"normal"}
ref $data eq "HASH"
exists $data->{"normal"}
defined $data->{"normal"}
Truth table0%0/4
A and B and C
A= ref $data eq "HASH"B= exists $data->{"normal"}C= defined $data->{"normal"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # TODO: Check whehter locale affects this. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $dt = $data->{'normal'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | my ($y, $m, $d) = $dt =~ m/^(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?$/ or die "Failed to parse '$dt'"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if (defined $y && defined $m && defined $d) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/6
defined $y and defined $m and defined $d
defined $y and defined $m
MC/DC0%0/3
defined $y and defined $m and defined $d
defined $y
defined $m
defined $d
Truth table0%0/4
A and B and C
A= defined $yB= defined $mC= defined $d
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | return DateTime::Incomplete->new('year' => $y, 'month' => $m, 'day' => $d); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | ▶ | } elsif (defined $y && defined $m) { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
defined $y and defined $m
MC/DC0%0/2
defined $y and defined $m
defined $y
defined $m
Truth table0%0/3
A and B
A= defined $yB= defined $m
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | return DateTime::Incomplete->new('year' => $y, 'month' => $m); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } elsif (defined $y) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | return DateTime::Incomplete->new('year' => $y); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | die ""; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ▶ | } elsif (ref $data eq "HASH" && exists $data->{'text'}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $data eq "HASH" and exists $data->{"text"}
MC/DC0%0/2
ref $data eq "HASH" and exists $data->{"text"}
ref $data eq "HASH"
exists $data->{"text"}
Truth table0%0/3
A and B
A= ref $data eq "HASH"B= exists $data->{"text"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if (!defined $data->{'text'}) { # A bit hacky, but not sure how else to deal with this. | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 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 | 0 | my $d = DateTime::Format::Strptime->new('pattern' => "%B %d, %Y", 'on_error' => "undef")->parse_datetime($data->{'text'}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if (defined $d) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 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 | ▶ | my ($m, $y) = $data->{'text'} =~ m/^(\d{1,2})\/(\d{2})$/ or die "Failed to parse '$data->{'text'}'"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 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 | ▶ | my ($m, $d, $y) = $data->{'text'} =~ m/^(\d{1,2})\/(\d{1,2})\/(\d{2})$/ or die "Failed to parse '$data->{'text'}'"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | return DateTime::Incomplete->new('year' => $y, 'month' => $m, 'day' => $d); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return $data->{'text'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | ▶ | } elsif (ref $data eq "" && $data =~ m/^\d{4}-\d{2}-\d{2}$/) { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $data eq "" and $data =~ /^\d{4}-\d{2}-\d{2}$/
MC/DC0%0/2
ref $data eq "" and $data =~ /^\d{4}-\d{2}-\d{2}$/
ref $data eq ""
$data =~ /^\d{4}-\d{2}-\d{2}$/
Truth table0%0/3
A and B
A= ref $data eq ""B= $data =~ /^\d{4}-\d{2}-\d{2}$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my ($y, $m, $d) = $data =~ m/^(\d{4})-(\d{2})-(\d{2})$/ or die "Failed to parse '$data'"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | return DateTime::Incomplete->new('year' => $y, 'month' => $m, 'day' => $d); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | croak "Unable to parse date"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||