lib/DR/DateTime.pm

filter: Statementstmt 91.7% 212 / 231 Branchbran 77.6% 59 / 76 Conditioncond 66.6% 24 / 36 MC/DCmcdc 20.0% 4 / 20 Subroutinesub 87.5% 42 / 48 Podpod 76.9% 20 / 26 totaltotal 82.6% 361 / 437 CC 74 SCAR 20.6
CC
74
Cov
86%
add
18.6
parse
17.6
new
9.3
CRAP 7.8
« blib/lib/DR/DateTime.pm ↑ Summary blib/lib/DR/DateTime/Defaults.pm »
1 package DR::DateTime;
2 7 ▶use DR::DateTime::Defaults;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 7 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 our $VERSION = '0.07';
8 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Time::Local ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Time::Zone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 use overload
16 9 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 3 ▶ '""' => sub { shift->strftime('%F %T%z') },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 '<=>' => sub {
19 2 ▶ my ($self, $cv, $flip) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
20 2 ▶ if ('DR::DateTime' eq ref $cv) {
Branch: 1 branch
BranchTrueFalseTotal
if ("DR::DateTime" eq ref $cv) 1 1 2
21 1 return $self->fepoch <=> $cv->fepoch;
22 }
23 1 return $self->fepoch <=> $cv;
24 },
25  
26 'cmp' => sub {
27 2 ▶ my ($self, $cv, $flip) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
28 2 ▶ if ('DR::DateTime' eq ref $cv) {
Branch: 1 branch
BranchTrueFalseTotal
if ("DR::DateTime" eq ref $cv) 1 1 2
29 1 return $self->strftime('%F %T%z') cmp $cv->strftime('%F %T%z');
30 }
31 1 my $pct = $self->parse($cv);
32 1 ▶ return $self->strftime('%F %T%z') cmp $cv unless $pct;
Branch: 1 branch
BranchTrueFalseTotal
unless $pct 0 1 1
33 1 return $self->strftime('%F %T%z') cmp $pct->strftime('%F %T%z');
34 },
35  
36 2 ▶ int => sub { shift->epoch }
Subroutine __ANON__: called CC=1 SCAR=0.0
37 7 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39  
40 sub new {
41 45 ▶ my ($class, $stamp, $tz) = @_;
Subroutine new: called CC=9 SCAR=22.3
Pod documented
42 45 ▶ $stamp //= time;
Condition66%2/3
$stamp //= time
l!l&&r!l&&!r
4140
MC/DC0%0/2
$stamp //= time
$stamp time
Truth table66%2/3
A //= B
A= $stampB= time
ABresult
1X1
011
000
43  
44 45 ▶ if (defined $tz) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tz) 39 6 45
45 39 $tz =~ /^([+-])?(\d{2})(\d{2})?$/;
46 39 ▶ croak "Wrong timezone format" unless defined $2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $2 0 39 39
47  
48 39 ▶ $tz = join '',
Condition100%4/4
$1 // "+"
l!l
354
$3 // "00"
l!l
363
MC/DC100%1/1
$1 // "+"
$1
MC/DC100%1/1
$3 // "00"
$3
Truth table100%2/2
A
A= $1
Aresult
11
00
Truth table100%2/2
A
A= $3
Aresult
11
00
49 $1 // '+',
50 $2,
51 $3 // '00';
52 }
53  
54 45 ▶ $tz = $DR::DateTime::Defaults::TZFORCE
Branch: 1 branch
BranchTrueFalseTotal
if defined $DR::DateTime::Defaults::TZFORCE 2 43 45
55 if defined $DR::DateTime::Defaults::TZFORCE;
56  
57 45 ▶ bless [ $stamp, $tz // () ] => ref($class) || $class;
Condition66%4/6
$tz // ()
l!l&&r!l&&!r
4140
ref $class || $class
l!l&&r!l&&!r
15300
MC/DC0%0/2
$tz // ()
$tz ()
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A // B
A= $tzB= ()
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
58 }
59  
60 sub parse {
61 35 ▶ my ($class, $str, $default_tz, $nocheck) = @_;
Subroutine parse: called CC=17 SCAR=28.7
Pod documented
62 35 ▶ return undef unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 35 35
63 35 my ($y, $m, $d, $H, $M, $S, $ns, $z);
64  
65 35 for ($str) {
66  
67 35 ▶ if (/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\d+$/) 1 34 35
68 1 ▶ return $class->new($str, $default_tz // '+0000');
Condition50%1/2
$default_tz // "+0000"
l!l
01
MC/DC0%0/1
$default_tz // "+0000"
$default_tz
Truth table50%1/2
A
A= $default_tz
Aresult
11
00
69 }
70 34 ▶ if (/^(\d{4})-(\d{2})-(\d{2})(?:\s+|T)(\d{2}):(\d{2}):(\d{2})(\.\d+)?\s*(\S+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d{4})-(\d{2})-(\d{2})(?:\s+|T)(\d{2}):(\d{2}):(\d{2})(\.\d+)?\s*(\S+)?$/) 28 6 34
71 28 ($y, $m, $d, $H, $M, $S, $ns, $z) =
72 ($1, $2, $3, $4, $5, $6, $7, $8);
73 28 goto PARSED;
74 }
75
76 6 ▶ if (/^(\d{4})-(\d{2})-(\d{2})(?:\s+|T)(\d{2}):(\d{2})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d{4})-(\d{2})-(\d{2})(?:\s+|T)(\d{2}):(\d{2})$/) 2 4 6
77 2 ($y, $m, $d, $H, $M, $S, $ns, $z) =
78 ($1, $2, $3, $4, $5, 0, 0, undef);
79 2 goto PARSED;
80 }
81
82 4 ▶ if (/^(\d{4})-(\d{2})-(\d{2})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d{4})-(\d{2})-(\d{2})$/) 1 3 4
83 1 ($y, $m, $d, $H, $M, $S, $ns, $z) =
84 ($1, $2, $3, 0, 0, 0, 0, undef);
85 1 goto PARSED;
86 }
87  
88 3 ▶ if (/^(\d{1,2})\.(\d{1,2})\.(\d{4})\s+(\d{2}):(\d{2}):(\d{2})(\.\d+)?\s*(\S+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d{1,2})\.(\d{1,2})\.(\d{4})\s+(\d{2}):(\d{2}):(\d{2})(\.\d+)?\s*(\S+)?$/) 3 0 3
89 3 ($y, $m, $d, $H, $M, $S, $ns, $z) =
90 ($3, $2, $1, $4, $5, $6, $7, $8);
91 3 goto PARSED;
92 }
93  
94 0 return undef;
95 }
96  
97  
98 PARSED:
99  
100 34 ▶ $z //= $default_tz // '+0000';
Condition60%3/5
$z //= $default_tz // "+0000"
l!l&&r!l&&!r
2950
$default_tz // "+0000"
l!l
05
MC/DC0%0/2
$z //= $default_tz // "+0000"
$z $default_tz
Truth table33%1/3
A //= B
A= $zB= $default_tz
ABresult
1X1
011
000
101 34 for ($z) {
102 34 ▶ if (/^[+-]\d{1,4}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[+-]\d{1,4}$/) { } 34 0 34
103 34 s/^([+-])(\d|\d{3})$/${1}0$2/;
104 34 s/^([+-])(\d{2})$/${1}${2}00/;
105 } else {
106 0 croak "Wrong time zone format: '$z'";
107 }
108 }
109 34 for ($m) {
110 34 s/^0//;
111 34 $_--;
112 }
113 34 for ($d, $H, $M, $S) {
114 136 s/^0//;
115 }
116 34 $y -= 1900;
117  
118 34 ▶ $ns //= 0;
Condition100%2/2
$ns //= 0
l!l
1816
MC/DC100%1/1
$ns //= 0
$ns
Truth table100%2/2
A
A= $ns
Aresult
11
00
119 34 my $stamp = eval {
120 34 ▶ local $SIG{__DIE__} = sub {}; # Ick!
Subroutine __ANON__: not called CC=1 SCAR=0.0
121 34 ▶ return Time::Local::timegm_nocheck($S,$M,$H,$d,$m,$y) if $nocheck;
Branch: 1 branch
BranchTrueFalseTotal
if $nocheck 9 25 34
122 25 Time::Local::timegm($S,$M,$H,$d,$m,$y);
123 };
124 34 $stamp += $ns;
125  
126 34 my $offset = Time::Zone::tz_offset($z, $stamp);
127 34 $class->new($stamp - $offset, $z);
128 }
129  
130 136 ▶sub fepoch { shift->[0] }
Subroutine fepoch: called CC=1 SCAR=0.0
Pod documented
131 281 ▶sub epoch { POSIX::floor(shift->[0]) }
Subroutine epoch: called CC=1 SCAR=0.0
Pod documented
132 195 ▶sub tz { shift->[1] // $DR::DateTime::Defaults::TZ }
Subroutine tz: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
+(shift())->[1] // $DR::DateTime::Defaults::TZ
l!l&&r!l&&!r
181140
MC/DC0%0/2
+(shift())->[1] // $DR::DateTime::Defaults::TZ
+(shift())->[1] $DR::DateTime::Defaults::TZ
Truth table66%2/3
A // B
A= +(shift())->[1]B= $DR::DateTime::Defaults::TZ
ABresult
1X1
011
000
133  
134 sub strftime :method {
135 123 ▶ my ($self, $format) = @_;
Subroutine strftime: called CC=5 SCAR=16.3
Pod documented
136 123 ▶ croak 'Invalid format' unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 0 123 123
137 123 my $offset = Time::Zone::tz_offset($self->tz, $self->epoch);
138 123 my $stamp = $self->epoch + $offset;
139 123 my $fstamp = $self->fepoch + $offset;
140  
141 123 state $patterns;
142 123 ▶ unless ($patterns) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($patterns) 7 116 123
143 $patterns = {
144 0 ▶ '%' => sub { '%' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 47 ▶ 'z' => sub { shift->tz },
Subroutine __ANON__: called CC=1 SCAR=0.0
146 0 ▶ 'Z' => sub { shift->tz },
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 'N' => sub {
148 20 ▶ int(1_000_000_000 * abs($_[2] - $_[1])) }
Subroutine __ANON__: called CC=1 SCAR=0.0
149  
150 7 };
151 7 for my $sp (split //, 'aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyY') {
152 172 ▶ $patterns->{$sp} = sub { POSIX::strftime "%$sp", gmtime $_[1] }
Subroutine __ANON__: called CC=1 SCAR=0.0
153 280 }
154 }
155  
156 123 $format =~ s{%([a-zA-Z%])}
157 239 ▶ { $patterns->{$1} ? $patterns->{$1}->($self, $stamp, $fstamp) : "%$1" }sgex;
Branch: 1 branch
BranchTrueFalseTotal
$patterns->{$1} ? : 239 0 239
158  
159 123 $format;
160 }
161  
162  
163 7 ▶sub year { shift->strftime('%Y') }
Subroutine year: called CC=1 SCAR=0.0
Pod documented
164  
165 sub month {
166 9 ▶ for my $m (shift->strftime('%m')) {
Subroutine month: called CC=2 SCAR=6.9
Pod documented
167 9 $m =~ s/^0//;
168 9 return $m;
169 }
170 }
171  
172 sub day {
173 28 ▶ for my $d (shift->strftime('%d')) {
Subroutine day: called CC=2 SCAR=6.9
Pod documented
174 28 $d =~ s/^0//;
175 28 return $d;
176 }
177 }
178  
179 2 ▶sub day_of_week { shift->strftime('%u') }
Subroutine day_of_week: called CC=1 SCAR=0.0
Pod documented
180  
181 2 ▶sub quarter { POSIX::ceil(shift->month / 3) }
Subroutine quarter: called CC=1 SCAR=0.0
Pod undocumented
182  
183 sub hour {
184 11 ▶ for my $h (shift->strftime('%H')) {
Subroutine hour: called CC=2 SCAR=6.9
Pod documented
185 11 $h =~ s/^0//;
186 11 return $h;
187 }
188 }
189  
190 sub minute {
191 6 ▶ for my $m (shift->strftime('%M')) {
Subroutine minute: called CC=2 SCAR=6.9
Pod documented
192 6 $m =~ s/^0//;
193 6 return $m;
194 }
195 }
196 sub second {
197 4 ▶ for my $s (shift->strftime('%S')) {
Subroutine second: called CC=2 SCAR=6.9
Pod documented
198 4 $s =~ s/^0//;
199 4 return $s;
200 }
201 }
202  
203 4 ▶sub nanosecond { shift->strftime('%N') }
Subroutine nanosecond: called CC=1 SCAR=0.0
Pod documented
204  
205 sub hms {
206 2 ▶ my ($self, $sep) = @_;
Subroutine hms: called CC=3 SCAR=11.1
Pod undocumented
207 2 ▶ $sep //= ':';
Condition50%1/2
$sep //= ":"
l!l
20
MC/DC0%0/1
$sep //= ":"
$sep
Truth table50%1/2
A
A= $sep
Aresult
11
00
208 2 for ($sep) {
209 2 s/%/%%/g;
210 }
211 2 $self->strftime("%H$sep%M$sep%S");
212 }
213  
214 sub datetime {
215 0 ▶ my ($self) = @_;
Subroutine datetime: not called CC=1 SCAR=6.9
Pod undocumented
216 0 return join 'T', $self->ymd, $self->hms;
217 }
218  
219 sub ymd {
220 2 ▶ my ($self, $sep) = @_;
Subroutine ymd: called CC=3 SCAR=11.1
Pod undocumented
221 2 ▶ $sep //= ':';
Condition50%1/2
$sep //= ":"
l!l
20
MC/DC0%0/1
$sep //= ":"
$sep
Truth table50%1/2
A
A= $sep
Aresult
11
00
222 2 for ($sep) {
223 2 s/%/%%/g;
224 }
225 2 $self->strftime("%Y$sep%m$sep%d");
226 }
227  
228 2 ▶sub time_zone { goto \&tz }
Subroutine time_zone: called CC=1 SCAR=0.0
Pod documented
229 0 ▶sub hires_epoch { goto \&fepoch }
Subroutine hires_epoch: not called CC=1 SCAR=6.9
Pod documented
230 sub _fix_date_after_arith_month {
231 9 ▶ my ($self, $new) = @_;
Subroutine _fix_date_after_arith_month: called CC=3 SCAR=11.0
232 9 ▶ return $new->fepoch if $self->day == $new->day;
Branch: 1 branch
BranchTrueFalseTotal
if $self->day == $new->day 7 2 9
233 2 ▶ if ($new->day < $self->day) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new->day < $self->day) 2 0 2
234 2 $new->[0] -= 86400;
235 }
236 2 $new->fepoch;
237 }
238 sub add {
239 6 ▶ my ($self, %set) = @_;
Subroutine add: called CC=18 SCAR=29.2
Pod documented
240
241 6 for my $n (delete $set{nanosecond}) {
242 6 ▶ last unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 6 0 6
243 0 $self->[0] += $n / 1_000_000_000;
244 }
245  
246 6 for my $s (delete $set{second}) {
247 6 ▶ last unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 5 1 6
248 1 $self->[0] += $s;
249 }
250  
251 6 for my $m (delete $set{minute}) {
252 6 ▶ last unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 6 0 6
253 0 $self->[0] += $m * 60;
254 }
255
256 6 for my $h (delete $set{hour}) {
257 6 ▶ last unless defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 5 1 6
258 1 $self->[0] += $h * 3600;
259 }
260  
261 6 for my $d (delete $set{day}) {
262 6 ▶ last unless defined $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 5 1 6
263 1 $self->[0] += $d * 86400;
264 }
265  
266 6 for my $m (delete $set{month}) {
267 6 ▶ last unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 2 4 6
268 4 my $nm = $self->month + $m;
269  
270 4 ▶ $set{year} //= 0;
Condition100%2/2
$set{"year"} //= 0
l!l
13
MC/DC100%1/1
$set{"year"} //= 0
$set{"year"}
Truth table100%2/2
A
A= $set{"year"}
Aresult
11
00
271 4 ▶ while ($nm > 12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nm > 12) 1 4 5
272 1 $nm -= 12;
273 1 $set{year}++;
274 }
275  
276 4 ▶ while ($nm < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nm < 1) 0 4 4
277 0 $nm += 12;
278 0 $set{year}--;
279 }
280 4 my $str = $self->strftime('%F %T.%N %z');
281 4 $str =~ s/(\d{4})-\d{2}-/sprintf "%s-%02d-", $1, $nm/e;
282 4 $self->[0] =
283 $self->_fix_date_after_arith_month($self->parse($str, undef, 1));
284 }
285  
286 6 for my $y (delete $set{year}) {
287 6 ▶ last unless defined $y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y 1 5 6
288 5 $y += $self->year;
289 5 my $str = $self->strftime('%F %T.%N %z');
290 5 $str =~ s/^\d{4}/$y/;
291 5 $self->[0] =
292 $self->_fix_date_after_arith_month($self->parse($str, undef, 1));
293 }
294 6 $self;
295 }
296  
297 sub subtract {
298 0 ▶ my ($self, %set) = @_;
Subroutine subtract: not called CC=2 SCAR=17.9
Pod undocumented
299  
300 0 my %sub;
301 0 ▶ while (my ($k, $v) = each %set) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %set) 0 0 0
302 0 $sub{$k} = -$v;
303 }
304 0 $self->add(%sub);
305 }
306  
307 sub truncate {
308 6 ▶ my ($self, %opts) = @_;
Subroutine truncate: called CC=8 SCAR=20.8
Pod documented
309  
310 6 ▶ my $to = $opts{to} // 'second';
Condition50%1/2
$opts{"to"} // "second"
l!l
60
MC/DC0%0/1
$opts{"to"} // "second"
$opts{"to"}
Truth table50%1/2
A
A= $opts{"to"}
Aresult
11
00
311  
312 6 ▶ if ($to eq 'second') {
Branch: 1 branch
BranchTrueFalseTotal
if ($to eq "second") 1 5 6
313 1 $self->[0] = $self->epoch;
314 1 return $self;
315 }
316  
317 5 my $str;
318 5 ▶ if ($to eq 'minute') {
Branch: 1 branch
BranchTrueFalseTotal
if ($to eq "minute") 1 4 5
319 1 $str = $self->strftime('%F %H:%M:00%z');
320 1 goto PARSE;
321 }
322  
323 4 ▶ if ($to eq 'hour') {
Branch: 1 branch
BranchTrueFalseTotal
if ($to eq "hour") 1 3 4
324 1 $str = $self->strftime('%F %H:00:00%z');
325 1 goto PARSE;
326 }
327
328 3 ▶ if ($to eq 'day') {
Branch: 1 branch
BranchTrueFalseTotal
if ($to eq "day") 1 2 3
329 1 $str = $self->strftime('%F 00:00:00%z');
330 1 goto PARSE;
331 }
332  
333 2 ▶ if ($to eq 'month') {
Branch: 1 branch
BranchTrueFalseTotal
if ($to eq "month") 1 1 2
334 1 $str = $self->strftime('%Y-%m-01 00:00:00%z');
335 1 goto PARSE;
336 }
337
338 1 ▶ if ($to eq 'year') {
Branch: 1 branch
BranchTrueFalseTotal
if ($to eq "year") 1 0 1
339 1 $str = $self->strftime('%Y-01-01 00:00:00%z');
340 1 goto PARSE;
341 }
342  
343 0 croak "Can not truncate the datetime to '$to'";
344  
345 5 PARSE:
346 $self->[0] = $self->parse($str)->epoch;
347 5 $self;
348 }
349  
350 sub clone {
351 1 ▶ my ($self) = @_;
Subroutine clone: called CC=2 SCAR=8.1
Pod documented
352 1 ▶ bless [ @$self ] => ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
353 }
354  
355 sub set_time_zone {
356 1 ▶ my ($self, $tz) = @_;
Subroutine set_time_zone: called CC=5 SCAR=17.1
Pod documented
357 1 ▶ if (defined $tz) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tz) 1 0 1
358 1 for ($tz) {
359 1 s/^\d{1,4}$/+$&/;
360 1 s/^([+-])(\d)$/${1}0${2}00/;
361 1 s/^([+-]\d{2})$/${1}00/;
362 1 s/^([+-])(\d{3})$/${1}0$2/;
363 }
364  
365 1 ▶ if ($tz eq 'local') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tz eq 'local') { } 0 1 1
elsif (not $tz =~ /^[+-]\d{4}$/) { } 0 1 1
366 0 $tz = undef;
367 } elsif ($tz !~ /^[+-]\d{4}$/) {
368 0 croak "Wrong time zone: '$tz'";
369 }
370 }
371 1 $self->[1] = $tz;
372 1 $self;
373 }
374  
375 1 ▶sub set_tz { goto \&set_time_zone }
Subroutine set_tz: called CC=1 SCAR=0.0
Pod undocumented
376  
377 7 1;
378  
379 __END__
« blib/lib/DR/DateTime.pm ↑ Summary blib/lib/DR/DateTime/Defaults.pm »