blib/lib/PDL/DateTime.pm

filter: Statementstmt 73.7% 345 / 468 Branchbran 52.0% 151 / 290 Conditioncond 15.9% 11 / 69 MC/DCmcdc 2.3% 1 / 43 Subroutinesub 75.6% 59 / 78 Podpod 90.9% 40 / 44 totaltotal 61.1% 607 / 992 CC 175 SCAR 36.8
CC
175
Cov
61%
dt_periodicity
342.0
dt_diff
90.0
dt_endpoints
42.0
CRAP 39.5
↑ Summary
1 package PDL::DateTime;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use parent 'PDL';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '0.003';
8  
9 5 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use PDL::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use PDL::Primitive;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use PDL::Basic qw(sequence);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use PDL::Math qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use PDL::Core qw(longlong long double byte short indx);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5 use overload '>' => \&_num_compare_gt,
20 '<' => \&_num_compare_lt,
21 '>=' => \&_num_compare_ge,
22 '<=' => \&_num_compare_le,
23 '==' => \&_num_compare_eq,
24 '!=' => \&_num_compare_ne,
25 5 ▶ '""' => \&_stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 my %INC_SECONDS = (
28 week => 60 * 60 * 24 * 7,
29 day => 60 * 60 * 24,
30 hour => 60 * 60,
31 minute => 60,
32 second => 1,
33 );
34  
35 sub initialize {
36 2737 ▶ my ($class, %args) = @_;
Subroutine initialize: called CC=2 SCAR=6.9
Pod undocumented
37 2737 ▶ $class = ref $class ? ref $class : $class;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 2105 632 2737
38 2737 return bless { %args, PDL => PDL->null }, $class;
39 }
40  
41 sub new {
42 10 ▶ my ($class, $data, %opts) = @_;
Subroutine new: called CC=8 SCAR=28.2
Pod documented
43  
44 # for 'PDL::DateTime' just make a copy
45 10 ▶ return $data->copy(%opts) if ref $data eq 'PDL::DateTime';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "PDL::DateTime" 1 9 10
46  
47 9 my $self = $class->initialize(%opts);
48 # $data is expected to contain epoch timestamps in microseconds
49 9 ▶ if (ref $data eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 1 8 9
elsif (ref $data eq 'PDL') { } 8 0 8
50 1 $self->{PDL} = longlong($data);
51 }
52 elsif (ref $data eq 'PDL') {
53 8 ▶ if ($data->type == longlong) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data->type == longlong()) { } 8 0 8
elsif ($data->type == double()) { } 0 0 0
54 8 $self->{PDL} = $data->copy;
55 # NOTE:
56 # $x = sequence(longlong, 6) # type LL
57 # $u = long($x) # == clone/copy of $x (type L)
58 # $u = longlong($x) # == same data, same type as $x
59 # $w = PDL->new($x) # == clone/copy of $x (type LL)
60 }
61 elsif ($data->type == double) {
62 0 $self->{PDL} = longlong(floor($data + 0.5));
63 0 $self->{PDL} -= $self->{PDL} % 1000; #truncate to milliseconds
64 }
65 else {
66 0 $self->{PDL} = longlong($data);
67 }
68 }
69 else {
70 0 ▶ if (looks_like_number $data) {
Branch: 2 branches
BranchTrueFalseTotal
if (&looks_like_number($data)) { } 0 0 0
elsif ($data) { } 0 0 0
71 0 $self->{PDL} = longlong($data);
72 }
73 elsif ($data) {
74 0 $self->{PDL} = longlong(_datetime_to_jumboepoch($data));
75 }
76 else {
77 0 croak "PDL::DateTime->new: invalid data";
78 }
79 }
80  
81 9 return $self;
82 }
83  
84 # Derived objects need to supply its own copy!
85 sub copy {
86 34 ▶ my ($self, %opts) = @_;
Subroutine copy: called CC=1 SCAR=0.0
Pod undocumented
87 34 my $new = $self->initialize(%opts);
88 # copy the PDL
89 34 $new->{PDL} = $self->{PDL}->SUPER::copy;
90 # copy the other stuff
91 #$new->{someThingElse} = $self->{someThingElse};
92 34 return $new;
93 }
94  
95 sub new_from_epoch {
96 3 ▶ my ($class, $ep, %opts) = @_;
Subroutine new_from_epoch: called CC=2 SCAR=6.9
Pod documented
97 3 my $self = $class->initialize(%opts);
98 3 ▶ $ep = double($ep) if ref $ep eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ep eq "ARRAY" 1 2 3
99 # convert epoch timestamp in seconds to microseconds
100 3 $self->{PDL} = longlong(floor(double($ep) * 1_000 + 0.5)) * 1000;
101 3 return $self;
102 }
103  
104 sub new_from_ratadie {
105 3 ▶ my ($class, $rd, %opts) = @_;
Subroutine new_from_ratadie: called CC=2 SCAR=6.9
Pod documented
106 3 my $self = $class->initialize(%opts);
107 3 ▶ $rd = double($rd) if ref $rd eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rd eq "ARRAY" 1 2 3
108 # EPOCH = (RD - 719_163) * 86_400
109 # only milisecond precision => strip microseconds
110 3 $self->{PDL} = longlong(floor((double($rd) - 719_163) * 86_400_000 + 0.5)) * 1000;
111 3 return $self;
112 }
113  
114 sub new_from_serialdate {
115 3 ▶ my ($class, $sd, %opts) = @_;
Subroutine new_from_serialdate: called CC=2 SCAR=6.9
Pod documented
116 3 my $self = $class->initialize(%opts);
117 3 ▶ $sd = double($sd) if ref $sd eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $sd eq "ARRAY" 1 2 3
118 # EPOCH = (SD - 719_163 - 366) * 86_400
119 # only milisecond precision => strip microseconds
120 3 $self->{PDL} = longlong(floor((double($sd) - 719_529) * 86_400_000 + 0.5)) * 1000;
121 3 return $self;
122 }
123  
124 sub new_from_juliandate {
125 3 ▶ my ($class, $jd, %opts) = @_;
Subroutine new_from_juliandate: called CC=2 SCAR=6.9
Pod documented
126 3 my $self = $class->initialize(%opts);
127 3 ▶ $jd = double($jd) if ref $jd eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $jd eq "ARRAY" 1 2 3
128 # EPOCH = (JD - 2_440_587.5) * 86_400
129 # only milisecond precision => strip microseconds
130 3 $self->{PDL} = longlong(floor((double($jd) - 2_440_587.5) * 86_400_000 + 0.5)) * 1000;
131 3 return $self;
132 }
133  
134 sub new_from_datetime {
135 32 ▶ my ($class, $array, %opts) = @_;
Subroutine new_from_datetime: called CC=1 SCAR=0.0
Pod documented
136 32 my $self = $class->initialize(%opts);
137 32 $self->{PDL} = longlong _datetime_to_jumboepoch($array);
138 32 return $self;
139 }
140  
141 sub new_from_parts {
142 3 ▶ my ($class, $y, $m, $d, $H, $M, $S, $U, %opts) = @_;
Subroutine new_from_parts: called CC=15 SCAR=27.2
Pod documented
143 3 ▶ croak "new_from_parts: args - y, m, d - are mandatory" unless defined $y && defined $m && defined $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y and defined $m and defined $d 0 3 3
Condition33%2/6
defined $y and defined $m and defined $d
!ll&&!rl&&r
003
defined $y and defined $m
!ll&&!rl&&r
003
MC/DC0%0/3
defined $y and defined $m and defined $d
defined $y defined $m defined $d
Truth table25%1/4
A and B and C
A= defined $yB= defined $mC= defined $d
ABCresult
0XX0
10X0
1100
1111
144 3 my $self = $class->initialize(%opts);
145 3 ▶ $y = long($y) if ref $y eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $y eq "ARRAY" 1 2 3
146 3 ▶ $d = long($d) if ref $d eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $d eq "ARRAY" 1 2 3
147 3 ▶ $m = long($m) if ref $m eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $m eq "ARRAY" 1 2 3
148 3 ▶ $H = long($H) if ref $H eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $H eq "ARRAY" 1 2 3
149 3 ▶ $M = long($M) if ref $M eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $M eq "ARRAY" 1 2 3
150 3 ▶ $S = long($S) if ref $S eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $S eq "ARRAY" 1 2 3
151 3 ▶ $U = long($U) if ref $U eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $U eq "ARRAY" 1 2 3
152 3 my $rdate = _ymd2ratadie($y->copy, $m->copy, $d->copy);
153 3 my $epoch = (floor($rdate) - 719163) * 86400;
154 3 ▶ $epoch += floor($H) * 3600 if defined $H;
Branch: 1 branch
BranchTrueFalseTotal
if defined $H 2 1 3
155 3 ▶ $epoch += floor($M) * 60 if defined $M;
Branch: 1 branch
BranchTrueFalseTotal
if defined $M 2 1 3
156 3 ▶ $epoch += floor($S) if defined $S;
Branch: 1 branch
BranchTrueFalseTotal
if defined $S 2 1 3
157 3 $epoch = longlong($epoch) * 1_000_000;
158 3 ▶ $epoch += longlong(floor($U)) if defined $U;
Branch: 1 branch
BranchTrueFalseTotal
if defined $U 2 1 3
159 3 $self->{PDL} = longlong($epoch);
160 3 return $self;
161 }
162  
163 sub new_from_ymd {
164 1 ▶ my ($class, $ymd) = @_;
Subroutine new_from_ymd: called CC=1 SCAR=0.0
Pod documented
165 1 my $y = floor(($ymd/10000) % 10000);
166 1 my $m = floor(($ymd/100) % 100);
167 1 my $d = floor($ymd % 100);
168 1 return $class->new_from_parts($y, $m, $d);
169 }
170  
171 sub new_sequence {
172 44 ▶ my ($class, $start, $count, $unit, $step, %opts) = @_;
Subroutine new_sequence: called CC=12 SCAR=25.9
Pod documented
173 44 ▶ croak "new_sequence: args - count, unit - are mandatory" unless defined $count && defined $unit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count and defined $unit 0 44 44
Condition33%1/3
defined $count and defined $unit
!ll&&!rl&&r
0044
MC/DC0%0/2
defined $count and defined $unit
defined $count defined $unit
Truth table33%1/3
A and B
A= defined $countB= defined $unit
ABresult
0X0
100
111
174 44 ▶ $step = 1 unless defined $step;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $step 5 39 44
175 44 my $self = $class->initialize(%opts);
176 44 ▶ my $tm_start = $start eq 'now' ? Time::Moment->now_utc : _dt2tm($start);
Branch: 1 branch
BranchTrueFalseTotal
$start eq 'now' ? : 0 44 44
177 44 my $microseconds = $tm_start->microsecond;
178 44 ▶ if ($unit eq 'year') {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq "year") 5 39 44
179 # slow :(
180 5 my @epoch = ($tm_start->epoch);
181 5 push @epoch, $tm_start->plus_years($_*$step)->epoch for (1..$count-1);
182 5 $self->{PDL} = longlong(\@epoch) * 1_000_000 + $microseconds;
183 }
184 44 ▶ if ($unit eq 'quarter') {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq "quarter") 0 44 44
185 # slow :(
186 0 my @epoch = ($tm_start->epoch);
187 0 push @epoch, $tm_start->plus_months(3*$_*$step)->epoch for (1..$count-1);
188 0 $self->{PDL} = longlong(\@epoch) * 1_000_000 + $microseconds;
189 }
190 44 ▶ if ($unit eq 'month') {
Branch: 2 branches
BranchTrueFalseTotal
if ($unit eq 'month') { } 12 32 44
elsif (my $inc = $INC_SECONDS{$unit}) { } 27 5 32
191 # slow :(
192 12 my @epoch = ($tm_start->epoch);
193 12 push @epoch, $tm_start->plus_months($_*$step)->epoch for (1..$count-1);
194 12 $self->{PDL} = longlong(\@epoch) * 1_000_000 + $microseconds;
195 }
196 elsif (my $inc = $INC_SECONDS{$unit}) { # week day hour minute second
197 27 my $epoch = $tm_start->epoch;
198 27 $self->{PDL} = (longlong(floor(sequence($count) * $step * $inc + 0.5)) + $epoch) * 1_000_000 + $microseconds;
199 }
200 44 return $self;
201 }
202  
203 sub double_epoch {
204 7 ▶ my $self = shift;
Subroutine double_epoch: called CC=1 SCAR=0.0
Pod documented
205 # EP = JUMBOEPOCH / 1_000_000;
206 7 my $epoch_milisec = ($self - ($self % 1000)) / 1000; # BEWARE: precision only in milliseconds!
207 7 return double($epoch_milisec) / 1_000;
208 }
209  
210 sub longlong_epoch {
211 2 ▶ my $self = shift;
Subroutine longlong_epoch: called CC=1 SCAR=0.0
Pod documented
212 # EP = JUMBOEPOCH / 1_000_000;
213 # BEWARE: precision only in seconds!
214 2 my $epoch_sec = ($self - ($self % 1_000_000)) / 1_000_000;
215 2 return longlong($epoch_sec->{PDL});
216 }
217  
218 sub double_ratadie {
219 42 ▶ my $self = shift;
Subroutine double_ratadie: called CC=1 SCAR=0.0
Pod documented
220 # RD = EPOCH / 86_400 + 719_163;
221 42 my $epoch_milisec = ($self - ($self % 1000)) / 1000; # BEWARE: precision only in milliseconds!
222 42 return double($epoch_milisec) / 86_400_000 + 719_163;
223 }
224  
225 sub double_serialdate {
226 7 ▶ my $self = shift;
Subroutine double_serialdate: called CC=1 SCAR=0.0
Pod documented
227 # SD = EPOCH / 86_400 + 719_163 + 366;
228 7 my $epoch_milisec = ($self - ($self % 1000)) / 1000; # BEWARE: precision only in milliseconds!
229 7 return double($epoch_milisec) / 86_400_000 + 719_529;
230 }
231  
232 sub double_juliandate {
233 6 ▶ my $self = shift;
Subroutine double_juliandate: called CC=1 SCAR=0.0
Pod documented
234 # JD = EPOCH / 86_400 + 2_440_587.5;
235 6 my $epoch_milisec = ($self - ($self % 1000)) / 1000; # BEWARE: precision only in milliseconds!
236 6 return double($epoch_milisec) / 86_400_000 + 2_440_587.5;
237 }
238  
239 sub dt_ymd {
240 1 ▶ my $self = shift;
Subroutine dt_ymd: called CC=1 SCAR=0.0
Pod documented
241 1 my ($y, $m, $d) = _ratadie2ymd($self->double_ratadie);
242 1 return (short($y), byte($m), byte($d));
243 }
244  
245 sub dt_year {
246 1 ▶ my $self = shift;
Subroutine dt_year: called CC=1 SCAR=0.0
Pod documented
247 1 my ($y, undef, undef) = _ratadie2ymd($self->double_ratadie);
248 1 return short($y);
249 }
250  
251 sub dt_quarter {
252 1 ▶ my $self = shift;
Subroutine dt_quarter: called CC=1 SCAR=0.0
Pod documented
253 1 my (undef, $m, undef) = _ratadie2ymd($self->double_ratadie);
254 1 return ((byte($m)-1) / 3) + 1;
255 }
256  
257 sub dt_month {
258 1 ▶ my $self = shift;
Subroutine dt_month: called CC=1 SCAR=0.0
Pod documented
259 1 my (undef, $m, undef) = _ratadie2ymd($self->double_ratadie);
260 1 return byte($m);
261 }
262  
263 sub dt_day {
264 1 ▶ my $self = shift;
Subroutine dt_day: called CC=1 SCAR=0.0
Pod documented
265 1 my (undef, undef, $d) = _ratadie2ymd($self->double_ratadie);
266 1 return byte($d);
267 }
268  
269 sub dt_hour {
270 1 ▶ my $self = shift;
Subroutine dt_hour: called CC=1 SCAR=0.0
Pod documented
271 1 return PDL->new(byte((($self - ($self % 3_600_000_000)) / 3_600_000_000) % 24));
272 }
273  
274 sub dt_minute {
275 1 ▶ my $self = shift;
Subroutine dt_minute: called CC=1 SCAR=0.0
Pod documented
276 1 return PDL->new(byte((($self - ($self % 60_000_000)) / 60_000_000) % 60));
277 }
278  
279 sub dt_second {
280 1 ▶ my $self = shift;
Subroutine dt_second: called CC=1 SCAR=0.0
Pod documented
281 1 return PDL->new(byte((($self - ($self % 1_000_000)) / 1_000_000) % 60));
282 }
283  
284 sub dt_microsecond {
285 1 ▶ my $self = shift;
Subroutine dt_microsecond: called CC=1 SCAR=0.0
Pod documented
286 1 return PDL->new(long($self % 1_000_000));
287 }
288  
289 sub dt_day_of_week {
290 1 ▶ my $self = shift;
Subroutine dt_day_of_week: called CC=1 SCAR=0.0
Pod documented
291 1 my $days = ($self - ($self % 86_400_000_000)) / 86_400_000_000;
292 1 return PDL->new(byte(($days + 3) % 7) + 1); # 1..Mon, 7..Sun
293 }
294  
295 sub dt_day_of_year {
296 1 ▶ my $self = shift;
Subroutine dt_day_of_year: called CC=1 SCAR=0.0
Pod documented
297 1 my $rd1 = long(floor($self->double_ratadie));
298 1 my $rd2 = long(floor($self->dt_align('year')->double_ratadie));
299 1 return PDL->new(short, ($rd1 - $rd2 + 1));
300 }
301  
302 sub dt_add {
303 48 ▶ my $self = shift;
Subroutine dt_add: called CC=16 SCAR=27.9
Pod documented
304 48 ▶ if ($self->is_inplace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_inplace) { } 16 32 48
305 16 $self->set_inplace(0);
306 16 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 16 16 32
307 16 my ($unit, $num) = (shift, shift);
308 16 ▶ if ($unit eq 'month') {
Branch: 6 branches
BranchTrueFalseTotal
if ($unit eq 'month') { } 3 13 16
elsif ($unit eq 'quarter') { } 0 13 13
elsif ($unit eq 'year') { } 4 9 13
elsif ($unit eq 'millisecond') { } 1 8 9
elsif ($unit eq 'microsecond') { } 1 7 8
elsif (my $inc = $INC_SECONDS{$unit}) { } 7 0 7
309 3 $self += $self->_plus_delta_m($num);
310 }
311 elsif ($unit eq 'quarter') {
312 0 $self += $self->_plus_delta_m($num * 3);
313 }
314 elsif ($unit eq 'year') {
315 4 $self += $self->_plus_delta_m($num * 12);
316 }
317 elsif ($unit eq 'millisecond') {
318 1 $self += $num * 1000;
319 }
320 elsif ($unit eq 'microsecond') {
321 1 $self += $num;
322 }
323 elsif (my $inc = $INC_SECONDS{$unit}) { # week day hour minute second
324 7 my $add = longlong(floor($num * $inc * 1_000_000 + 0.5));
325 7 $self->inplace->plus($add, 0);
326 }
327 }
328 16 return $self;
329 }
330 else {
331 32 my $rv = $self->copy;
332 32 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 32 32 64
333 32 my ($unit, $num) = (shift, shift);
334 32 ▶ if ($unit eq 'month') {
Branch: 6 branches
BranchTrueFalseTotal
if ($unit eq 'month') { } 6 26 32
elsif ($unit eq 'quarter') { } 0 26 26
elsif ($unit eq 'year') { } 8 18 26
elsif ($unit eq 'millisecond') { } 2 16 18
elsif ($unit eq 'microsecond') { } 2 14 16
elsif (my $inc = $INC_SECONDS{$unit}) { } 14 0 14
335 6 $rv += $rv->_plus_delta_m($num);
336 }
337 elsif ($unit eq 'quarter') {
338 0 $rv += $rv->_plus_delta_m($num * 3);
339 }
340 elsif ($unit eq 'year') {
341 8 $rv += $rv->_plus_delta_m($num * 12);
342 }
343 elsif ($unit eq 'millisecond') {
344 2 $rv += $num * 1000;
345 }
346 elsif ($unit eq 'microsecond') {
347 2 $rv += $num;
348 }
349 elsif(my $inc = $INC_SECONDS{$unit}) { # week day hour minute second
350 14 $rv += longlong(floor($num * $inc * 1_000_000 + 0.5));
351 }
352 }
353 32 return $rv;
354 }
355 }
356  
357 sub dt_align {
358 24 ▶ my ($self, $unit, $up) = @_;
Subroutine dt_align: called CC=20 SCAR=31.9
Pod documented
359 24 ▶ if ($self->is_inplace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_inplace) { } 8 16 24
360 8 $self->set_inplace(0);
361 8 ▶ return $self unless defined $unit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $unit 0 8 8
362 8 ▶ if ($unit eq 'year') {
Branch: 5 branches
BranchTrueFalseTotal
if ($unit eq 'year') { } 1 7 8
elsif ($unit eq 'quarter') { } 0 7 7
elsif ($unit eq 'month') { } 1 6 7
elsif ($unit eq 'millisecond') { } 1 5 6
elsif (my $inc = $INC_SECONDS{$unit}) { } 5 0 5
363 1 $self->{PDL} = $self->_allign_myq(0, 1, 0, $up)->{PDL};
364 }
365 elsif ($unit eq 'quarter') {
366 0 $self->{PDL} = $self->_allign_myq(0, 0, 1, $up)->{PDL};
367 }
368 elsif ($unit eq 'month') {
369 1 $self->{PDL} = $self->_allign_myq(1, 0, 0, $up)->{PDL};
370 }
371 elsif ($unit eq 'millisecond') {
372 1 my $sub = $self % 1_000;
373 1 $self->inplace->minus($sub, 0);
374 }
375 elsif (my $inc = $INC_SECONDS{$unit}) { # week day hour minute second
376 5 ▶ my $sub = $unit eq 'week' ? ($self + 3 * 60 * 60 * 24 * 1_000_000) % ($inc * 1_000_000) : $self % ($inc * 1_000_000);
Branch: 1 branch
BranchTrueFalseTotal
$unit eq 'week' ? : 1 4 5
377 5 ▶ $sub -= 6 * 60 * 60 * 24 * 1_000_000 if $up && $unit eq 'week';
Branch: 1 branch
BranchTrueFalseTotal
if $up and $unit eq "week" 0 5 5
Condition33%1/3
$up and $unit eq "week"
!ll&&!rl&&r
500
MC/DC0%0/2
$up and $unit eq "week"
$up $unit eq "week"
Truth table33%1/3
A and B
A= $upB= $unit eq "week"
ABresult
0X0
100
111
378 5 $self->inplace->minus($sub, 0);
379 }
380 8 return $self;
381 }
382 else {
383 16 ▶ return unless defined $unit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $unit 0 16 16
384 16 ▶ if ($unit eq 'year') {
Branch: 5 branches
BranchTrueFalseTotal
if ($unit eq 'year') { } 3 13 16
elsif ($unit eq 'quarter') { } 0 13 13
elsif ($unit eq 'month') { } 2 11 13
elsif ($unit eq 'millisecond') { } 1 10 11
elsif (my $inc = $INC_SECONDS{$unit}) { } 10 0 10
385 3 return $self->_allign_myq(0, 1, 0, $up);
386 }
387 elsif ($unit eq 'quarter') {
388 0 return $self->_allign_myq(0, 0, 1, $up);
389 }
390 elsif ($unit eq 'month') {
391 2 return $self->_allign_myq(1, 0, 0, $up);
392 }
393 elsif ($unit eq 'millisecond') {
394 1 return $self - $self % 1_000;
395 }
396 elsif (my $inc = $INC_SECONDS{$unit}) { # week day hour minute second
397 10 ▶ my $sub = $unit eq 'week' ? ($self + 3 * 60 * 60 * 24 * 1_000_000) % ($inc * 1_000_000) : $self % ($inc * 1_000_000);
Branch: 1 branch
BranchTrueFalseTotal
$unit eq 'week' ? : 2 8 10
398 10 ▶ $sub -= 6 * 60 * 60 * 24 * 1_000_000 if $up && $unit eq 'week';
Branch: 1 branch
BranchTrueFalseTotal
if $up and $unit eq "week" 0 10 10
Condition33%1/3
$up and $unit eq "week"
!ll&&!rl&&r
1000
MC/DC0%0/2
$up and $unit eq "week"
$up $unit eq "week"
Truth table33%1/3
A and B
A= $upB= $unit eq "week"
ABresult
0X0
100
111
399 10 return $self - $sub;
400 }
401 }
402 }
403  
404 sub dt_at {
405 5 ▶ my $self = shift;
Subroutine dt_at: called CC=4 SCAR=15.2
Pod documented
406 5 ▶ my $fmt = looks_like_number($_[-1]) ? 'auto' : pop;
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_[-1]) ? : 5 0 5
407 5 my $v = PDL::Core::at_c($self, [@_]);
408 5 ▶ $fmt = $self->_autodetect_strftime_format if !$fmt || $fmt eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if not $fmt or $fmt eq "auto" 5 0 5
Condition33%1/3
not $fmt or $fmt eq "auto"
l!l&&r!l&&!r
050
MC/DC0%0/2
not $fmt or $fmt eq "auto"
not $fmt $fmt eq "auto"
Truth table33%1/3
not(A) or B
A= not $fmtB= $fmt eq "auto"
ABresult
1X1
011
000
409 5 return _jumboepoch_to_datetime($v, $fmt);
410 }
411  
412 sub dt_set {
413 4 ▶ my $self = shift;
Subroutine dt_set: called CC=1 SCAR=0.0
Pod documented
414 4 my $datetime = pop;
415 4 PDL::Core::set_c($self, [@_], _datetime_to_jumboepoch($datetime));
416 }
417  
418 sub dt_unpdl {
419 113 ▶ my ($self, $fmt) = @_;
Subroutine dt_unpdl: called CC=5 SCAR=16.1
Pod documented
420 113 ▶ $fmt = $self->_autodetect_strftime_format if !$fmt || $fmt eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if not $fmt or $fmt eq "auto" 70 43 113
Condition66%2/3
not $fmt or $fmt eq "auto"
l!l&&r!l&&!r
70043
MC/DC50%1/2
not $fmt or $fmt eq "auto"
not $fmt $fmt eq "auto"
Truth table66%2/3
not(A) or B
A= not $fmtB= $fmt eq "auto"
ABresult
1X1
011
000
421 113 ▶ if ($fmt eq 'epoch') {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmt eq 'epoch') { } 1 112 113
elsif ($fmt eq 'epoch_int') { } 1 111 112
422 1 return (double($self) / 1_000_000)->unpdl;
423 }
424 elsif ($fmt eq 'epoch_int') {
425 1 return longlong(($self - ($self % 1_000_000)) / 1_000_000)->unpdl;
426 }
427 else {
428 111 my $array = $self->unpdl;
429 111 _jumboepoch_to_datetime($array, $fmt, 1); # change $array inplace!
430 111 return $array;
431 }
432 }
433  
434 sub dt_diff {
435 0 ▶ my ($self, $unit) = @_;
Subroutine dt_diff: not called CC=9 SCAR=45.0
Pod documented
436 0 ▶ return PDL->new('BAD')->reshape(1) if $self->nelem == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->nelem == 1 0 0 0
437 0 my $rv = PDL->new(longlong, 'BAD')->glue(0, $self->slice("1:-1") - $self->slice("0:-2"));
438 0 ▶ return $rv unless $unit;
Branch: 1 branch
BranchTrueFalseTotal
unless $unit 0 0 0
439 0 ▶ return double($rv) / 604_800_000_000 if $unit eq 'week';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "week" 0 0 0
440 0 ▶ return double($rv) / 86_400_000_000 if $unit eq 'day';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "day" 0 0 0
441 0 ▶ return double($rv) / 3_600_000_000 if $unit eq 'hour';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "hour" 0 0 0
442 0 ▶ return double($rv) / 60_000_000 if $unit eq 'minute';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "minute" 0 0 0
443 0 ▶ return double($rv) / 1_000_000 if $unit eq 'second';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "second" 0 0 0
444 0 ▶ return double($rv) / 1_000 if $unit eq 'millisecond';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "millisecond" 0 0 0
445 0 croak "dt_diff: invalid unit '$unit'";
446 }
447  
448 sub dt_periodicity {
449 0 ▶ my $self = shift;
Subroutine dt_periodicity: not called CC=18 SCAR=58.3
Pod documented
450 0 ▶ return '' if !$self->is_increasing && !$self->is_decreasing;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_increasing or $self->is_decreasing 0 0 0
Condition0%0/3
$self->is_increasing or $self->is_decreasing
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->is_increasing or $self->is_decreasing
$self->is_increasing $self->is_decreasing
Truth table0%0/3
A or B
A= $self->is_increasingB= $self->is_decreasing
ABresult
1X1
011
000
451 0 my $freq = $self->qsort->dt_diff->median;
452 0 ▶ return '' if $freq eq 'BAD' || $freq < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $freq eq "BAD" or $freq < 0 0 0 0
Condition0%0/3
$freq eq "BAD" or $freq < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$freq eq "BAD" or $freq < 0
$freq eq "BAD" $freq < 0
Truth table0%0/3
A or B
A= $freq eq "BAD"B= $freq < 0
ABresult
1X1
011
000
453 0 ▶ if ($freq < 1_000 ) {
Branch: 10 branches
BranchTrueFalseTotal
if ($freq < 1000) { } 0 0 0
elsif ($freq < 1000000) { } 0 0 0
elsif ($freq < 60000000) { } 0 0 0
elsif ($freq < 3600000000) { } 0 0 0
elsif ($freq < 86400000000) { } 0 0 0
elsif ($freq == 86400000000) { } 0 0 0
elsif ($freq == 604800000000) { } 0 0 0
elsif ($freq >= 2419200000000 and $freq <= 2678400000000) { } 0 0 0
elsif ($freq >= 7776000000000 and $freq <= 7948800000000) { } 0 0 0
elsif ($freq >= 31536000000000 and $freq <= 31622400000000) { } 0 0 0
454 # $freq < 1 millisecond
455 0 return "microsecond";
456 }
457 elsif ($freq < 1_000_000 ) {
458 # 1 millisecond <= $freq < 1 second
459 0 return "millisecond";
460 }
461 elsif ($freq < 60_000_000 ) {
462 # 1 second <= $freq < 1 minute
463 0 return "second";
464 }
465 elsif ($freq < 3_600_000_000) {
466 # 1 minute <= $freq < 1 hour
467 0 return "minute";
468 }
469 elsif ($freq < 86_400_000_000) {
470 # 1 hour <= $freq < 24 hours
471 0 return "hour";
472 }
473 elsif ($freq == 86_400_000_000) {
474 # 24 hours
475 0 return "day";
476 }
477 elsif ($freq == 604_800_000_000) {
478 # 7 days
479 0 return "week";
480 }
481 ▶ elsif ($freq >= 2_419_200_000_000 && $freq <= 2_678_400_000_000 ) {
Condition0%0/3
$freq >= '2419200000000' and $freq <= '2678400000000'
!ll&&!rl&&r
000
MC/DC0%0/2
$freq >= '2419200000000' and $freq <= '2678400000000'
$freq >= '2419200000000' $freq <= '2678400000000'
Truth table0%0/3
A and B
A= $freq >= '2419200000000'B= $freq <= '2678400000000'
ABresult
0X0
100
111
482 # 28days <= $freq <= 31days
483 0 return "month";
484 }
485 ▶ elsif ($freq >= 7_776_000_000_000 && $freq <= 7_948_800_000_000 ) {
Condition0%0/3
$freq >= '7776000000000' and $freq <= '7948800000000'
!ll&&!rl&&r
000
MC/DC0%0/2
$freq >= '7776000000000' and $freq <= '7948800000000'
$freq >= '7776000000000' $freq <= '7948800000000'
Truth table0%0/3
A and B
A= $freq >= '7776000000000'B= $freq <= '7948800000000'
ABresult
0X0
100
111
486 # 90days <= $freq <= 92days
487 0 return "quarter";
488 }
489 ▶ elsif ($freq >= 31_536_000_000_000 && $freq <= 31_622_400_000_000 ) {
Condition0%0/3
$freq >= '31536000000000' and $freq <= '31622400000000'
!ll&&!rl&&r
000
MC/DC0%0/2
$freq >= '31536000000000' and $freq <= '31622400000000'
$freq >= '31536000000000' $freq <= '31622400000000'
Truth table0%0/3
A and B
A= $freq >= '31536000000000'B= $freq <= '31622400000000'
ABresult
0X0
100
111
490 # 365days <= $freq <= 366days
491 0 return "year";
492 }
493 0 return ''; # unknown
494 }
495  
496 sub dt_startpoints {
497 0 ▶ my ($self, $unit) = @_;
Subroutine dt_startpoints: not called CC=4 SCAR=30.0
Pod documented
498 0 ▶ croak "dt_startpoints: undefined unit" unless $unit;
Branch: 1 branch
BranchTrueFalseTotal
unless $unit 0 0 0
499 0 ▶ croak "dt_startpoints: 1D piddle required" unless $self->ndims == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ndims == 1 0 0 0
500 0 ▶ croak "dt_startpoints: input not increasing" unless $self->is_increasing;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_increasing 0 0 0
501 0 return indx(0)->append($self->dt_endpoints($unit)->slice("0:-2") + 1);
502 }
503  
504 sub dt_endpoints {
505 0 ▶ my ($self, $unit) = @_;
Subroutine dt_endpoints: not called CC=6 SCAR=37.4
Pod documented
506 0 ▶ croak "dt_endpoints: undefined unit" unless $unit;
Branch: 1 branch
BranchTrueFalseTotal
unless $unit 0 0 0
507 0 ▶ croak "dt_endpoints: 1D piddle required" unless $self->ndims == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ndims == 1 0 0 0
508 0 ▶ croak "dt_endpoints: input not increasing" unless $self->is_increasing;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_increasing 0 0 0
509 0 my $diff = $self->dt_align($unit)->dt_diff;
510 0 my $end = which($diff != 0) - 1;
511 0 ▶ if ($end->nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end->nelem == 0) { } 0 0 0
512 0 $end = indx([$self->nelem-1]);
513 }
514 else {
515 0 ▶ $end = $end->append($self->nelem-1) unless $end->at($end->nelem-1) == $end->nelem-1;
Branch: 1 branch
BranchTrueFalseTotal
unless $end->at($end->nelem - 1) == $end->nelem - 1 0 0 0
516 }
517 0 return indx($end);
518 }
519  
520 sub dt_slices {
521 0 ▶ my ($self, $unit) = @_;
Subroutine dt_slices: not called CC=5 SCAR=34.0
Pod documented
522 0 ▶ croak "dt_slices: undefined unit" unless $unit;
Branch: 1 branch
BranchTrueFalseTotal
unless $unit 0 0 0
523 0 ▶ croak "dt_slices: 1D piddle required" unless $self->ndims == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ndims == 1 0 0 0
524 0 ▶ croak "dt_slices: input not increasing" unless $self->is_increasing;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_increasing 0 0 0
525 0 my $end = $self->dt_endpoints($unit);
526 0 my $start = indx([0]);
527 0 ▶ $start = $start->append($end->slice("0:-2") + 1) if $end->nelem > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $end->nelem > 1 0 0 0
528 0 return $start->cat($end)->transpose;
529 }
530  
531 sub dt_nperiods {
532 0 ▶ my ($self, $unit) = @_;
Subroutine dt_nperiods: not called CC=2 SCAR=17.9
Pod documented
533 0 ▶ croak "dt_nperiods: undefined unit" unless $unit;
Branch: 1 branch
BranchTrueFalseTotal
unless $unit 0 0 0
534 0 return $self->dt_endpoints($unit)->nelem;
535 }
536  
537 sub is_increasing {
538 0 ▶ my ($self, $strictly) = @_;
Subroutine is_increasing: not called CC=2 SCAR=17.9
Pod documented
539 0 ▶ return !(which($self->dt_diff <= 0)->nelem > 0) if $strictly;
Branch: 1 branch
BranchTrueFalseTotal
if $strictly 0 0 0
540 0 return !(which($self->dt_diff < 0)->nelem > 0);
541 }
542  
543 sub is_decreasing {
544 0 ▶ my ($self, $strictly) = @_;
Subroutine is_decreasing: not called CC=2 SCAR=17.9
Pod documented
545 0 ▶ return !(which($self->dt_diff >= 0)->nelem > 0) if $strictly;
Branch: 1 branch
BranchTrueFalseTotal
if $strictly 0 0 0
546 0 return !(which($self->dt_diff > 0)->nelem > 0);
547 }
548  
549 sub is_uniq {
550 0 ▶ my $self = shift;
Subroutine is_uniq: not called CC=1 SCAR=6.9
Pod documented
551 0 my $diff = $self->qsort->dt_diff;
552 0 return !(which($diff == 0)->nelem > 0);
553 }
554  
555 sub is_regular {
556 0 ▶ my $self = shift;
Subroutine is_regular: not called CC=4 SCAR=30.0
Pod documented
557 0 my $dt = $self->dt_diff;
558 0 my $diff = $self->dt_diff->qsort;
559 0 my $min = $diff->min;
560 0 my $max = $diff->max;
561 0 ▶ return ($min ne "BAD") && ($max ne "BAD") && ($min == $max) && ($max > 0);
Condition0%0/9
$min ne 'BAD' && $max ne 'BAD' && $min == $max && $max > 0
!ll&&!rl&&r
000
$min ne "BAD" and $max ne "BAD" and $min == $max
!ll&&!rl&&r
000
$min ne "BAD" and $max ne "BAD"
!ll&&!rl&&r
000
MC/DC0%0/4
$min ne 'BAD' && $max ne 'BAD' && $min == $max && $max > 0
$min ne "BAD" $max ne "BAD" $min == $max $max > 0
Truth table0%0/5
A and B and C && D
A= $min ne "BAD"B= $max ne "BAD"C= $min == $maxD= $max > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
562 }
563  
564 ### private methods
565  
566 sub _stringify {
567 1 ▶ my $self = shift;
Subroutine _stringify: called CC=2 SCAR=7.0
568 1 ▶ my $data = $self->ndims > 0 ? $self->dt_unpdl : $self->dt_unpdl->[0];
Branch: 1 branch
BranchTrueFalseTotal
$self->ndims > 0 ? : 1 0 1
569 1 my $rv = _print_array($data, 0);
570 1 $rv =~ s/\n$//;
571 1 return $rv;
572 }
573  
574 sub _num_compare_gt {
575 0 ▶ my ($self, $other, $swap) = @_;
Subroutine _num_compare_gt: not called CC=3 SCAR=24.8
576 0 ▶ $other = PDL::DateTime->new_from_datetime($other) if !ref $other && !looks_like_number($other);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other or &looks_like_number($other) 0 0 0
Condition0%0/3
ref $other or &looks_like_number($other)
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $other or &looks_like_number($other)
ref $other &looks_like_number($other)
Truth table0%0/3
A or B
A= ref $otherB= &looks_like_number($other)
ABresult
1X1
011
000
577 0 PDL::gt($self, $other, $swap);
578 }
579  
580 sub _num_compare_lt {
581 0 ▶ my ($self, $other, $swap) = @_;
Subroutine _num_compare_lt: not called CC=3 SCAR=24.8
582 0 ▶ $other = PDL::DateTime->new_from_datetime($other) if !ref $other && !looks_like_number($other);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other or &looks_like_number($other) 0 0 0
Condition0%0/3
ref $other or &looks_like_number($other)
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $other or &looks_like_number($other)
ref $other &looks_like_number($other)
Truth table0%0/3
A or B
A= ref $otherB= &looks_like_number($other)
ABresult
1X1
011
000
583 0 PDL::lt($self, $other, $swap);
584 }
585  
586 sub _num_compare_ge {
587 0 ▶ my ($self, $other, $swap) = @_;
Subroutine _num_compare_ge: not called CC=3 SCAR=24.8
588 0 ▶ $other = PDL::DateTime->new_from_datetime($other) if !ref $other && !looks_like_number($other);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other or &looks_like_number($other) 0 0 0
Condition0%0/3
ref $other or &looks_like_number($other)
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $other or &looks_like_number($other)
ref $other &looks_like_number($other)
Truth table0%0/3
A or B
A= ref $otherB= &looks_like_number($other)
ABresult
1X1
011
000
589 0 PDL::ge($self, $other, $swap);
590 }
591  
592 sub _num_compare_le {
593 0 ▶ my ($self, $other, $swap) = @_;
Subroutine _num_compare_le: not called CC=3 SCAR=24.8
594 0 ▶ $other = PDL::DateTime->new_from_datetime($other) if !ref $other && !looks_like_number($other);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other or &looks_like_number($other) 0 0 0
Condition0%0/3
ref $other or &looks_like_number($other)
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $other or &looks_like_number($other)
ref $other &looks_like_number($other)
Truth table0%0/3
A or B
A= ref $otherB= &looks_like_number($other)
ABresult
1X1
011
000
595 0 PDL::le($self, $other, $swap);
596 }
597  
598 sub _num_compare_eq {
599 27 ▶ my ($self, $other, $swap) = @_;
Subroutine _num_compare_eq: called CC=3 SCAR=12.5
600 27 ▶ $other = PDL::DateTime->new_from_datetime($other) if !ref $other && !looks_like_number($other);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other or &looks_like_number($other) 0 27 27
Condition33%1/3
ref $other or &looks_like_number($other)
l!l&&r!l&&!r
2700
MC/DC0%0/2
ref $other or &looks_like_number($other)
ref $other &looks_like_number($other)
Truth table33%1/3
A or B
A= ref $otherB= &looks_like_number($other)
ABresult
1X1
011
000
601 27 PDL::eq($self, $other, $swap);
602 }
603  
604 sub _num_compare_ne {
605 266 ▶ my ($self, $other, $swap) = @_;
Subroutine _num_compare_ne: called CC=3 SCAR=12.5
606 266 ▶ $other = PDL::DateTime->new_from_datetime($other) if !ref $other && !looks_like_number($other);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other or &looks_like_number($other) 0 266 266
Condition33%1/3
ref $other or &looks_like_number($other)
l!l&&r!l&&!r
02660
MC/DC0%0/2
ref $other or &looks_like_number($other)
ref $other &looks_like_number($other)
Truth table33%1/3
A or B
A= ref $otherB= &looks_like_number($other)
ABresult
1X1
011
000
607 266 PDL::ne($self, $other, $swap);
608 }
609  
610 sub _autodetect_strftime_format {
611 75 ▶ my $self = shift;
Subroutine _autodetect_strftime_format: called CC=5 SCAR=16.1
612 75 ▶ if (which(($self % (24*60*60*1_000_000)) != 0)->nelem == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if (which($self % 86400000000 != 0)->nelem == 0) { } 8 67 75
elsif (which($self % 60000000 != 0)->nelem == 0) { } 4 63 67
elsif (which($self % 1000000 != 0)->nelem == 0) { } 2 61 63
elsif (which($self % 1000 != 0)->nelem == 0) { } 2 59 61
613 8 return "%Y-%m-%d";
614 }
615 elsif (which(($self % (60*1_000_000)) != 0)->nelem == 0) {
616 4 return "%Y-%m-%dT%H:%M";
617 }
618 elsif (which(($self % 1_000_000) != 0)->nelem == 0) {
619 2 return "%Y-%m-%dT%H:%M:%S";
620 }
621 elsif (which(($self % 1_000) != 0)->nelem == 0) {
622 2 return "%Y-%m-%dT%H:%M:%S.%3N";
623 }
624 else {
625 59 return "%Y-%m-%dT%H:%M:%S.%6N";
626 }
627 }
628  
629 sub _plus_delta_m {
630 21 ▶ my ($self, $delta_m) = @_;
Subroutine _plus_delta_m: called CC=1 SCAR=0.0
631 21 my $day_fraction = $self % 86_400_000_000;
632 21 my $rdate_bf = ($self - $day_fraction)->double_ratadie;
633 21 my ($y, $m, $d) = _ratadie2ymd($rdate_bf);
634 21 my $rdate_af = _ymd2ratadie($y, $m, $d, $delta_m);
635 21 my $rv = longlong($rdate_af - $rdate_bf) * 86_400_000_000;
636 21 return $rv;
637 }
638  
639 sub _allign_myq {
640 7 ▶ my ($self, $mflag, $yflag, $qflag, $up) = @_;
Subroutine _allign_myq: called CC=6 SCAR=19.1
641 7 my $rdate = $self->double_ratadie;
642 7 my ($y, $m, $d) = _ratadie2ymd($rdate);
643 7 ▶ $m .= $up ? 12 : 1 if $yflag;
Branch: 2 branches
BranchTrueFalseTotal
if $yflag 4 3 7
$up ? : 0 4 4
644 7 ▶ $m = $up ? $m+((3-$m)%3) : $m-(($m-1)%3) if $qflag;
Branch: 2 branches
BranchTrueFalseTotal
if $qflag 0 7 7
$up ? : 0 0 0
645 7 ▶ $d .= $up ? _days_in_month($y, $m) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$up ? : 0 7 7
646 7 $rdate = _ymd2ratadie($y, $m, $d);
647 7 return PDL::DateTime->new(longlong(floor($rdate) - 719163) * 86_400_000_000);
648 }
649  
650 ### public functions (used e.g. by PDL::IO::CSV)
651  
652 sub dt2ll {
653 0 ▶ eval {
Subroutine dt2ll: not called CC=1 SCAR=6.9
Pod undocumented
654 0 my $tm = Time::Moment->from_string(_fix_datetime_value(shift), lenient=>1);
655 0 $tm->epoch * 1_000_000 + $tm->microsecond;
656 };
657 }
658  
659 sub ll2dt {
660 0 ▶ my $v = shift;
Subroutine ll2dt: not called CC=2 SCAR=17.9
Pod undocumented
661 0 my $us = int($v % 1_000_000);
662 0 my $ts = int(($v - $us) / 1_000_000);
663 0 ▶ my $rv = eval { Time::Moment->from_epoch($ts, $us * 1000)->to_string(reduced=>1) } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rv = eval { do { "Time::Moment"->from_epoch($ts, $us * 1000)->to_string("reduced", 1) } } or return 0 0 0
664 0 $rv =~ s/(T00:00)?Z$//;
665 0 return $rv;
666 }
667  
668 ### private functions
669  
670 sub _dt2tm {
671 648 ▶ eval { Time::Moment->from_string(_fix_datetime_value(shift), lenient=>1) };
Subroutine _dt2tm: called CC=1 SCAR=0.0
672 }
673  
674 sub _ll2tm {
675 768 ▶ my $v = shift;
Subroutine _ll2tm: called CC=1 SCAR=0.0
676 768 my $us = int($v % 1_000_000);
677 768 my $ts = int(($v - $us) / 1_000_000);
678 768 eval { Time::Moment->from_epoch($ts, $us * 1000) };
679 }
680  
681 sub _print_array {
682 1 ▶ my ($val, $level) = @_;
Subroutine _print_array: called CC=5 SCAR=23.4
683 1 my $prefix = " " x $level;
684 1 ▶ if (ref $val eq 'ARRAY' && !ref $val->[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY' and not ref $val->[0]) { } 1 0 1
elsif (ref $val eq 'ARRAY') { } 0 0 0
Condition33%1/3
ref $val eq "ARRAY" and not ref $val->[0]
!ll&&!rl&&r
001
MC/DC0%0/2
ref $val eq "ARRAY" and not ref $val->[0]
ref $val eq "ARRAY" not ref $val->[0]
Truth table33%1/3
A and not(B)
A= ref $val eq "ARRAY"B= not ref $val->[0]
ABresult
0X0
100
111
685 1 return $prefix . join(" ", '[', @$val, ']') . "\n";
686 }
687 elsif (ref $val eq 'ARRAY') {
688 0 my $out = $prefix."[\n";
689 0 $out .= _print_array($_, $level + 1) for (@$val);
690 0 $out .= $prefix."]\n";
691 }
692 else {
693 0 return $prefix . $val . "\n";
694 }
695 }
696  
697 sub _fix_datetime_value {
698 648 ▶ my $v = shift;
Subroutine _fix_datetime_value: called CC=4 SCAR=13.9
699 # '2015-12-29' > '2015-12-29T00Z'
700 648 ▶ return $v."T00Z" if $v =~ /^\d\d\d\d-\d\d-\d\d$/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^\d\d\d\d-\d\d-\d\d$/ 1 647 648
701 # '2015-12-29 11:59' > '2015-12-29 11:59Z'
702 647 ▶ return $v."Z" if $v =~ /^\d\d\d\d-\d\d-\d\d[ T]\d\d:\d\d$/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^\d\d\d\d-\d\d-\d\d[ T]\d\d:\d\d$/ 0 647 647
703 # '2015-12-29 11:59:11' > '2015-12-29 11:59:11Z' or '2015-12-29 11:59:11.123' > '2015-12-29 11:59:11.123Z'
704 647 ▶ return $v."Z" if $v =~ /^\d\d\d\d-\d\d-\d\d[ T]\d\d:\d\d:\d\d(\.\d+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^\d\d\d\d-\d\d-\d\d[ T]\d\d:\d\d:\d\d(\.\d+)?$/ 589 58 647
705 58 return $v;
706 }
707  
708 sub _datetime_to_jumboepoch {
709 636 ▶ my ($dt, $inplace) = @_;
Subroutine _datetime_to_jumboepoch: called CC=14 SCAR=37.0
710 636 my $tm;
711 636 ▶ if (ref $dt eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dt eq 'ARRAY') { } 32 604 636
712 32 my @new;
713 32 for (@$dt) {
714 600 my $s = _datetime_to_jumboepoch($_, $inplace);
715 600 ▶ if ($inplace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inplace) { } 0 600 600
716 0 ▶ $_ = (ref $_ ? undef : $s) if ref $_ ne 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
if ref $_ ne "ARRAY" 0 0 0
ref $_ ? : 0 0 0
717 }
718 else {
719 600 push @new, $s;
720 }
721 }
722 32 ▶ return \@new if !$inplace;
Branch: 1 branch
BranchTrueFalseTotal
if not $inplace 32 0 32
723 }
724 else {
725 604 ▶ if (looks_like_number $dt) {
Branch: 4 branches
BranchTrueFalseTotal
if (&looks_like_number($dt)) { } 0 604 604
elsif (not ref $dt) { } 604 0 604
elsif (ref $dt eq 'DateTime' or ref $dt eq 'Time::Piece') { } 0 0 0
elsif (ref $dt eq 'Time::Moment') { } 0 0 0
726 0 return int POSIX::floor($dt * 1_000_000 + 0.5);
727 }
728 elsif (!ref $dt) {
729 604 ▶ $tm = ($dt eq 'now') ? Time::Moment->now_utc : _dt2tm($dt);
Branch: 1 branch
BranchTrueFalseTotal
$dt eq 'now' ? : 0 604 604
730 }
731 ▶ elsif (ref $dt eq 'DateTime' || ref $dt eq 'Time::Piece') {
Condition0%0/3
ref $dt eq "DateTime" or ref $dt eq "Time::Piece"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $dt eq "DateTime" or ref $dt eq "Time::Piece"
ref $dt eq "DateTime" ref $dt eq "Time::Piece"
Truth table0%0/3
A or B
A= ref $dt eq "DateTime"B= ref $dt eq "Time::Piece"
ABresult
1X1
011
000
732 0 $tm = eval { Time::Moment->from_object($dt) };
733 }
734 elsif (ref $dt eq 'Time::Moment') {
735 0 $tm = $dt;
736 }
737 604 ▶ return undef unless $tm;
Branch: 1 branch
BranchTrueFalseTotal
unless $tm 0 604 604
738 604 return int($tm->epoch * 1_000_000 + $tm->microsecond);
739 }
740 }
741  
742 sub _jumboepoch_to_datetime {
743 879 ▶ my ($v, $fmt, $inplace) = @_;
Subroutine _jumboepoch_to_datetime: called CC=10 SCAR=25.2
744 879 ▶ return 'BAD' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 879 879
745 879 ▶ if (ref $v eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $v eq 'ARRAY') { } 111 768 879
elsif (not ref $v) { } 768 0 768
746 111 my @new;
747 111 for (@$v) {
748 763 my $s = _jumboepoch_to_datetime($_, $fmt, $inplace);
749 763 ▶ if ($inplace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inplace) { } 763 0 763
750 763 ▶ $_ = $s if ref $_ ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ ne "ARRAY" 763 0 763
751 }
752 else {
753 0 push @new, $s;
754 }
755 }
756 111 ▶ return \@new if !$inplace;
Branch: 1 branch
BranchTrueFalseTotal
if not $inplace 0 111 111
757 }
758 elsif (!ref $v) {
759 768 my $tm = _ll2tm($v);
760 768 ▶ return 'BAD' unless defined $tm;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tm 0 768 768
761 768 ▶ if ($fmt eq 'Time::Moment') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmt eq 'Time::Moment') { } 0 768 768
762 0 return $tm;
763 }
764 else {
765 768 return $tm->strftime($fmt);
766 }
767 }
768 }
769  
770 5 my $DAYS_PER_400_YEARS = 146_097;
771 5 my $DAYS_PER_100_YEARS = 36_524;
772 5 my $DAYS_PER_4_YEARS = 1_461;
773 5 my $MAR_1_TO_DEC_31 = 306;
774  
775 sub _ymd2ratadie {
776 31 ▶ my ($y, $m, $d, $delta_m) = @_;
Subroutine _ymd2ratadie: called CC=2 SCAR=6.9
777 # based on Rata Die calculation from https://metacpan.org/source/DROLSKY/DateTime-1.10/lib/DateTime.xs#L151
778 # RD: 1 => 0001-01-01
779 # RD: 2 => 0001-01-02
780 # RD: 719163 => 1970-01-01
781 # RD: 730120 => 2000-01-01
782 # RD: 2434498 => 6666-06-06
783 # RD: 3652059 => 9999-12-31
784  
785 31 ▶ if (defined $delta_m) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $delta_m) 21 10 31
786 # handle months + years
787 21 $m->inplace->plus($delta_m - 1, 0);
788 21 my $extra_y = floor($m / 12);
789 21 $m->inplace->modulo(12, 0);
790 21 $m->inplace->plus(1, 0);
791 21 $y->inplace->plus($extra_y, 0);
792 # fix days
793 21 my $dec_by_one = ($d==31) * (($m==4) + ($m==6) + ($m==9) + ($m==11));
794 # 1800, 1900, 2100, 2200, 2300 - common; 2000, 2400 - leap
795 21 my $is_nonleap_yr = (($y % 4)!=0) + (($y % 100)==0) - (($y % 400)==0);
796 21 my $dec_nonleap_feb = ($m==2) * ($d>28) * $is_nonleap_yr * ($d-28);
797 21 my $dec_leap_feb = ($m==2) * ($d>29) * (1 - $is_nonleap_yr) * ($d-29);
798 21 $d->inplace->minus($dec_by_one + $dec_leap_feb + $dec_nonleap_feb, 0);
799 }
800  
801 31 my $rdate = double($d); # may contain day fractions
802 31 $rdate->setbadif(($y < 1) + ($y > 9999));
803 31 $rdate->setbadif(($m < 1) + ($m > 12));
804 31 $rdate->setbadif(($d < 1) + ($d >= 32)); # not 100% correct (max. can be 31.9999999)
805  
806 31 my $m2 = ($m <= 2);
807 31 $y -= $m2;
808 31 $m += $m2 * 12;
809  
810 31 $rdate += floor(($m * 367 - 1094) / 12);
811 31 $rdate += floor($y % 100 * $DAYS_PER_4_YEARS / 4);
812 31 $rdate += floor($y / 100) * $DAYS_PER_100_YEARS + floor($y / 400);
813 31 $rdate -= $MAR_1_TO_DEC_31;
814 31 return $rdate;
815 }
816  
817 sub _ratadie2ymd {
818 # based on Rata Die calculation from https://metacpan.org/source/DROLSKY/DateTime-1.10/lib/DateTime.xs#L82
819 33 ▶ my $rdate = shift;
Subroutine _ratadie2ymd: called CC=1 SCAR=0.0
820  
821 33 my $d = floor($rdate);
822 33 $d += $MAR_1_TO_DEC_31;
823  
824 33 my $c = floor((($d * 4) - 1) / $DAYS_PER_400_YEARS); # century
825 33 $d -= floor($c * $DAYS_PER_400_YEARS / 4);
826 33 my $y = floor((($d * 4) - 1) / $DAYS_PER_4_YEARS);
827 33 $d -= floor($y * $DAYS_PER_4_YEARS / 4);
828 33 my $m = floor((($d * 12) + 1093) / 367);
829 33 $d -= floor((($m * 367) - 1094) / 12);
830 33 $y += ($c * 100);
831  
832 33 my $m12 = ($m > 12);
833 33 $y += $m12;
834 33 $m -= $m12 * 12;
835  
836 33 return ($y, $m, $d);
837 }
838  
839 sub _is_non_leap_year {
840 0 ▶ my $y = shift;
Subroutine _is_non_leap_year: not called CC=1 SCAR=6.9
841 0 return (($y % 4)!=0) + (($y % 100)==0) - (($y % 400)==0);
842 }
843  
844 sub _days_in_year {
845 0 ▶ my $y = shift;
Subroutine _days_in_year: not called CC=1 SCAR=6.9
846 0 return 366 - _is_non_leap_year($y);
847 }
848  
849 sub _days_in_month {
850 0 ▶ my ($y, $m) = @_;
Subroutine _days_in_month: not called CC=1 SCAR=6.9
851 0 my $dec_simple = (2*($m==2) + ($m==4) + ($m==6) + ($m==9) + ($m==11));
852 0 my $dec_nonleap_feb = ($m==2) * _is_non_leap_year($y);
853 0 return 31 - $dec_simple - $dec_nonleap_feb;
854 }
855  
856 5 1;
↑ Summary