Quiq/Duration.pm

filter: Statementstmt 90.0% 118 / 131 Branchbran 83.3% 50 / 60 Conditioncond 64.0% 16 / 25 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 100% 15 / 15 Podpod 100% 8 / 8 totaltotal 83.9% 214 / 255 CC 42 SCAR 26.6
CC
42
Cov
85%
asShortString
28.6
secondsToString
12.2
stringToSeconds
9.0
CRAP 14.3
« Quiq/ExampleCode.pm ↑ Summary Quiq/TreeFormatter.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Duration - Rechnen und Konvertieren von Zeiträumen
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert eine Zeitdauer, die intern als
16 numerischer Wert (Sekunden mit Nachkommastellen) gespeichert wird.
17  
18 Als externe Repäsentation wird die Darstellung
19  
20 DdHhMmS.Xs
21  
22 verwendet, wobei
23  
24 D = Anzahl Tage
25 H = Anzahl Stunden
26 M = Anzahl Minuten
27 S = Anzahl Sekunden
28 X = Bruchteil einer Sekunde
29  
30 Es gelten folgende Eingenschaften:
31  
32 =over 2
33  
34 =item *
35  
36 führende Anteile bis auf Ss fehlen, wenn sie 0 sind
37  
38 =item *
39  
40 X fehlt, wenn 0
41  
42 =item *
43  
44 Ss erscheint immer, auch bei 0 Sekunden
45  
46 =item *
47  
48 die leere Zeichenkette ('') oder undef entspricht 0 Sekunden
49  
50 =item *
51  
52 der Sekundenanteil kann Nachkommastellen haben
53  
54 =back
55  
56 Bei der Instantiierung kann die Zeitdauer auch in Doppelpunkt-Notation
57 übergeben werden:
58  
59 D:H:M:S.X
60  
61 =cut
62  
63 # -----------------------------------------------------------------------------
64  
65 package Quiq::Duration;
66 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 270 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 270 our $VERSION = '1.240';
74  
75 270 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 270 ▶use Quiq::Math;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 # -----------------------------------------------------------------------------
79  
80 =head1 METHODS
81  
82 =head2 Konstruktor
83  
84 =head3 new() - Konstruktor
85  
86 =head4 Synopsis
87  
88 $dur = Quiq::Duration->new($sec);
89 $dur = Quiq::Duration->new($str);
90  
91 =head4 Description
92  
93 Instantiiere ein Zeitdauer-Objekt und liefere einen Referenz auf
94 dieses Objekt zurück. Die Zeitdauer kann als numerischer Wert $sec
95 oder als Zeichenkette $str angegeben werden. Die Zeichenkette
96 kann auch in Doppelpunkt-Notation (D:H:M:S.X) angegeben sein.
97  
98 =cut
99  
100 # -----------------------------------------------------------------------------
101  
102 82 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
103 82 my $class = shift;
104 82 ▶ my $sec = shift || 0;
Condition100%2/2
shift() || 0
l!l
7012
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
105  
106 # Schlägt unerwünschterweise bei Zahl mit Exponentialdarstellung zu
107 #
108 #if ($sec !~ /^[0-9.:dhms]+$/) {
109 # $class->throw(
110 # 'DURATION-00002: Illegal duration',
111 # Duration => $sec,
112 # );
113 #}
114  
115 82 ▶ if ($sec =~ tr/:dhms//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sec =~ tr/:dhms//) 12 70 82
116 12 $sec = $class->stringToSeconds($sec);
117 }
118  
119 82 return bless \$sec,$class;
120 }
121  
122 # -----------------------------------------------------------------------------
123  
124 =head2 Objektmethoden
125  
126 =head3 asSeconds() - Dauer in Sekunden
127  
128 =head4 Synopsis
129  
130 $sec = $dur->asSeconds;
131  
132 =head4 Description
133  
134 Liefere die Zeitdauer in Sekunden - sofern vorhanden,
135 mit Nachkommastellen.
136  
137 =cut
138  
139 # -----------------------------------------------------------------------------
140  
141 3 ▶sub asSeconds {
Subroutine asSeconds: called CC=1 SCAR=0.0
Pod documented
142 3 my $self = shift;
143 3 return $$self;
144 }
145  
146 # -----------------------------------------------------------------------------
147  
148 =head3 asString() - Dauer als Zeichenkette
149  
150 =head4 Synopsis
151  
152 $str = $dur->asString;
153 $str = $dur->asString($prec);
154  
155 =head4 Description
156  
157 Liefere die Zeitdauer als Zeichenkette in der Form DdHhMmS.Xs.
158  
159 =cut
160  
161 # -----------------------------------------------------------------------------
162  
163 1 ▶sub asString {
Subroutine asString: called CC=1 SCAR=0.0
Pod documented
164 1 my ($self,$prec) = @_;
165 1 return ref($self)->secondsToString($$self,$prec);
166 }
167  
168 # -----------------------------------------------------------------------------
169  
170 =head3 asShortString() - Dauer als kürzestmögliche Zeichenkette
171  
172 =head4 Synopsis
173  
174 $str = $dur->asShortString(@opt);
175  
176 =head4 Options
177  
178 =over 4
179  
180 =item -maxUnit => 'd'|'h'|'m'|'s' (Default: 'd')
181  
182 Größte dargestellte Einheit.
183  
184 =item -minUnit => 'd'|'h'|'m'|'s' (Default: 's')
185  
186 Kleinste dargestellte Einheit.
187  
188 =item -notBlank => $bool (Default: 0)
189  
190 Bei einer Zeit von 0 Sekunden wird ein Leerstring geliefert.
191 Ist diese Option gesetzt, erfolgt immer eine Ausgabe -
192 mit -minUnit als Einheit.
193  
194 =item -precision => $n (Default: 0)
195  
196 Anzahl der Sekunden-Nachkommastellen.
197  
198 =back
199  
200 =head4 Description
201  
202 Liefere die Zeitdauer als Zeichenkette der Form DdHhMmSs (per
203 Default, siehe Option -maxUnit), wobei alle Anteile, die 0 sind,
204 weggelassen werden, sowohl am Anfang als auch am Ende.
205  
206 =cut
207  
208 # -----------------------------------------------------------------------------
209  
210 9 ▶sub asShortString {
Subroutine asShortString: called CC=19 SCAR=33.5
Pod documented
211 9 my $self = shift;
212 # @_: @opt
213  
214 9 my $maxUnit = 'd';
215 9 my $minUnit = 's';
216 9 my $notBlank = 0;
217 9 my $precision = 0;
218  
219 9 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 2 9
220 7 Quiq::Option->extract(\@_,
221 -maxUnit => \$maxUnit,
222 -minUnit => \$minUnit,
223 -notBlank => \$notBlank,
224 -precision => \$precision,
225 );
226 }
227  
228 9 my @unit = qw/d h m s/;
229 9 my @arr = $self->asArray;
230  
231 9 ▶ if ($maxUnit =~ /^[hms]$/ && $arr[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxUnit =~ /^[hms]$/ and $arr[0]) 2 7 9
Condition66%2/3
$maxUnit =~ /^[hms]$/ and $arr[0]
!ll&&!rl&&r
702
MC/DC50%1/2
$maxUnit =~ /^[hms]$/ and $arr[0]
$maxUnit =~ /^[hms]$/ $arr[0]
Truth table66%2/3
A and B
A= $maxUnit =~ /^[hms]$/B= $arr[0]
ABresult
0X0
100
111
232 2 $arr[1] += $arr[0]*24;
233 2 $arr[0] = 0;
234 }
235 9 ▶ if ($maxUnit =~ /^[ms]$/ && $arr[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxUnit =~ /^[ms]$/ and $arr[1]) 0 9 9
Condition33%1/3
$maxUnit =~ /^[ms]$/ and $arr[1]
!ll&&!rl&&r
900
MC/DC0%0/2
$maxUnit =~ /^[ms]$/ and $arr[1]
$maxUnit =~ /^[ms]$/ $arr[1]
Truth table33%1/3
A and B
A= $maxUnit =~ /^[ms]$/B= $arr[1]
ABresult
0X0
100
111
236 0 $arr[2] += $arr[1]*60;
237 0 $arr[1] = 0;
238 }
239 9 ▶ if ($maxUnit eq 's' && $arr[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxUnit eq "s" and $arr[2]) 0 9 9
Condition33%1/3
$maxUnit eq "s" and $arr[2]
!ll&&!rl&&r
900
MC/DC0%0/2
$maxUnit eq "s" and $arr[2]
$maxUnit eq "s" $arr[2]
Truth table33%1/3
A and B
A= $maxUnit eq "s"B= $arr[2]
ABresult
0X0
100
111
240 0 $arr[3] += $arr[2]*60;
241 0 $arr[2] = 0;
242 }
243  
244 9 ▶ if ($minUnit =~ /^[dhm]$/ && $arr[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minUnit =~ /^[dhm]$/ and $arr[3]) 2 7 9
Condition100%3/3
$minUnit =~ /^[dhm]$/ and $arr[3]
!ll&&!rl&&r
522
MC/DC100%2/2
$minUnit =~ /^[dhm]$/ and $arr[3]
$minUnit =~ /^[dhm]$/ $arr[3]
Truth table100%3/3
A and B
A= $minUnit =~ /^[dhm]$/B= $arr[3]
ABresult
0X0
100
111
245 2 $arr[2] += Quiq::Math->roundToInt($arr[3]/60);
246 2 $arr[3] = 0;
247 }
248 9 ▶ if ($minUnit =~ /^[dh]$/ && $arr[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minUnit =~ /^[dh]$/ and $arr[2]) 0 9 9
Condition33%1/3
$minUnit =~ /^[dh]$/ and $arr[2]
!ll&&!rl&&r
900
MC/DC0%0/2
$minUnit =~ /^[dh]$/ and $arr[2]
$minUnit =~ /^[dh]$/ $arr[2]
Truth table33%1/3
A and B
A= $minUnit =~ /^[dh]$/B= $arr[2]
ABresult
0X0
100
111
249 0 $arr[1] += Quiq::Math->roundToInt($arr[2]/60);
250 0 $arr[2] = 0;
251 }
252 9 ▶ if ($minUnit eq 'd' && $arr[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minUnit eq "d" and $arr[1]) 0 9 9
Condition33%1/3
$minUnit eq "d" and $arr[1]
!ll&&!rl&&r
900
MC/DC0%0/2
$minUnit eq "d" and $arr[1]
$minUnit eq "d" $arr[1]
Truth table33%1/3
A and B
A= $minUnit eq "d"B= $arr[1]
ABresult
0X0
100
111
253 0 $arr[0] += Quiq::Math->roundToInt($arr[1]/24);
254 0 $arr[1] = 0;
255 }
256  
257 9 ▶ if ($precision) {
Branch: 1 branch
BranchTrueFalseTotal
if ($precision) 0 9 9
258 0 $arr[3] = sprintf '%.*f',$precision,$arr[3];
259 }
260  
261 9 my $str = '';
262 9 for my $i (0 .. 3) {
263 36 ▶ if ($arr[$i] != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr[$i] != 0) 6 30 36
264 6 $str .= $arr[$i].$unit[$i];
265 }
266 }
267 9 ▶ if ($str eq '' && $notBlank) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str eq "" and $notBlank) 2 7 9
Condition100%3/3
$str eq "" and $notBlank
!ll&&!rl&&r
522
MC/DC100%2/2
$str eq "" and $notBlank
$str eq "" $notBlank
Truth table100%3/3
A and B
A= $str eq ""B= $notBlank
ABresult
0X0
100
111
268 2 $str = "0$minUnit";
269 }
270  
271 9 return $str;
272 }
273  
274 # -----------------------------------------------------------------------------
275  
276 =head3 asArray() - Dauer als Array
277  
278 =head4 Synopsis
279  
280 @arr | $arr = $dur->asArray;
281  
282 =head4 Description
283  
284 Liefere die Dauer als Array mit den Komponenten
285 ($days,$hours,$minutes,$seconds).
286  
287 =cut
288  
289 # -----------------------------------------------------------------------------
290  
291 63 ▶sub asArray {
Subroutine asArray: called CC=3 SCAR=11.0
Pod documented
292 63 my $self = shift;
293  
294 63 my @arr;
295 63 my $sec = $$self;
296 63 for my $x (86400,3600,60) {
297 189 my $fac = int($sec/$x);
298 189 push @arr,$fac;
299 189 $sec -= $fac*$x;
300 }
301 63 push @arr,$sec;
302  
303 63 ▶ return wantarray? @arr: \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 63 0 63
304 }
305  
306 # -----------------------------------------------------------------------------
307  
308 =head3 asFFmpegString() - Dauer als Parameter für ffmpeg-Option -t
309  
310 =head4 Synopsis
311  
312 $str = $dur->asFFmpegString;
313  
314 =head4 Description
315  
316 Liefere Dauer in der Form wie sie ffmpeg bei der Option -t erwartet,
317 also im Format
318  
319 H:M:S.XXX
320  
321 =cut
322  
323 # -----------------------------------------------------------------------------
324  
325 6 ▶sub asFFmpegString {
Subroutine asFFmpegString: called CC=1 SCAR=0.0
Pod documented
326 6 my $self = shift;
327 6 my @arr = $self->asArray;
328 6 return sprintf '%d:%d:%.3f',$arr[1],$arr[2],$arr[3];
329 }
330  
331 # -----------------------------------------------------------------------------
332  
333 =head3 stringToSeconds() - Wandele Zeitdauer-Angabe in Sekunden
334  
335 =head4 Synopsis
336  
337 $sec = $this->stringToSeconds($str);
338  
339 =head4 Description
340  
341 Wandele Zeichenkette zur Bezeichnung einer Zeitdauer in die Anzahl
342 Sekunden.
343  
344 =head4 Examples
345  
346 Zeitdauer-Zeichenkette bestehend aus Tagen, Stunden, Mintuten, Sekunden:
347  
348 $sec = Quiq::Duration->stringToSeconds('152d5h25m3.457s');
349 # 13152303.457
350  
351 Dasselbe mit Doppelpunkt-Notation:
352  
353 $sec = Quiq::Duration->stringToSeconds('152:5:25:3.457');
354 # 13152303.457
355  
356 =cut
357  
358 # -----------------------------------------------------------------------------
359  
360 26 ▶sub stringToSeconds {
Subroutine stringToSeconds: called CC=9 SCAR=22.0
Pod documented
361 26 my ($class,$str) = @_;
362  
363 26 ▶ if ($str =~ tr/://) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ tr/://) 7 19 26
364 # Repräsentation D:H:M:S.X nach DdHhMmS.Xs wandeln
365  
366 7 my @unit = qw/s m h d/;
367 7 my @arr = reverse split /:/,$str;
368 7 ▶ for (my $i = 0; $i < @arr; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @arr) 22 7 29
369 22 $arr[$i] .= $unit[$i];
370 }
371 7 $str = join '',reverse @arr;
372 }
373  
374 26 my $sec = 0;
375 26 my @arr = split /([dhms])/,$str;
376 26 ▶ for (my $i = 0; $i < @arr; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @arr) 59 26 85
377 59 my $x = $arr[$i];
378 59 ▶ my $unit = $arr[$i+1] || 's';
Condition100%2/2
$arr[$i + 1] || 's'
l!l
563
MC/DC100%1/1
$arr[$i + 1] || 's'
$arr[$i + 1]
Truth table100%2/2
A
A= $arr[$i + 1]
Aresult
11
00
379 59 ▶ if ($unit eq 'd') {
Branch: 4 branches
BranchTrueFalseTotal
if ($unit eq 'd') { } 9 50 59
elsif ($unit eq 'h') { } 11 39 50
elsif ($unit eq 'm') { } 15 24 39
elsif ($unit eq 's') { } 24 0 24
380 9 $sec += $x*86400;
381 }
382 elsif ($unit eq 'h') {
383 11 $sec += $x*3600;
384 }
385 elsif ($unit eq 'm') {
386 15 $sec += $x*60;
387 }
388 elsif ($unit eq 's') {
389 24 $sec += $x;
390 }
391 else {
392 0 $class->throw(
393 'DURATION-00001: Unbekannter Anteil einer Dauer',
394 Unit => $unit,
395 Value => $x,
396 );
397 }
398 }
399  
400 26 return $sec;
401 }
402  
403 # -----------------------------------------------------------------------------
404  
405 =head3 secondsToString() - Wandele Sekunden in Zeitdauer-Zeichenkette
406  
407 =head4 Synopsis
408  
409 $str = $this->secondsToString($sec,@opt);
410  
411 =head4 Arguments
412  
413 =over 4
414  
415 =item $sec
416  
417 Anzahl Sekunden, ggf. mit Nachkommastellen.
418  
419 =back
420  
421 =head4 Options
422  
423 =over 4
424  
425 =item $prec (Default: 0)
426  
427 Anzahl der Nachkommastellen bei den Sekunden. Ist kein Wert angegeben,
428 wird auf ganze Sekunden gerundet.
429  
430 =item $unit (Default: undef)
431  
432 Liefere String fester Breite ab Einheit $unit.
433  
434 =back
435  
436 =head4 Description
437  
438 Wandele Anzahl Sekunden in eine Zeichenkette zur Bezeichnung einer
439 Zeitdauer.
440  
441 =cut
442  
443 # -----------------------------------------------------------------------------
444  
445 44 ▶sub secondsToString {
Subroutine secondsToString: called CC=12 SCAR=25.0
Pod documented
446 44 my $class = shift;
447 44 my $sec = shift;
448 # @_: $prec -and/or- $unit (Reihenfolge egal)
449  
450 # Negative Sekundenzahl
451  
452 44 my $minusSign = 0;
453 44 ▶ if ($sec < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sec < 0) 0 44 44
454 0 $sec = abs $sec;
455 0 $minusSign = 1;
456 }
457  
458 # Optionen
459  
460 44 my $unit = '';
461 44 my $prec = 0;
462 44 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 44 50
463 6 ▶ if (!defined $_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_[0]) { } 0 6 6
elsif ($_[0] =~ /^\d+$/) { } 3 3 6
464 # undef übergehen wir
465 0 shift;
466 }
467 elsif ($_[0] =~ /^\d+$/) {
468 3 $prec = shift;
469 }
470 else {
471 3 $unit = shift;
472 }
473 }
474  
475 # Operation ausführen
476  
477 44 my @unit = qw/d h m s/;
478 44 my @arr = $class->new($sec)->asArray;
479  
480 44 my $str = '';
481 44 my $started = 0;
482 44 my $append = 0;
483 44 ▶ for (my $i = 0; $i <= 2; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 2) 132 44 176
484 132 ▶ if ($arr[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr[$i]) 12 120 132
485 12 $started = 1;
486 }
487 132 ▶ if ($unit eq $unit[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq $unit[$i]) 3 129 132
488 3 $append = 1;
489 }
490 132 ▶ if ($started) {
Branch: 1 branch
BranchTrueFalseTotal
if ($started) 12 120 132
491 12 ▶ if ($append) {
Branch: 1 branch
BranchTrueFalseTotal
if ($append) { } 2 10 12
492 2 $str .= sprintf '%02d%s',$arr[$i],$unit[$i];
493 }
494 else {
495 10 $str .= sprintf '%d%s',$arr[$i],$unit[$i];
496 }
497 }
498 }
499 44 $str =~ s/^0//;
500
501  
502 # Sekundenanteil immer liefern
503  
504 44 ▶ if ($unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit) { } 3 41 44
505 3 $str .= sprintf "%02.*f%s",$prec,$arr[3],$unit[3];
506 3 $str =~ s/^0(\d)/$1/; # etwaig führende 0 entfernen
507 }
508 else {
509 41 $str .= sprintf "%.*f%s",$prec,$arr[3],$unit[3];
510 # 0-Angaben vom Ende her entfernen
511 # FIXME: nicht nur Sekunden, sondern auch Minuten etc. entfernen,
512 # wenn sie 0 sind. Besseren Ansatz wählen.
513 # $str =~ s/(\D)0s$/$1/;
514 }
515  
516 44 ▶ return $minusSign? "-$str": $str;
Branch: 1 branch
BranchTrueFalseTotal
$minusSign ? : 0 44 44
517 }
518  
519 # -----------------------------------------------------------------------------
520  
521 =head1 VERSION
522  
523 1.240
524  
525 =head1 AUTHOR
526  
527 Frank Seitz, L<http://fseitz.de/>
528  
529 =head1 COPYRIGHT
530  
531 Copyright (C) 2026 Frank Seitz
532  
533 =head1 LICENSE
534  
535 This code is free software; you can redistribute it and/or modify
536 it under the same terms as Perl itself.
537  
538 =cut
539  
540 # -----------------------------------------------------------------------------
541  
542 270 1;
543  
544 # eof
« Quiq/ExampleCode.pm ↑ Summary Quiq/TreeFormatter.pm »