blib/lib/JSCalendar/Duration.pm

filter: Statementstmt 97.5% 81 / 83 Branchbran 90.9% 60 / 66 Conditioncond 92.8% 13 / 14 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 93.8% 168 / 179 CC 39 SCAR 29.5
CC
39
Cov
94%
seconds_to_duration
24.1
duration_to_seconds
16.1
BEGIN
1.0
CRAP 19.1
↑ Summary
1 package JSCalendar::Duration 0.005;
2 # ABSTRACT: Convert seconds to JSCalendar durations and back
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our @EXPORT = qw(seconds_to_duration duration_to_seconds);
11  
12 33 ▶sub duration_to_seconds {
Subroutine duration_to_seconds: called CC=16 SCAR=27.8
Pod documented
13 33 my $input = shift;
14  
15 33 ▶ croak("Usage: duration_to_seconds(\$duration). (Extra args provided: @_)")
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 33 33
16 if @_;
17  
18 33 ▶ croak('Usage: duration_to_seconds($duration)')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 33 33
19 unless defined $input;
20  
21 # Let's get that out of the way
22 33 ▶ return '0' if $input eq 'P0D';
Branch: 1 branch
BranchTrueFalseTotal
if $input eq "P0D" 2 31 33
23  
24 31 my $toparse = $input;
25  
26 31 my $seconds = 0;
27  
28 31 ▶ if ($toparse =~ /\P{ASCII}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toparse =~ /\P{ASCII}/) 1 30 31
29 1 croak("Invalid duration '$input', must be entirely ASCII");
30 }
31  
32 30 ▶ unless ($toparse =~ s/^P//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($toparse =~ s/^P//) 0 30 30
33 0 croak("Invalid duration '$input', must start with 'P'");
34 }
35  
36 30 ▶ unless (length $toparse) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $toparse) 1 29 30
37 1 croak("Invalid duration '$input', no components after 'P'");
38 }
39  
40 29 ▶ if ($toparse =~ s/^(\d+)W\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toparse =~ s/^(\d+)W\z//) 1 28 29
41 # Weeks must appear on their own, no day or time component.
42 1 $seconds += (86400 * 7 * $1);
43 1 return $seconds;
44 }
45  
46 28 ▶ if ($toparse =~ s/^(\d+)D//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toparse =~ s/^(\d+)D//) 11 17 28
47 11 $seconds += (86400 * $1);
48 }
49  
50 28 ▶ return $seconds unless length $toparse;
Branch: 1 branch
BranchTrueFalseTotal
unless length $toparse 3 25 28
51  
52 25 ▶ unless ($toparse =~ s/^T//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($toparse =~ s/^T//) 1 24 25
53 1 croak("Invalid duration '$input', expected T here: '$toparse'");
54 }
55  
56 24 ▶ unless (length $toparse) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $toparse) 2 22 24
57 2 croak("Invalid duration '$input', no components after 'T'");
58 }
59  
60 22 ▶ if ($toparse =~ s/^(\d+)H//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toparse =~ s/^(\d+)H//) 6 16 22
61 6 $seconds += (3600 * $1);
62 }
63  
64 22 ▶ if ($toparse =~ s/^(\d+)M//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toparse =~ s/^(\d+)M//) 7 15 22
65 7 $seconds += (60 * $1);
66 }
67  
68 22 ▶ if ($toparse =~ s/^(\d+(?:\.\d+)?)S//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toparse =~ s/^(\d+(?:\.\d+)?)S//) 18 4 22
69 18 $seconds += $1;
70 }
71  
72 22 ▶ if (length $toparse) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $toparse) 2 20 22
73 2 croak("Invalid duration '$input': confused by '$toparse'");
74 }
75  
76 20 return $seconds;
77 }
78  
79 26 ▶sub seconds_to_duration {
Subroutine seconds_to_duration: called CC=24 SCAR=31.8
Pod documented
80 26 my $input = shift;
81  
82 26 ▶ croak("Usage: seconds_to_duration(\$seconds). (Extra args provided: @_)")
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 26 26
83 if @_;
84  
85 26 ▶ croak('Usage: seconds_to_duration($seconds)')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 26 26
86 unless defined $input;
87  
88 26 my $toparse = $input;
89  
90 26 my $dec;
91  
92 26 ▶ $dec = $1 if $toparse =~ s/\.(\d+)$//;
Branch: 1 branch
BranchTrueFalseTotal
if $toparse =~ s/\.(\d+)$// 9 17 26
93  
94 # .1 becomes "", we want 0 after
95 26 ▶ $toparse ||= 0;
Condition100%2/2
$toparse ||= 0
l!l
224
MC/DC100%1/1
$toparse ||= 0
$toparse
Truth table100%2/2
A
A= $toparse
Aresult
11
00
96  
97 26 ▶ if ($toparse && $toparse !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toparse and not $toparse =~ /^\d+$/) 0 26 26
Condition66%2/3
$toparse and not $toparse =~ /^\d+$/
!ll&&!rl&&r
4220
MC/DC0%0/2
$toparse and not $toparse =~ /^\d+$/
$toparse not $toparse =~ /^\d+$/
Truth table66%2/3
A and not(B)
A= $toparseB= not $toparse =~ /^\d+$/
ABresult
0X0
100
111
98 0 croak("Usage: seconds_to_duration(\$seconds). (Non-number value provided: '$input'");
99 }
100  
101 26 my ($durday, $durtime) = ("", "");
102  
103 26 my $days = 0;
104  
105 26 ▶ while ($toparse >= 86400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toparse >= 86400) 31 26 57
106 31 $days++;
107 31 $toparse -= 86400;
108 }
109  
110 26 ▶ if ($days && $days % 7 == 0 && $toparse == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($days and $days % 7 == 0 and $toparse == 0) 1 25 26
Condition100%6/6
$days and $days % 7 == 0 and $toparse == 0
!ll&&!rl&&r
2411
$days and $days % 7 == 0
!ll&&!rl&&r
1772
MC/DC100%3/3
$days and $days % 7 == 0 and $toparse == 0
$days $days % 7 == 0 $toparse == 0
Truth table100%4/4
A and B and C
A= $daysB= $days % 7 == 0C= $toparse == 0
ABCresult
0XX0
10X0
1100
1111
111 1 return 'P' . ($days/7) . "W";
112 }
113  
114 25 ▶ $durday = "${days}D" if $days;
Branch: 1 branch
BranchTrueFalseTotal
if $days 8 17 25
115  
116 25 my $hours = 0;
117  
118 25 ▶ while ($toparse >= 3600) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toparse >= 3600) 48 25 73
119 48 $hours++;
120 48 $toparse -= 3600;
121 }
122  
123 25 ▶ $durtime = "${hours}H" if $hours;
Branch: 1 branch
BranchTrueFalseTotal
if $hours 4 21 25
124  
125 25 my $minutes = 0;
126  
127 25 ▶ while ($toparse >= 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toparse >= 60) 239 25 264
128 239 $minutes++;
129 239 $toparse -= 60;
130 }
131  
132 25 ▶ $durtime .= "${minutes}M" if $minutes;
Branch: 1 branch
BranchTrueFalseTotal
if $minutes 7 18 25
133  
134 25 my $seconds = 0;
135  
136 25 ▶ while ($toparse >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toparse >= 1) 364 25 389
137 364 $seconds++;
138 364 $toparse -= 1;
139 }
140  
141 25 ▶ $durtime .= "${seconds}" if $seconds;
Branch: 1 branch
BranchTrueFalseTotal
if $seconds 16 9 25
142  
143 25 ▶ if ($dec) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dec) { } 7 18 25
elsif ($seconds) { } 11 7 18
144 7 ▶ $durtime .= $durtime ? ".${dec}S" : "0.${dec}S";
Branch: 1 branch
BranchTrueFalseTotal
$durtime ? : 5 2 7
145 } elsif ($seconds) {
146 11 $durtime .= "S";
147 }
148  
149 # P<zero>D
150 25 ▶ return "P0D" unless $durday || $durtime;
Branch: 1 branch
BranchTrueFalseTotal
unless $durday or $durtime 2 23 25
Condition100%3/3
$durday or $durtime
l!l&&r!l&&!r
8152
MC/DC100%2/2
$durday or $durtime
$durday $durtime
Truth table100%3/3
A or B
A= $durdayB= $durtime
ABresult
1X1
011
000
151  
152 23 ▶ $durtime = "T$durtime" if $durtime;
Branch: 1 branch
BranchTrueFalseTotal
if $durtime 20 3 23
153  
154 23 return "P" . $durday . $durtime;
155 }
156  
157 1 1;
158  
159 =pod
160  
161 =encoding UTF-8
162  
163 =head1 NAME
164  
165 JSCalendar::Duration - Convert seconds to JSCalendar durations and back
166  
167 =head1 VERSION
168  
169 version 0.005
170  
171 =head1 SYNOPSIS
172  
173 use JSCalendar::Duration qw(
174 seconds_to_duration
175 duration_to_seconds
176 );
177  
178 # 104403.1
179 my $seconds = duration_to_seconds("P1DT5H3.1S");
180  
181 # P1D
182 my $duration = seconds_to_duration('86400');
183  
184 =head1 DESCRIPTION
185  
186 This module converts between a duration of time as specified by seconds and
187 a JSCalendar duration (L<https://tools.ietf.org/html/draft-ietf-calext-jscalendar-00#section-3.2.3>).
188  
189 =head1 PERL VERSION
190  
191 This module should work on any version of perl still receiving updates from
192 the Perl 5 Porters. This means it should work on any version of perl
193 released in the last two to three years. (That is, if the most recently
194 released version is v5.40, then this module should work on both v5.40 and
195 v5.38.)
196  
197 Although it may work on older versions of perl, no guarantee is made that the
198 minimum required version will not be increased. The version may be increased
199 for any reason, and there is no promise that patches will be accepted to
200 lower the minimum required perl.
201  
202 =head1 EXPORTS
203  
204 =head2 seconds_to_duration
205  
206 my $duration = seconds_to_duration("86401.2");
207  
208 Converts seconds to a JSCalendar duration representation.
209  
210 =head2 duration_to_seconds
211  
212 my $seconds = duration_to_seconds("P1DT4H");
213  
214 Converts a JSCalendar duration to seconds.
215  
216 =head1 SEE ALSO
217  
218 =over 4
219  
220 =item L<https://tools.ietf.org/html/draft-ietf-calext-jscalendar-00#section-3.2.3>
221  
222 The JSCalendar duration spec.
223  
224 =back
225  
226 =head1 AUTHOR
227  
228 Matthew Horsfall <wolfsage@gmail.com>
229  
230 =head1 CONTRIBUTORS
231  
232 =for stopwords Mohammad S Anwar Ricardo Signes
233  
234 =over 4
235  
236 =item *
237  
238 Mohammad S Anwar <mohammad.anwar@yahoo.com>
239  
240 =item *
241  
242 Ricardo Signes <rjbs@semiotic.systems>
243  
244 =item *
245  
246 Ricardo Signes <rjbs@users.noreply.github.com>
247  
248 =back
249  
250 =head1 COPYRIGHT AND LICENSE
251  
252 This software is copyright (c) 2018 by Matthew Horsfall.
253  
254 This is free software; you can redistribute it and/or modify it under
255 the same terms as the Perl 5 programming language system itself.
256  
257 =cut
258  
259 __END__
↑ Summary