blib/lib/PDL/DateTime.pm

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