| 1 | package Wikibase::Datatype::Print::Value::Time; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 47 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 47 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 47 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 47 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 47 | ▶ | use English; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 47 | ▶ | use Error::Pure qw(err); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 47 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 47 | Readonly::Array our @EXPORT_OK => qw(print); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 47 | our $VERSION = 0.21; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 16 | ▶ | sub print { | ||||||||||||||||||||||||||
|
Subroutine
print: called CC=19 SCAR=31.6
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 16 | my ($obj, $opts_hr) = @_; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Default options. | ||||||||||||||||||||||||||||
| 20 | 16 | ▶ | if (! defined $opts_hr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 3 | $opts_hr = {}; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 16 | ▶ | if (! exists $opts_hr->{'print_name'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 15 | $opts_hr->{'print_name'} = 1; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 16 | ▶ | if (! $obj->isa('Wikibase::Datatype::Value::Time')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 1 | err "Object isn't 'Wikibase::Datatype::Value::Time'."; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 15 | ▶ | if (exists $opts_hr->{'cb'} && ! $opts_hr->{'cb'}->isa('Wikibase::Cache')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $opts_hr->{"cb"} and not $opts_hr->{"cb"}->isa("Wikibase::Cache")
MC/DC100%2/2
exists $opts_hr->{"cb"} and not $opts_hr->{"cb"}->isa("Wikibase::Cache")
exists $opts_hr->{"cb"}
not $opts_hr->{"cb"}->isa("Wikibase::Cache")
Truth table100%3/3
A and not(B)
A= exists $opts_hr->{"cb"}B= not $opts_hr->{"cb"}->isa("Wikibase::Cache")
| |||||||||||||||||||||||||||||
| 32 | 1 | err "Option 'cb' must be a instance of Wikibase::Cache."; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Calendar. | ||||||||||||||||||||||||||||
| 36 | 14 | my $calendar; | |||||||||||||||||||||||||||
| 37 | 14 | ▶ | if ($opts_hr->{'print_name'} && exists $opts_hr->{'cb'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts_hr->{"print_name"} and exists $opts_hr->{"cb"}
MC/DC50%1/2
$opts_hr->{"print_name"} and exists $opts_hr->{"cb"}
$opts_hr->{"print_name"}
exists $opts_hr->{"cb"}
Truth table66%2/3
A and B
A= $opts_hr->{"print_name"}B= exists $opts_hr->{"cb"}
| |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | $calendar = $opts_hr->{'cb'}->get('label', $obj->calendarmodel) || $obj->calendarmodel; | ||||||||||||||||||||||||||
|
Condition33%1/3
$opts_hr->{'cb'}->get('label', $obj->calendarmodel) || $obj->calendarmodel
MC/DC0%0/2
$opts_hr->{'cb'}->get('label', $obj->calendarmodel) || $obj->calendarmodel
$opts_hr->{'cb'}->get('label', $obj->calendarmodel)
$obj->calendarmodel
Truth table33%1/3
A || B
A= $opts_hr->{'cb'}->get('label', $obj->calendarmodel)B= $obj->calendarmodel
| |||||||||||||||||||||||||||||
| 39 | } else { | ||||||||||||||||||||||||||||
| 40 | 13 | $calendar = $obj->calendarmodel; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Convert to DateTime. | ||||||||||||||||||||||||||||
| 44 | 14 | my $dt = _parse_date($obj->value); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 14 | my $printed_date; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Day. | ||||||||||||||||||||||||||||
| 49 | 14 | ▶ | if ($obj->precision == 11) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 50 | 10 | $printed_date = $dt->strftime("%e %B %Y"); | |||||||||||||||||||||||||||
| 51 | 10 | $printed_date =~ s/^\s+//ms; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # Month. | ||||||||||||||||||||||||||||
| 54 | } elsif ($obj->precision == 10) { | ||||||||||||||||||||||||||||
| 55 | 0 | $printed_date = $dt->strftime("%B %Y"); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Year. | ||||||||||||||||||||||||||||
| 58 | } elsif ($obj->precision == 9) { | ||||||||||||||||||||||||||||
| 59 | 2 | $printed_date = $dt->strftime("%Y"); | |||||||||||||||||||||||||||
| 60 | 2 | ▶ | if ($obj->before || $obj->after) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$obj->before or $obj->after
MC/DC50%1/2
$obj->before or $obj->after
$obj->before
$obj->after
Truth table66%2/3
A or B
A= $obj->beforeB= $obj->after
| |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | my $before = $obj->before ? $dt->year - $obj->before : $dt->year; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | my $after = $obj->after ? $dt->year + $obj->after : $dt->year; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | $printed_date .= " ($before-$after)"; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Decade. | ||||||||||||||||||||||||||||
| 67 | } elsif ($obj->precision == 8) { | ||||||||||||||||||||||||||||
| 68 | 2 | $printed_date = (int($dt->strftime('%Y') / 10) * 10).'s'; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # TODO Better precision print? | ||||||||||||||||||||||||||||
| 71 | # 0 - billion years, 1 - hundred million years, ..., 6 - millenia, 7 - century | ||||||||||||||||||||||||||||
| 72 | ▶ | } elsif ($obj->precision <= 7 && $obj->precision >= 0) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$obj->precision <= 7 and $obj->precision >= 0
MC/DC0%0/2
$obj->precision <= 7 and $obj->precision >= 0
$obj->precision <= 7
$obj->precision >= 0
Truth table0%0/3
A and B
A= $obj->precision <= 7B= $obj->precision >= 0
| |||||||||||||||||||||||||||||
| 73 | 0 | $printed_date = $dt->strftime("%Y"); | |||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||
| 75 | 0 | err "Unsupported precision '".$obj->precision."'."; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 14 | return $printed_date.' ('.$calendar.')'; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 14 | ▶ | sub _parse_date { | ||||||||||||||||||||||||||
|
Subroutine
_parse_date: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 82 | 14 | my $date = shift; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 14 | my ($year, $month, $day) = ($date =~ m/^([\+\-]\d+)\-(\d{2})\-(\d{2})T\d{2}:\d{2}:\d{2}Z$/ms); | |||||||||||||||||||||||||||
| 85 | 14 | ▶ | my $dt = DateTime->new( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 86 | 'year' => int($year), | ||||||||||||||||||||||||||||
| 87 | $month != 0 ? ('month' => $month) : (), | ||||||||||||||||||||||||||||
| 88 | $day != 0 ? ('day' => $day) : (), | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 14 | return $dt; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 47 | 1; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||