Units/Convert/Date.pm

filter: Statementstmt 84.8% 56 / 66 Branchbran 59.3% 19 / 32 Conditioncond 88.8% 8 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 69.2% 9 / 13 Podpod 0.0% 0 / 7 totaltotal 71.9% 95 / 132 CC 22 SCAR 25.6
CC
22
Cov
78%
render
20.0
construct
18.9
unit_map
2.0
CRAP 12.9
« Units/Grammar.pm ↑ Summary Units/Rank.pm »
1 package Math::Calc::Units::Convert::Date;
2 1 ▶use base 'Math::Calc::Units::Convert::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Time::Local qw(timegm);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw(%units %pref %ranges %total_unit_map);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 my $min_nice_time = timegm(0, 0, 0, 1, 0, 1975-1900);
8 1 my $max_nice_time = timegm(0, 0, 0, 1, 0, 2030-1900);
9  
10 1 %units = ();
11 1 %pref = ( default => 1 );
12 1 %ranges = ( timestamp => [ $min_nice_time, $max_nice_time ] );
13  
14 0 ▶sub major_pref {
Subroutine major_pref: not called CC=1 SCAR=6.9
Pod undocumented
15 0 return 2;
16 }
17  
18 # sub major_variants {}
19  
20 # sub variants {}
21  
22 139 ▶sub canonical_unit { return 'timestamp'; }
Subroutine canonical_unit: called CC=1 SCAR=0.0
Pod undocumented
23  
24 32 ▶sub unit_map {
Subroutine unit_map: called CC=2 SCAR=7.0
Pod undocumented
25 32 my ($self) = @_;
26 32 ▶ if (keys %total_unit_map == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %Math::Calc::Units::Convert::Date::total_unit_map == 0) 32 0 32
27 32 %total_unit_map = (%{$self->SUPER::unit_map()}, %units);
28 }
29 32 return \%total_unit_map;
30 }
31  
32 0 ▶sub get_ranges {
Subroutine get_ranges: not called CC=1 SCAR=6.9
Pod undocumented
33 0 return \%ranges;
34 }
35  
36 0 ▶sub get_prefs {
Subroutine get_prefs: not called CC=1 SCAR=6.9
Pod undocumented
37 0 return \%pref;
38 }
39  
40 1 ▶use vars qw(@MonthNames);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶BEGIN { @MonthNames = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); }
Subroutine BEGIN: called CC=1 SCAR=0.0
42 7 ▶sub construct {
Subroutine construct: called CC=18 SCAR=29.4
Pod undocumented
43 7 my ($self, $constructor, $args) = @_;
44  
45 # Allow timestamp(1000347142) or timestamp() for the current time
46 7 ▶ if ($constructor eq 'timestamp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($constructor eq "timestamp") 2 5 7
47 2 ▶ $args = time if $args eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $args eq "" 0 2 2
48 2 return [ $args, { 'timestamp' => 1 } ];
49 }
50  
51 5 ▶ return unless $constructor eq 'date';
Branch: 1 branch
BranchTrueFalseTotal
unless $constructor eq "date" 0 5 5
52  
53 # Accept a very limited range of formats.
54  
55 # Always assume GMT if not given. Currently, do not handle timezones.
56 5 $args =~ s/\s+GMT\s+$//;
57  
58 5 my ($Mon, $d, $y, $h, $m, $s, $tz, $M);
59 5 $tz = 'GMT';
60  
61 # Format 1: [Weekday] Mon DD HH:MM:SS [Timezone] YYYY
62 # (as returned by gmtime and the 'date' command)
63 # The weekday is ignored if given. The timezone is currently ignored.
64 5 ▶ if ($args =~ /^((?:\w\w\w\s+)?)
Branch: 4 branches
BranchTrueFalseTotal
if ($args =~ /^((?:\w\w\w\s+)?) (\w\w\w)\s* (\d+)\s+ (\d+):(\d+)[:.](\d+)\s+ (\w+)?\s* (\d\d\d\d)$/x) { } 1 4 5
elsif ($args =~ /^(\w\w\w)[\s-]* (\d+)[,\s-]+ (\d\d\d\d)$/x) { } 2 2 4
elsif ($args =~ /^(\d\d\d\d)-(\d+)-(\d+)\s+ (\d+):(\d+)[:.](\d+)$/x) { } 1 1 2
elsif ($args =~ /^(\d\d\d\d)-(\d+)-(\d+)$/) { } 1 0 1
65 (\w\w\w)\s*
66 (\d+)\s+
67 (\d+):(\d+)[:.](\d+)\s+
68 (\w+)?\s*
69 (\d\d\d\d)$/x)
70 {
71 1 (undef, $Mon, $d, $h, $m, $s, $tz, $y) = ($1, $2, $3, $4, $5, $6, $7, $8);
72  
73 # Format 2: Mon DD YYYY
74 } elsif ($args =~ /^(\w\w\w)[\s-]*
75 (\d+)[,\s-]+
76 (\d\d\d\d)$/x)
77 {
78 2 ($Mon, $d, $y) = ($1, $2, $3);
79  
80 # Format 3: YYYY-MM-DD HH:MM:SS
81 } elsif ($args =~ /^(\d\d\d\d)-(\d+)-(\d+)\s+
82 (\d+):(\d+)[:.](\d+)$/x)
83 {
84 1 ($y, $M, $d, $h, $m, $s) = ($1, $2, $3, $4, $5, $6);
85 1 $M--;
86  
87 # Format 4: YYYY-MM-DD
88 } elsif ($args =~ /^(\d\d\d\d)-(\d+)-(\d+)$/) {
89 1 ($y, $M, $d) = ($1, $2, $3);
90 1 $M--;
91 } else {
92 0 die "Unparseable date string '$args'";
93 }
94  
95 5 ▶ $h ||= 0;
Condition100%2/2
$h ||= 0
l!l
23
MC/DC100%1/1
$h ||= 0
$h
Truth table100%2/2
A
A= $h
Aresult
11
00
96 5 ▶ $m ||= 0;
Condition100%2/2
$m ||= 0
l!l
23
MC/DC100%1/1
$m ||= 0
$m
Truth table100%2/2
A
A= $m
Aresult
11
00
97 5 ▶ $s ||= 0;
Condition100%2/2
$s ||= 0
l!l
23
MC/DC100%1/1
$s ||= 0
$s
Truth table100%2/2
A
A= $s
Aresult
11
00
98  
99 5 ▶ if (defined $Mon) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Mon) 3 2 5
100 3 $M = 0;
101 3 foreach (@MonthNames) {
102 30 ▶ last if lc($_) eq lc($Mon);
Branch: 1 branch
BranchTrueFalseTotal
if lc $_ eq lc $Mon 3 27 30
103 27 $M++;
104 }
105 3 ▶ die "Unparseable month '$Mon'" if $M > 11;
Branch: 1 branch
BranchTrueFalseTotal
if $M > 11 0 3 3
106 }
107  
108 5 ▶ if (defined($tz) && $tz ne 'GMT') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tz and $tz ne "GMT") 0 5 5
Condition66%2/3
defined $tz and $tz ne "GMT"
!ll&&!rl&&r
140
MC/DC0%0/2
defined $tz and $tz ne "GMT"
defined $tz $tz ne "GMT"
Truth table66%2/3
A and B
A= defined $tzB= $tz ne "GMT"
ABresult
0X0
100
111
109 0 warn "Timezones not supported. Assuming GMT.\n";
110 }
111  
112 5 my $timestamp = timegm($s, $m, $h, $d, $M, $y-1900);
113 5 ▶ die "Date '$args' is out of range" if $timestamp == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $timestamp == -1 0 5 5
114 5 return [ $timestamp, { 'timestamp' => 1 } ];
115 }
116  
117 0 ▶sub render {
Subroutine render: not called CC=4 SCAR=30.0
Pod undocumented
118 0 my ($self, $mag, $name, $power) = @_;
119 0 ▶ return "\@$mag" if $power != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $power != 1 0 0 0
120 0 ▶ return "\@$mag" if $mag < $min_nice_time;
Branch: 1 branch
BranchTrueFalseTotal
if $mag < $min_nice_time 0 0 0
121 0 ▶ return "\@$mag" if $mag > $max_nice_time;
Branch: 1 branch
BranchTrueFalseTotal
if $mag > $max_nice_time 0 0 0
122 0 return gmtime($mag) . " (\@$mag)";
123 }
124  
125 1 1;
« Units/Grammar.pm ↑ Summary Units/Rank.pm »