Epoch.pm

filter: Statementstmt 95.2% 100 / 105 Branchbran 82.0% 41 / 50 Conditioncond 73.3% 11 / 15 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 88.0% 170 / 193 CC 31 SCAR 21.4
CC
31
Cov
89%
format_datetime
13.1
parse_datetime
11.9
new
7.0
CRAP 8.5
↑ Summary Epoch/TAI64.pm »
1 package DateTime::Format::Epoch;
2  
3 19 ▶use 5.00503; #qr
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 19 $VERSION = '0.16';
9  
10 19 ▶use DateTime 0.22;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 19 ▶use DateTime::LeapSecond;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 19 ▶use Math::BigInt 'lib' => 'GMP,Pari,FastCalc';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 19 ▶use Params::Validate qw/validate BOOLEAN OBJECT/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 ▶sub _floor {
Subroutine _floor: called CC=2 SCAR=7.4
17 11 my $x = shift;
18 11 my $ix = int $x;
19 11 ▶ if ($ix <= $x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix <= $x) { } 11 0 11
20 11 return $ix;
21 } else {
22 0 return $ix - 1;
23 }
24 }
25  
26 19 my %units_per_second = (
27 seconds => 1,
28 milliseconds => 1000,
29 microseconds => 1e6,
30 nanoseconds => 1e9,
31 );
32  
33 57 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
34 57 my $class = shift;
35 my %p = validate( @_,
36 { epoch => {type => OBJECT,
37 can => 'utc_rd_values'},
38 unit => {callbacks =>
39 {'valid unit' =>
40 48 ▶ sub { exists $units_per_second{$_[0]}
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $units_per_second{$_[0]} or $_[0] > 0 24 24 48
41 or $_[0] > 0 }},
42 57 default => 'seconds'},
43 type => {regex => qr/^(?:int|float|bigint)$/,
44 default => 0},
45 local_epoch => {type => BOOLEAN,
46 default => 0},
47 dhms => {type => BOOLEAN,
48 default => 0},
49 skip_leap_seconds => {type => BOOLEAN,
50 default => 1},
51 start_at => {default => 0},
52 } );
53  
54 57 ▶ $p{epoch} = $p{epoch}->clone if $p{epoch}->can('clone');
Branch: 1 branch
BranchTrueFalseTotal
if $p{"epoch"}->can("clone") 57 0 57
55  
56 57 ▶ $p{unit} = $units_per_second{$p{unit}} || $p{unit};
Condition66%2/3
$units_per_second{$p{'unit'}} || $p{'unit'}
l!l&&r!l&&!r
33240
MC/DC0%0/2
$units_per_second{$p{'unit'}} || $p{'unit'}
$units_per_second{$p{'unit'}} $p{'unit'}
Truth table66%2/3
A || B
A= $units_per_second{$p{'unit'}}B= $p{'unit'}
ABresult
1X1
011
000
57 57 ▶ $p{unit} = 1 if $p{dhms};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"dhms"} 1 56 57
58  
59 57 ▶ if (!$p{type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p{"type"}) 11 46 57
60 11 ▶ $p{type} = ($p{unit} > 1e6 ? 'bigint' : 'int');
Branch: 1 branch
BranchTrueFalseTotal
$p{'unit'} > 1000000.0 ? : 1 10 11
61 }
62  
63 57 ($p{epoch_rd_days}, $p{epoch_rd_secs}) = $p{epoch}->utc_rd_values;
64 57 $p{epoch_class} = ref $p{epoch};
65  
66 57 ▶ if (!$p{skip_leap_seconds}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p{"skip_leap_seconds"}) 5 52 57
67 $p{leap_secs} =
68 5 DateTime::LeapSecond::leap_seconds( $p{epoch_rd_days} );
69 }
70  
71 57 my $self = bless \%p, $class;
72 57 return $self;
73 }
74  
75 96 ▶sub format_datetime {
Subroutine format_datetime: called CC=13 SCAR=25.7
Pod documented
76 96 my ($self, $dt) = @_;
77  
78 96 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 25 71 96
79 25 $self = $self->new;
80 }
81  
82 $dt = $dt->clone->set_time_zone('floating')
83 if $self->{local_epoch} &&
84 $self->{epoch}->can('time_zone') &&
85 $self->{epoch}->time_zone->is_floating &&
86 96 ▶ $dt->can('time_zone') &&
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"local_epoch"} and $self->{"epoch"}->can("time_zone") and $self->{"epoch"}->time_zone->is_floating and $dt->can("time_zone") and not $dt->time_zone->is_floating 5 91 96
Condition75%9/12
$self->{"local_epoch"} and $self->{"epoch"}->can("time_zone") and $self->{"epoch"}->time_zone->is_floating and $dt->can("time_zone") and not $dt->time_zone->is_floating
!ll&&!rl&&r
64275
$self->{"local_epoch"} and $self->{"epoch"}->can("time_zone") and $self->{"epoch"}->time_zone->is_floating and $dt->can("time_zone")
!ll&&!rl&&r
64032
$self->{"local_epoch"} and $self->{"epoch"}->can("time_zone") and $self->{"epoch"}->time_zone->is_floating
!ll&&!rl&&r
64032
$self->{"local_epoch"} and $self->{"epoch"}->can("time_zone")
!ll&&!rl&&r
64032
MC/DC40%2/5
$self->{"local_epoch"} and $self->{"epoch"}->can("time_zone") and $self->{"epoch"}->time_zone->is_floating and $dt->can("time_zone") and not $dt->time_zone->is_floating
$self->{"local_epoch"} $self->{"epoch"}->can("time_zone") $self->{"epoch"}->time_zone->is_floating $dt->can("time_zone") not $dt->time_zone->is_floating
Truth table50%3/6
A and B and C and D and not(E)
A= $self->{"local_epoch"}B= $self->{"epoch"}->can("time_zone")C= $self->{"epoch"}->time_zone->is_floatingD= $dt->can("time_zone")E= not $dt->time_zone->is_floating
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
87 !$dt->time_zone->is_floating;
88  
89 96 my ($rd_days, $rd_secs) = $dt->utc_rd_values;
90 96 my $delta_days = $rd_days - $self->{epoch_rd_days};
91 96 my $delta_secs = $rd_secs - $self->{epoch_rd_secs};
92  
93 96 my $secs = $delta_days * 86_400 + $delta_secs;
94  
95 96 ▶ if (!$self->{skip_leap_seconds}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"skip_leap_seconds"}) 16 80 96
96 $secs += DateTime::LeapSecond::leap_seconds( $rd_days )
97 16 - $self->{leap_secs};
98 }
99  
100 96 ▶ if ($self->{type} eq 'bigint') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "bigint") 21 75 96
101 21 ▶ if ($secs > 2_147_483_647) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secs > 2147483647) 2 19 21
102 2 $secs = "$secs"; #https://rt.cpan.org/Ticket/Display.html?id=103517
103 }
104 21 $secs = Math::BigInt->new($secs);
105 }
106  
107 96 $secs *= $self->{unit};
108  
109 96 ▶ if ($dt->can('nanosecond')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt->can("nanosecond")) 96 0 96
110 96 my $fraction = $dt->nanosecond / 1e9 * $self->{unit};
111 96 ▶ if ($self->{type} eq 'float') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type'} eq 'float') { } 11 85 96
112 11 $secs += $fraction;
113 } else {
114 85 $secs += int $fraction;
115 }
116 }
117  
118 96 $secs += $self->{start_at};
119  
120 96 ▶ if ($self->{dhms}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dhms"}) 5 91 96
121 5 my $mins = int($secs / 60);
122 5 $secs -= $mins * 60;
123 5 my $hours = int($mins / 60);
124 5 $mins -= $hours * 60;
125 5 my $days = int($hours / 24);
126 5 $hours -= $days * 24;
127  
128 5 return $days, $hours, $mins, $secs;
129 }
130  
131 91 return $secs;
132 }
133  
134 11 ▶sub parse_datetime {
Subroutine parse_datetime: called CC=11 SCAR=24.8
Pod documented
135 11 my ($self, $str) = @_;
136  
137 11 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 7 4 11
138 7 $self = $self->new;
139 }
140  
141 11 ▶ if ($self->{dhms}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dhms"}) 0 11 11
142 0 my (undef, $d, $h, $m, $s) = @_;
143 0 $str = (($d * 24 + $h) * 60 + $m) + $s;
144 }
145  
146 11 $str -= $self->{start_at};
147  
148 11 my $delta_days = _floor( $str / (86_400 * $self->{unit}) );
149 11 $str -= $delta_days * 86_400 * $self->{unit};
150  
151 # $str cannot be negative now, so int() instead of _floor()
152 11 my $delta_secs = int( $str / $self->{unit} );
153 11 $str -= $delta_secs * $self->{unit};
154  
155 11 my $delta_nano = $str / $self->{unit} * 1e9;
156  
157 11 my $rd_days = $self->{epoch_rd_days} + $delta_days;
158 11 my $rd_secs = $self->{epoch_rd_secs} + $delta_secs;
159  
160 11 ▶ if (!$self->{skip_leap_seconds}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'skip_leap_seconds'}) { } 1 10 11
161 $rd_secs -= DateTime::LeapSecond::leap_seconds( $rd_days )
162 1 - $self->{leap_secs};
163 1 ▶ if ($rd_secs >= DateTime::LeapSecond::day_length( $rd_days )) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rd_secs >= DateTime::LeapSecond::day_length($rd_days)) { } 1 0 1
elsif ($rd_secs < 0) { } 0 0 0
164 1 $rd_secs -= DateTime::LeapSecond::day_length( $rd_days );
165 1 $rd_days++;
166 } elsif ($rd_secs < 0) {
167 0 $rd_days--;
168 0 $rd_secs += DateTime::LeapSecond::day_length( $rd_days );
169 }
170 } else {
171 10 ▶ if ($rd_secs >= 86400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rd_secs >= 86400) 2 8 10
172 2 $rd_secs -= 86400;
173 2 $rd_days++;
174 }
175 }
176  
177 11 ▶ $rd_days = $rd_days->numify if UNIVERSAL::isa($rd_days, 'Math::BigInt');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($rd_days, "Math::BigInt") 1 10 11
178 11 ▶ $rd_secs = $rd_secs->numify if UNIVERSAL::isa($rd_secs, 'Math::BigInt');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($rd_secs, "Math::BigInt") 1 10 11
179  
180 11 my $temp_dt = bless { rd_days => $rd_days, rd_secs => $rd_secs},
181 'DateTime::Format::Epoch::_DateTime';
182  
183 11 my $dt = $self->{epoch_class}->from_object( object => $temp_dt );
184  
185 11 ▶ if (!$self->{local_epoch}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"local_epoch"}) 11 0 11
186 11 ▶ $dt->set_time_zone( 'UTC' ) if $dt->can('set_time_zone');
Branch: 1 branch
BranchTrueFalseTotal
if $dt->can("set_time_zone") 11 0 11
187 }
188  
189 11 return $dt;
190 }
191  
192 11 ▶sub DateTime::Format::Epoch::_DateTime::utc_rd_values {
Subroutine utc_rd_values: called CC=1 SCAR=0.0
193 11 my $self = shift;
194  
195 11 return $self->{rd_days}, $self->{rd_secs};
196 }
197  
198 19 1;
199 __END__
↑ Summary Epoch/TAI64.pm »