ICal/Duration.pm

filter: Statementstmt 95.6% 154 / 161 Branchbran 77.9% 53 / 68 Conditioncond 87.8% 29 / 33 MC/DCmcdc 77.7% 14 / 18 Subroutinesub 100% 21 / 21 Podpod 100% 11 / 11 totaltotal 90.3% 282 / 312 CC 50 SCAR 20.6
CC
50
Cov
90%
new
15.5
_set_from_components
13.0
as_ical
8.2
CRAP 7.9
« ICal.pm ↑ Summary
1 package Date::ICal::Duration;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use vars qw($VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 $VERSION = '2.'.(qw'$Rev: 682 $')[1];
9  
10 # Documentation {{{
11  
12 =encoding utf8
13  
14 =head1 NAME
15  
16 Date::ICal::Duration - durations in iCalendar format, for math purposes.
17  
18 =head1 VERSION
19  
20 $Revision: 682 $
21  
22 =head1 SYNOPSIS
23  
24 use Date::ICal::Duration;
25  
26 $d = Date::ICal::Duration->new( ical => '-P1W3DT2H3M45S' );
27  
28 $d = Date::ICal::Duration->new( weeks => 1,
29 days => 1,
30 hours => 6,
31 minutes => 15,
32 seconds => 45);
33  
34 # a one hour duration, without other components
35 $d = Date::ICal::Duration->new( seconds => "3600");
36  
37 # Read-only accessors:
38 $d->weeks;
39 $d->days;
40 $d->hours;
41 $d->minutes;
42 $d->seconds;
43 $d->sign;
44  
45 # TODO: Resolve sign() discussion from rk-devel and update synopsis.
46
47 $d->as_seconds (); # returns just seconds
48 $d->as_elements (); # returns a hash of elements, like the accessors above
49 $d->as_ical(); # returns an iCalendar duration string
50
51 =head1 DESCRIPTION
52  
53 This is a trivial class for representing duration objects, for doing math
54 in Date::ICal
55  
56 =head1 AUTHOR
57  
58 Rich Bowen, and the Reefknot team. Alas, Reefknot is no more. See
59 L<https://github.com/houseabsolute/DateTime.pm/wiki> or L<https://metacpan.org/> for more modern modules.
60  
61 Last touched by $Author: rbowen $
62  
63 =head1 METHODS
64  
65 Date::ICal::Duration has the following methods available:
66  
67 =head2 new
68  
69 A new Date::ICal::Duration object can be created with an iCalendar string :
70  
71 my $ical = Date::ICal::Duration->new ( ical => 'P3W2D' );
72 # 3 weeks, 2 days, positive direction
73 my $ical = Date::ICal::Duration->new ( ical => '-P6H3M30S' );
74 # 6 hours, 3 minutes, 30 seconds, negative direction
75
76 Or with a number of seconds:
77  
78 my $ical = Date::ICal::Duration->new ( seconds => "3600" );
79 # one hour positive
80  
81 Or, better still, create it with components
82  
83 my $date = Date::ICal::Duration->new (
84 weeks => 6,
85 days => 2,
86 hours => 7,
87 minutes => 15,
88 seconds => 47,
89 sign => "+"
90 );
91  
92 The sign defaults to "+", but "+" and "-" are legal values.
93
94 =cut
95  
96 #}}}
97  
98 #{{{ sub new
99  
100 11 ▶sub new {
Subroutine new: called CC=15 SCAR=27.4
Pod documented
101 11 my ($class, %args) = @_;
102 11 my $verified = {};
103 11 my $self = {};
104 11 bless $self, $class;
105  
106 11 my $seconds_only = 1; # keep track of whether we were given length in seconds only
107 11 ▶ $seconds_only = 0 unless (defined $args{'seconds'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"seconds"} 5 6 11
108  
109 # If one of the attributes is negative, then they all must be
110 # negative. Otherwise, we're not sure what this means.
111 11 foreach (qw(hours minutes seconds days weeks)) {
112 55 ▶ if (defined($args{$_}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$_}) 13 42 55
113 # make sure this argument is all digits, optional - sign
114 13 ▶ if ($args{$_} =~ m/-?[0-9]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$_} =~ /-?[0-9]+$/) { } 13 0 13
115 13 ▶ if ($args{$_} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$_} < 0) 6 7 13
116 6 $args{sign} = '-';
117 6 $args{$_} = abs($args{$_});
118 }
119 13 $verified->{$_} = $args{$_};
120 13 ▶ unless ($_ eq 'seconds') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ eq "seconds") 7 6 13
121 7 $seconds_only = 0;
122 }
123 } else {
124 0 carp ("Parameter $_ contains non-numeric value " . $args{$_} . "\n");
125 }
126 }
127 }
128  
129 11 ▶ if (defined ($args{sign}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"sign"}) 2 9 11
130  
131 # make sure this argument + or -
132 2 ▶ if ($args{sign} =~ m/[+-]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'sign'} =~ /[+-]/) { } 2 0 2
133 # if so, assign it
134 2 ▶ $self->{sign} = ($args{sign} eq "+") ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$args{'sign'} eq '+' ? : 0 2 2
135 2 ▶ $verified->{sign} = ($args{sign} eq "+") ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$args{'sign'} eq '+' ? : 0 2 2
136 } else {
137 carp ("Parameter sign contains a value other than + or - : "
138 0 . $args{sign} . "\n");
139 }
140
141 }
142  
143 # If a number is given, convert it to hours, minutes, and seconds,
144 # but *don't* extract days -- we want it to represent an absolute
145 # amount of time, regardless of timezone
146 11 ▶ if ($seconds_only) { # if we were given an integer time_t
Branch: 3 branches
BranchTrueFalseTotal
if ($seconds_only) { } 1 10 11
elsif (defined $args{'ical'}) { } 4 6 10
elsif (not $seconds_only) { } 6 0 6
147 1 $self->_set_from_seconds($args{'seconds'});
148 } elsif (defined ($args{'ical'}) ) {
149 # A standard duration string
150 #warn "setting from ical\n";
151 4 $self->_set_from_ical($args{'ical'});
152 } elsif (not $seconds_only) {
153 #warn "setting from components";
154 #use Data::Dumper; warn Dumper $verified;
155 6 $self->_set_from_components($verified);
156 }
157
158 11 ▶ return undef unless %args;
Branch: 1 branch
BranchTrueFalseTotal
unless %args 1 10 11
159
160 10 return $self;
161 }
162  
163 #}}}
164  
165 # Accessors {{{
166  
167 =head2 sign, weeks, days, hours, minutes, seconds
168  
169 Read-only accessors for the elements of the object.
170  
171 =cut
172  
173 #}}}
174  
175 # {{{ sub sign
176  
177 2 ▶sub sign {
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
178 2 my ($self) = @_;
179 2 return $self->{sign};
180 }
181  
182 #}}}
183  
184 # {{{ sub weeks
185  
186 5 ▶sub weeks {
Subroutine weeks: called CC=2 SCAR=6.9
Pod documented
187 5 my ($self) = @_;
188 5 my $w = ${$self->_wd}[0];
189 5 ▶ return unless $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $w 2 3 5
190 3 return $self->{sign} * $w;
191 }
192  
193 #}}}
194  
195 # {{{ sub days
196  
197 4 ▶sub days {
Subroutine days: called CC=2 SCAR=7.0
Pod documented
198 4 my ($self) = @_;
199 4 my $d = ${$self->_wd}[1];
200 4 ▶ return unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 4 4
201 4 return $self->{sign} * $d;
202  
203 } #}}}
204  
205 #{{{ sub hours
206  
207 4 ▶sub hours {
Subroutine hours: called CC=2 SCAR=7.0
Pod documented
208 4 my ($self) = @_;
209 4 my $h = ${$self->_hms}[0];
210 4 ▶ return unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 4 4
211 4 return $self->{sign} * $h;
212 }
213  
214 #}}}
215  
216 # {{{ sub minutes
217  
218 4 ▶sub minutes {
Subroutine minutes: called CC=2 SCAR=7.0
Pod documented
219 4 my ($self) = @_;
220 4 my $m = ${$self->_hms}[1];
221 4 ▶ return unless $m;
Branch: 1 branch
BranchTrueFalseTotal
unless $m 0 4 4
222 4 return $self->{sign} * $m;
223 }
224  
225 #}}}
226  
227 # {{{ sub seconds
228  
229 4 ▶sub seconds {
Subroutine seconds: called CC=2 SCAR=7.0
Pod documented
230 4 my ($self) = @_;
231 4 my $s = ${$self->_hms}[2];
232 4 ▶ return unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 4 4
233 4 return $self->{sign} * $s;
234 }
235  
236 #}}}
237  
238 # sub as_seconds {{{
239  
240 =head2 as_seconds
241  
242 Returns the duration in raw seconds.
243  
244 WARNING -- this folds in the number of days, assuming that they are always 86400
245 seconds long (which is not true twice a year in areas that honor daylight
246 savings time). If you're using this for date arithmetic, consider using the
247 I<add()> method from a L<Date::ICal> object, as this will behave better.
248 Otherwise, you might experience some error when working with times that are
249 specified in a time zone that observes daylight savings time.
250  
251  
252 =cut
253  
254 6 ▶sub as_seconds {
Subroutine as_seconds: called CC=4 SCAR=14.1
Pod documented
255 6 my ($self) = @_;
256  
257 6 ▶ my $nsecs = $self->{nsecs} || 0;
Condition50%1/2
$self->{'nsecs'} || 0
l!l
60
MC/DC0%0/1
$self->{'nsecs'} || 0
$self->{'nsecs'}
Truth table50%1/2
A
A= $self->{'nsecs'}
Aresult
11
00
258 6 ▶ my $ndays = $self->{ndays} || 0;
Condition100%2/2
$self->{'ndays'} || 0
l!l
33
MC/DC100%1/1
$self->{'ndays'} || 0
$self->{'ndays'}
Truth table100%2/2
A
A= $self->{'ndays'}
Aresult
11
00
259 6 ▶ my $sign = $self->{sign} || 1;
Condition50%1/2
$self->{'sign'} || 1
l!l
60
MC/DC0%0/1
$self->{'sign'} || 1
$self->{'sign'}
Truth table50%1/2
A
A= $self->{'sign'}
Aresult
11
00
260 6 return $sign*($nsecs+($ndays*24*60*60));
261 }
262  
263 #}}}
264  
265 # sub as_days {{{
266  
267 =head2 as_days
268  
269 $days = $duration->as_days;
270  
271 Returns the duration as a number of days. Not to be confused with the
272 C<days> method, this method returns the total number of days, rather
273 than mod'ing out the complete weeks. Thus, if we have a duration of 33
274 days, C<weeks> will return 4, C<days> will return 5, but C<as_days> will
275 return 33.
276  
277 Note that this is a lazy convenience function which is just weeks*7 +
278 days.
279  
280 =cut
281  
282 3 ▶sub as_days {
Subroutine as_days: called CC=1 SCAR=0.0
Pod documented
283 3 my ($self) = @_;
284 3 my $wd = $self->_wd;
285 3 return $self->{sign} * ( $wd->[0]*7 + $wd->[1] );
286 }# }}}
287  
288 #{{{ sub as_ical
289  
290 =head2 as_ical
291  
292 Return the duration in an iCalendar format value string (e.g., "PT2H0M0S")
293  
294 =cut
295  
296 7 ▶sub as_ical {
Subroutine as_ical: called CC=8 SCAR=21.0
Pod documented
297 7 my ($self) = @_;
298  
299 7 my $tpart = '';
300  
301 7 ▶ if (my $ar_hms = $self->_hms) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ar_hms = $self->_hms) 7 0 7
302 7 $tpart = sprintf('T%dH%dM%dS', @$ar_hms);
303 }
304  
305 7 my $ar_wd = $self->_wd();
306
307 7 my $dpart = '';
308 7 ▶ if (defined $ar_wd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ar_wd) 5 2 7
309 5 my ($weeks, $days) = @$ar_wd;
310 5 ▶ if ($weeks && $days) {
Branch: 2 branches
BranchTrueFalseTotal
if ($weeks and $days) { } 2 3 5
elsif ($weeks) { } 0 3 3
Condition66%2/3
$weeks and $days
!ll&&!rl&&r
302
MC/DC50%1/2
$weeks and $days
$weeks $days
Truth table66%2/3
A and B
A= $weeksB= $days
ABresult
0X0
100
111
311 2 $dpart = sprintf('%dW%dD', $weeks, $days);
312 } elsif ($weeks) { # (if days = 0)
313 0 $dpart = sprintf('%dW', $weeks);
314 } else {
315 3 $dpart = sprintf('%dD', $days);
316 }
317 }
318  
319 # put a sign in the return value if necessary
320 7 ▶ my $value = join('', (($self->{sign} < 0) ? '-' : ''),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'sign'} < 0 ? : 2 5 7
321 'P', $dpart, $tpart);
322  
323 # remove any zero components from the time string (-P10D0H -> -P10D)
324 7 $value =~ s/(?<=[^\d])0[WDHMS]//g;
325  
326 # return either the time value or PT0S (if the time value is zero).
327 7 ▶ return (($value !~ /PT?$/) ? $value : 'PT0S');
Branch: 1 branch
BranchTrueFalseTotal
!($value =~ /PT?$/) ? : 6 1 7
328 }
329  
330 #}}}
331  
332 #{{{ sub as_elements
333  
334 =head2 as_elements
335  
336 Returns the duration as a hashref of elements.
337  
338 =cut
339  
340 1 ▶sub as_elements {
Subroutine as_elements: called CC=1 SCAR=0.0
Pod documented
341 1 my ($self) = @_;
342
343 # get values for all the elements
344 1 my $wd = $self->_wd;
345 1 my $hms = $self->_hms;
346
347 my $return = {
348 sign => $self->{sign},
349 1 weeks => ${$wd}[0],
350 1 days => ${$wd}[1],
351 1 hours => ${$hms}[0],
352 1 minutes => ${$hms}[1],
353 1 seconds => ${$hms}[2],
354 };
355 1 return $return;
356 }
357  
358 #}}}
359  
360 # INTERNALS {{{
361  
362 =head1 INTERNALS
363  
364 head2 GENERAL MODEL
365  
366 Internally, we store 3 data values: a number of days, a number of seconds (anything
367 shorter than a day), and a sign (1 or -1). We are assuming that a day is 24 hours for
368 purposes of this module; yes, we know that's not completely accurate because of
369 daylight-savings-time switchovers, but it's mostly correct. Suggestions are welcome.
370  
371 NOTE: The methods below SHOULD NOT be relied on to stay the same in future versions.
372  
373 =head2 _set_from_ical ($self, $duration_string)
374  
375 Converts a RFC2445 DURATION format string to the internal storage format.
376  
377 =cut
378  
379 #}}}
380  
381 # sub _set_from_ical (internal) {{{
382  
383 4 ▶sub _set_from_ical {
Subroutine _set_from_ical: called CC=1 SCAR=0.0
384 4 my ($self, $str) = @_;
385  
386 4 my $parsed_values = _parse_ical_string($str);
387
388 4 return $self->_set_from_components($parsed_values);
389 } # }}}
390  
391 # sub _parse_ical_string (internal) {{{
392  
393 =head2 _parse_ical_string ($string)
394  
395 Regular expression for parsing iCalendar into usable values.
396  
397 =cut
398  
399 7 ▶sub _parse_ical_string {
Subroutine _parse_ical_string: called CC=4 SCAR=14.4
400 7 my ($str) = @_;
401
402 # RFC 2445 section 4.3.6
403 #
404 # dur-value = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
405 # dur-date = dur-day [dur-time]
406 # dur-time = "T" (dur-hour / dur-minute / dur-second)
407 # dur-week = 1*DIGIT "W"
408 # dur-hour = 1*DIGIT "H" [dur-minute]
409 # dur-minute = 1*DIGIT "M" [dur-second]
410 # dur-second = 1*DIGIT "S"
411 # dur-day = 1*DIGIT "D"
412  
413 7 my ($sign_str, $magic, $weeks, $days, $hours, $minutes, $seconds) =
414 $str =~ m{
415 ([\+\-])? (?# Sign)
416 (P) (?# 'P' for period? This is our magic character)
417 (?:
418 (?:(\d+)W)? (?# Weeks)
419 (?:(\d+)D)? (?# Days)
420 )?
421 (?:T (?# Time prefix)
422 (?:(\d+)H)? (?# Hours)
423 (?:(\d+)M)? (?# Minutes)
424 (?:(\d+)S)? (?# Seconds)
425 )?
426 }x;
427  
428 7 ▶ if (!defined($magic)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $magic) 0 7 7
429 0 carp "Invalid duration: $str";
430 0 return undef;
431 }
432  
433 # make sure the sign gets set, and turn it into an integer multiplier
434 7 ▶ $sign_str ||= "+";
Condition50%1/2
$sign_str ||= "+"
l!l
07
MC/DC0%0/1
$sign_str ||= "+"
$sign_str
Truth table50%1/2
A
A= $sign_str
Aresult
11
00
435 7 ▶ my $sign = ($sign_str eq "-") ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$sign_str eq '-' ? : 0 7 7
436
437 7 my $return = {};
438 7 $return->{'weeks'} = $weeks;
439 7 $return->{'days'} = $days;
440 7 $return->{'hours'} = $hours;
441 7 $return->{'minutes'} = $minutes;
442 7 $return->{'seconds'} = $seconds;
443 7 $return->{'sign'} = $sign;
444  
445 7 return $return;
446 } # }}}
447  
448 # sub _set_from_components (internal) {{{
449  
450 =head2 _set_from_components ($self, $hashref)
451  
452 Converts from a hashref to the internal storage format.
453 The hashref can contain elements "sign", "weeks", "days", "hours", "minutes", "seconds".
454  
455 =cut
456  
457 10 ▶sub _set_from_components {
Subroutine _set_from_components: called CC=13 SCAR=25.6
458 10 my ($self, $args) = @_;
459  
460 # Set up some easier-to-read variables
461 10 my ($sign, $weeks, $days, $hours, $minutes, $seconds);
462 10 $sign = $args->{'sign'};
463 10 $weeks = $args->{'weeks'};
464 10 $days = $args->{'days'};
465 10 $hours = $args->{'hours'};
466 10 $minutes = $args->{'minutes'};
467 10 $seconds = $args->{'seconds'};
468
469 10 ▶ $self->{sign} = (defined($sign) && $sign eq '-') ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $sign && $sign eq '-' ? : 2 8 10
Condition100%3/3
defined $sign and $sign eq "-"
!ll&&!rl&&r
442
MC/DC100%2/2
defined $sign and $sign eq "-"
defined $sign $sign eq "-"
Truth table100%3/3
A and B
A= defined $signB= $sign eq "-"
ABresult
0X0
100
111
470  
471 10 ▶ if (defined($weeks) or defined($days)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $weeks or defined $days) 7 3 10
Condition100%3/3
defined $weeks or defined $days
l!l&&r!l&&!r
343
MC/DC100%2/2
defined $weeks or defined $days
defined $weeks defined $days
Truth table100%3/3
A or B
A= defined $weeksB= defined $days
ABresult
1X1
011
000
472 7 ▶ $self->_wd([$weeks || 0, $days || 0]);
Condition100%4/4
$weeks || 0
l!l
34
$days || 0
l!l
61
MC/DC100%1/1
$weeks || 0
$weeks
MC/DC100%1/1
$days || 0
$days
Truth table100%2/2
A
A= $weeks
Aresult
11
00
Truth table100%2/2
A
A= $days
Aresult
11
00
473 }
474  
475 10 ▶ if (defined($hours) || defined($minutes) || defined($seconds)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hours or defined $minutes or defined $seconds) 9 1 10
Condition100%6/6
defined $hours or defined $minutes or defined $seconds
l!l&&r!l&&!r
631
defined $hours or defined $minutes
l!l&&r!l&&!r
514
MC/DC100%3/3
defined $hours or defined $minutes or defined $seconds
defined $hours defined $minutes defined $seconds
Truth table100%4/4
A or B or C
A= defined $hoursB= defined $minutesC= defined $seconds
ABCresult
1XX1
01X1
0011
0000
476 9 ▶ $self->_hms([$hours || 0, $minutes || 0, $seconds || 0]);
Condition100%6/6
$hours || 0
l!l
54
$minutes || 0
l!l
54
$seconds || 0
l!l
72
MC/DC100%1/1
$hours || 0
$hours
MC/DC100%1/1
$minutes || 0
$minutes
MC/DC100%1/1
$seconds || 0
$seconds
Truth table100%2/2
A
A= $hours
Aresult
11
00
Truth table100%2/2
A
A= $minutes
Aresult
11
00
Truth table100%2/2
A
A= $seconds
Aresult
11
00
477 }
478  
479 10 return $self;
480 } # }}}
481  
482 # sub _set_from_ical (internal) {{{
483  
484 =head2 _set_from_ical ($self, $num_seconds)
485  
486 Sets internal data storage properly if we were only given seconds as a parameter.
487  
488 =cut
489  
490 1 ▶sub _set_from_seconds {
Subroutine _set_from_seconds: called CC=2 SCAR=7.0
491 1 my ($self, $seconds) = @_;
492
493 1 ▶ $self->{sign} = (($seconds < 0) ? -1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$seconds < 0 ? : 0 1 1
494 # find the number of days, if any
495 1 my $ndays = int ($seconds / (24*60*60));
496 # now, how many hours/minutes/seconds are there, after
497 # days are taken out?
498 1 my $nsecs = $seconds % (24*60*60);
499 1 $self->{ndays} = abs($ndays);
500 1 $self->{nsecs} = abs($nsecs);
501  
502  
503 1 return $self;
504 } # }}}
505  
506 # sub _hms (internal) {{{
507  
508 =head2 $self->_hms();
509  
510 Return an arrayref to hours, minutes, and second components, or undef
511 if nsecs is undefined. If given an arrayref, computes the new nsecs value
512 for the duration.
513  
514 =cut
515  
516 29 ▶sub _hms {
Subroutine _hms: called CC=3 SCAR=11.2
517 29 my ($self, $hms_arrayref) = @_;
518  
519 29 ▶ if (defined($hms_arrayref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hms_arrayref) 9 20 29
520 9 my $new_sec_value = $hms_arrayref->[0]*3600 +
521 $hms_arrayref->[1]*60 + $hms_arrayref->[2];
522 9 $self->{nsecs} = ($new_sec_value);
523 }
524  
525 29 my $nsecs = $self->{nsecs};
526 29 ▶ if (defined($nsecs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nsecs) { } 29 0 29
527 29 my $hours = int($nsecs/3600);
528 29 my $minutes = int(($nsecs-$hours*3600)/60);
529 29 my $seconds = $nsecs % 60;
530 29 return [ $hours, $minutes, $seconds ];
531 } else {
532 0 print "returning undef\n";
533 0 return undef;
534 }
535 } # }}}
536  
537 # sub _wd (internal) {{{
538  
539 =head2 $self->_wd()
540  
541 Return an arrayref to weeks and day components, or undef if ndays
542 is undefined. If Given an arrayref, computs the new ndays value
543 for the duration.
544  
545 =cut
546  
547 27 ▶sub _wd {
Subroutine _wd: called CC=3 SCAR=11.0
548 27 my ($self, $wd_arrayref) = @_;
549  
550 #print "entering _wd\n";
551
552 27 ▶ if (defined($wd_arrayref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wd_arrayref) 7 20 27
553
554 7 my $new_ndays = $wd_arrayref->[0]*7 + $wd_arrayref->[1];
555 7 $self->{ndays} = $new_ndays;
556 }
557
558 #use Data::Dumper; print Dumper $self->{ndays};
559
560 27 ▶ if (defined(my $ndays= $self->{ndays})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ndays = $self->{'ndays'})) { } 25 2 27
561 25 my $weeks = int($ndays/7);
562 25 my $days = $ndays % 7;
563 25 return [ $weeks, $days ];
564 } else {
565 2 return undef;
566 }
567 } # }}}
568  
569 =head1 LICENSE AND COPYRIGHT
570  
571 © 2001-2022 Rich Bowen
572  
573 © 2022-2023 Michal Josef Špaček
574  
575 This library is free software; you can redistribute it and/or
576 modify it under the same terms as Perl itself.
577  
578 =cut
579  
580 12 1;
« ICal.pm ↑ Summary