blib/lib/DT.pm

filter: Statementstmt 91.7% 78 / 85 Branchbran 82.6% 43 / 52 Conditioncond 33.3% 8 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 90.3% 28 / 31 Podpod 80.0% 4 / 5 totaltotal 76.3% 161 / 211 CC 37 SCAR 15.8
CC
37
Cov
80%
pg_timestamp_notz
12.0
pg_timestamp_tz
12.0
_dt_str_cmp
12.0
CRAP 4.8
↑ Summary
1 package DT;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings 'FATAL' => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use DateTime::Format::ISO8601::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use parent 'DateTime::Moonpig';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 our $VERSION = '0.5.0';
16  
17 use overload
18 5 ▶ '""' => \&_dt_stringify,
Subroutine BEGIN: called CC=1 SCAR=0.0
19 '==' => \&_dt_int_eq,
20 '!=' => \&_dt_int_ne,
21 '<=>' => \&_dt_int_cmp,
22 '<' => \&_dt_int_lt,
23 '<=' => \&_dt_int_le,
24 '>' => \&_dt_int_gt,
25 '>=' => \&_dt_int_ge,
26 'eq' => \&_dt_str_eq,
27 'ne' => \&_dt_str_ne,
28 'cmp' => \&_dt_str_cmp,
29 'lt' => \&_dt_str_lt,
30 'le' => \&_dt_str_le,
31 'gt' => \&_dt_str_gt,
32 5 'ge' => \&_dt_str_ge;
33  
34 5 my $HAVE_PG;
35  
36 47 ▶sub new {
Subroutine new: called CC=7 SCAR=19.8
Pod documented
37 47 my $class = shift;
38  
39 47 my $dt;
40  
41 47 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 47 0 47
42 # Most probably Unix time, will croak if not
43 47 ▶ if ( looks_like_number $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (&looks_like_number($_[0])) { } 31 16 47
elsif (not ref $_[0]) { } 2 14 16
44 31 $dt = $class->SUPER::new(@_);
45 }
46 elsif ( not ref $_[0] ) {
47 # May be ISO8601(ish) format used by PostgreSQL
48 2 $dt = eval {
49 2 ▶ $HAVE_PG || ($HAVE_PG = require DateTime::Format::Pg);
Branch: 1 branch
BranchTrueFalseTotal
$HAVE_PG or $HAVE_PG = require DateTime::Format::Pg 0 2 2
50 0 DateTime::Format::Pg->parse_datetime($_[0]);
51 };
52
53  
54 # May be a real ISO8601 format date/time
55 2 ▶ $dt = eval { DateTime::Format::ISO8601->parse_datetime($_[0]) }
Branch: 1 branch
BranchTrueFalseTotal
unless $dt 2 0 2
56 if not $dt;
57 }
58 }
59  
60 # This will croak
61 47 ▶ $dt = DateTime->new(@_) unless $dt;
Branch: 1 branch
BranchTrueFalseTotal
unless $dt 15 32 47
62  
63 # Rebless into DT so our methods are called instead of DateTime
64 46 return bless $dt, $class;
65 }
66  
67 1 ▶sub unix_time { $_[0]->epoch }
Subroutine unix_time: called CC=1 SCAR=0.0
Pod documented
68  
69 0 ▶sub pg_timestamp_notz {
Subroutine pg_timestamp_notz: not called CC=3 SCAR=24.8
Pod documented
70 0 ▶ Carp::croak("DateTime::Format::Pg is not installed")
Branch: 1 branch
BranchTrueFalseTotal
unless $HAVE_PG or $HAVE_PG = require DateTime::Format::Pg 0 0 0
Condition0%0/3
$HAVE_PG or $HAVE_PG = require DateTime::Format::Pg
l!l&&r!l&&!r
000
MC/DC0%0/2
$HAVE_PG or $HAVE_PG = require DateTime::Format::Pg
$HAVE_PG $HAVE_PG = require DateTime::Format::Pg
Truth table0%0/3
A or B
A= $HAVE_PGB= $HAVE_PG = require DateTime::Format::Pg
ABresult
1X1
011
000
71 unless $HAVE_PG || ($HAVE_PG = require DateTime::Format::Pg);
72
73 0 return DateTime::Format::Pg->format_timestamp_without_time_zone($_[0]);
74 }
75  
76 0 ▶sub pg_timestamp_tz {
Subroutine pg_timestamp_tz: not called CC=3 SCAR=24.8
Pod documented
77 0 ▶ Carp::croak("DateTime::Format::Pg is not installed")
Branch: 1 branch
BranchTrueFalseTotal
unless $HAVE_PG or $HAVE_PG = require DateTime::Format::Pg 0 0 0
Condition0%0/3
$HAVE_PG or $HAVE_PG = require DateTime::Format::Pg
l!l&&r!l&&!r
000
MC/DC0%0/2
$HAVE_PG or $HAVE_PG = require DateTime::Format::Pg
$HAVE_PG $HAVE_PG = require DateTime::Format::Pg
Truth table0%0/3
A or B
A= $HAVE_PGB= $HAVE_PG = require DateTime::Format::Pg
ABresult
1X1
011
000
78 unless $HAVE_PG || ($HAVE_PG = require DateTime::Format::Pg);
79
80 0 return DateTime::Format::Pg->format_timestamptz($_[0]);
81 }
82  
83 {
84 5 for my $method (qw(
85 add_duration subtract_duration
86 truncate
87 set set_time_zone set_year set_month set_day
88 set_hour set_minute set_second set_nanosecond
89 )) {
90 Sub::Install::install_sub({
91 14 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 14 my $dt = shift;
93
94 14 my $copy = $dt->clone;
95 14 bless $copy, 'DateTime';
96
97 14 $copy->$method(@_);
98
99 14 bless $copy, ref $dt;
100
101 14 return $copy;
102 },
103 60 as => $method,
104 });
105 }
106 }
107  
108 0 ▶sub TO_JSON {
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my ($self) = @_;
110
111 0 return "$self";
112 }
113  
114 ############## PRIVATE METHODS BELOW ##############
115  
116 28 ▶sub _promote {
Subroutine _promote: called CC=3 SCAR=12.5
117 28 my $side_b = $_[1];
118
119 # Deliberately not catching errors here
120 28 ▶ $side_b = DT->new($side_b)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $side_b and $side_b->isa("DateTime") 28 0 28
Condition33%1/3
ref $side_b and $side_b->isa("DateTime")
!ll&&!rl&&r
2800
MC/DC0%0/2
ref $side_b and $side_b->isa("DateTime")
ref $side_b $side_b->isa("DateTime")
Truth table33%1/3
A and B
A= ref $side_bB= $side_b->isa("DateTime")
ABresult
0X0
100
111
121 if not ref($side_b) or not $side_b->isa('DateTime');
122
123 28 return ($_[0], $side_b, $_[2]);
124 }
125  
126 71 ▶sub _dt_stringify {
Subroutine _dt_stringify: called CC=1 SCAR=0.0
127 71 return DateTime::Format::ISO8601::Format->new->format_datetime($_[0]);
128 }
129  
130 6 ▶sub _dt_int_eq {
Subroutine _dt_int_eq: called CC=2 SCAR=6.9
131 6 ▶ return undef unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 2 4 6
132
133 4 my ($side_a, $side_b) = _promote(@_);
134
135 4 return DateTime::compare($side_a, $side_b) == 0;
136 }
137  
138 12 ▶sub _dt_int_cmp {
Subroutine _dt_int_cmp: called CC=3 SCAR=11.0
139 12 ▶ return undef unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 2 10 12
140  
141 10 my ($side_a, $side_b, $flip) = _promote(@_);
142
143 10 ▶ return $flip ? DateTime::compare($side_b, $side_a)
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 5 5 10
144 : DateTime::compare($side_a, $side_b)
145 ;
146 }
147  
148 4 ▶sub _dt_int_ne { !defined($_[1]) ? undef : !_dt_int_eq(@_) }
Subroutine _dt_int_ne: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!defined($_[1]) ? : 2 2 4
149 4 ▶sub _dt_int_lt { !defined($_[1]) ? undef : _dt_int_cmp(@_) < 0 }
Subroutine _dt_int_lt: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!defined($_[1]) ? : 2 2 4
150 4 ▶sub _dt_int_le { !defined($_[1]) ? undef : _dt_int_cmp(@_) <= 0 }
Subroutine _dt_int_le: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!defined($_[1]) ? : 2 2 4
151 4 ▶sub _dt_int_gt { !defined($_[1]) ? undef : _dt_int_cmp(@_) > 0 }
Subroutine _dt_int_gt: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!defined($_[1]) ? : 2 2 4
152 4 ▶sub _dt_int_ge { !defined($_[1]) ? undef : _dt_int_cmp(@_) >= 0 }
Subroutine _dt_int_ge: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!defined($_[1]) ? : 2 2 4
153  
154 6 ▶sub _dt_str_eq {
Subroutine _dt_str_eq: called CC=4 SCAR=14.3
155 6 ▶ return undef unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 2 4 6
156
157 4 my ($side_a, $side_b) = _promote(@_);
158
159 4 ▶ return ("$side_a" || '') eq ("$side_b" || '');
Condition50%2/4
"$side_a" || ''
l!l
40
"$side_b" || ''
l!l
40
MC/DC0%0/1
"$side_a" || ''
"$side_a"
MC/DC0%0/1
"$side_b" || ''
"$side_b"
Truth table50%1/2
A
A= "$side_a"
Aresult
11
00
Truth table50%1/2
A
A= "$side_b"
Aresult
11
00
160 }
161  
162 12 ▶sub _dt_str_cmp {
Subroutine _dt_str_cmp: called CC=9 SCAR=24.8
163 12 ▶ return undef unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 2 10 12
164
165 10 my ($side_a, $side_b) = _promote(@_);
166
167 10 ▶ $side_b = DT->new($side_b)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $side_b and $side_b->isa("DateTime") 0 10 10
Condition33%1/3
ref $side_b and $side_b->isa("DateTime")
!ll&&!rl&&r
0010
MC/DC0%0/2
ref $side_b and $side_b->isa("DateTime")
ref $side_b $side_b->isa("DateTime")
Truth table33%1/3
A and B
A= ref $side_bB= $side_b->isa("DateTime")
ABresult
0X0
100
111
168 if not ref($side_b) or not $side_b->isa('DateTime');
169
170 10 ▶ return $_[2] ? ("$side_b" || '') cmp ("$side_a" || '')
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 5 5 10
Condition50%4/8
"$side_b" || ''
l!l
50
"$side_a" || ''
l!l
50
"$side_a" || ''
l!l
50
"$side_b" || ''
l!l
50
MC/DC0%0/1
"$side_b" || ''
"$side_b"
MC/DC0%0/1
"$side_a" || ''
"$side_a"
MC/DC0%0/1
"$side_a" || ''
"$side_a"
MC/DC0%0/1
"$side_b" || ''
"$side_b"
Truth table50%1/2
A
A= "$side_b"
Aresult
11
00
Truth table50%1/2
A
A= "$side_a"
Aresult
11
00
Truth table50%1/2
A
A= "$side_a"
Aresult
11
00
Truth table50%1/2
A
A= "$side_b"
Aresult
11
00
171 : ("$side_a" || '') cmp ("$side_b" || '')
172 ;
173 }
174  
175 4 ▶sub _dt_str_ne { !defined($_[1]) ? undef : !_dt_str_eq(@_) }
Subroutine _dt_str_ne: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!defined($_[1]) ? : 2 2 4
176 4 ▶sub _dt_str_lt { !defined($_[1]) ? undef : _dt_str_cmp(@_) < 0 }
Subroutine _dt_str_lt: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!defined($_[1]) ? : 2 2 4
177 4 ▶sub _dt_str_le { !defined($_[1]) ? undef : _dt_str_cmp(@_) <= 0 }
Subroutine _dt_str_le: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!defined($_[1]) ? : 2 2 4
178 4 ▶sub _dt_str_gt { !defined($_[1]) ? undef : _dt_str_cmp(@_) > 0 }
Subroutine _dt_str_gt: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!defined($_[1]) ? : 2 2 4
179 4 ▶sub _dt_str_ge { !defined($_[1]) ? undef : _dt_str_cmp(@_) >= 0 }
Subroutine _dt_str_ge: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!defined($_[1]) ? : 2 2 4
180  
181 5 1;
182  
183 __END__
↑ Summary