blib/lib/JSCalendar/Duration.pm

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