Quiq/Epoch.pm

filter: Statementstmt 74.5% 76 / 102 Branchbran 60.7% 17 / 28 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 81.8% 18 / 22 Podpod 100% 13 / 13 totaltotal 74.8% 128 / 171 CC 17 SCAR 21.7
CC
17
Cov
72%
new
24.3
as
6.3
asIso
2.1
CRAP 8.8
« Quiq/Html/Widget/Hidden.pm ↑ Summary Quiq/Html/List.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Epoch - Ein Zeitpunkt
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 GLOSSARY
14  
15 =over 4
16  
17 =item Epoch-Wert
18  
19 Anzahl der Sekunden seit 1.1.1970, 0 Uhr UTC in hoher Auflösung,
20 also mit Nachkommastellen.
21  
22 =item ISO-Zeitangabe
23  
24 Zeitangabe in der Darstellung C<YYYY-MM-DD HH:MI:SS.X>.
25  
26 =back
27  
28 =head1 DESCRIPTION
29  
30 Ein Objekt der Klasse repräsentiert einen Zeitpunkt. Die Klasse
31 implementiert Operationen auf einem solchen Zeitpunkt. Der
32 Zeitpunkt ist hochauflösend, umfasst also auch Sekundenbruchteile.
33  
34 =cut
35  
36 # -----------------------------------------------------------------------------
37  
38 package Quiq::Epoch;
39 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 270 our $VERSION = '1.240';
46  
47 270 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 270 ▶use Time::Local ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49 270 ▶use Quiq::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 270 ▶use Time::Zone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51 270 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # -----------------------------------------------------------------------------
54  
55 =head1 METHODS
56  
57 =head2 Konstruktor
58  
59 =head3 new() - Konstruktor
60  
61 =head4 Synopsis
62  
63 $t = $class->new;
64 $t = $class->new($epoch);
65 $t = $class->new($iso);
66 $t = $class->new('start-of-month');
67 $t = $class->new('start-of-previous-month');
68 $t = $class->new('start-of-next-month');
69  
70 =head4 Description
71  
72 Instantiiere ein Zeitpunkt-Objekt für Epoch-Wert $epoch bzw.
73 ISO-Zeitangabe $iso, letztere interpretiert in der lokalen
74 Zeitzone, und liefere dieses Objekt zurück. Ist kein Argument
75 angegeben, wird der aktuelle Zeitpunkt genommen.
76  
77 =cut
78  
79 # -----------------------------------------------------------------------------
80  
81 1464 ▶sub new {
Subroutine new: called CC=11 SCAR=31.9
Pod documented
82 1464 my $class = shift;
83 1464 ▶ my $epoch = shift // scalar(Time::HiRes::gettimeofday);
Condition100%2/2
shift() // scalar &Time::HiRes::gettimeofday()
l!l
14622
MC/DC100%1/1
shift() // scalar &Time::HiRes::gettimeofday()
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
84  
85 1464 ▶ if ($epoch eq 'start-of-month') {
Branch: 4 branches
BranchTrueFalseTotal
if ($epoch eq 'start-of-month') { } 0 1464 1464
elsif ($epoch eq 'start-of-next-month') { } 0 1464 1464
elsif ($epoch eq 'start-of-previous-month') { } 0 1464 1464
elsif (not $epoch =~ /^[\d.]+$/) { } 1452 12 1464
86 0 my (undef,undef,undef,undef,$m,$y) = localtime;
87 0 $epoch = Time::Local::timelocal(0,0,0,1,$m,$y);
88 }
89 elsif ($epoch eq 'start-of-next-month') {
90 0 my (undef,undef,undef,undef,$m,$y) = localtime;
91 0 ▶ if ($m == 11) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m == 11) { } 0 0 0
92 0 $m = 1;
93 0 $y++;
94 }
95 else {
96 0 $m++;
97 }
98 0 $epoch = Time::Local::timelocal(0,0,0,1,$m,$y);
99 }
100 elsif ($epoch eq 'start-of-previous-month') {
101 0 my (undef,undef,undef,undef,$m,$y) = localtime;
102 0 ▶ if ($m == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m == 0) { } 0 0 0
103 0 $m = 11;
104 0 $y--;
105 }
106 else {
107 0 $m--;
108 }
109 0 $epoch = Time::Local::timelocal(0,0,0,1,$m,$y);
110 }
111 elsif ($epoch !~ /^[\d.]+$/) {
112 # ISO Zeitangabe
113  
114 1452 my $x;
115 1452 ▶ if ($epoch =~ s/(\.\d+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($epoch =~ s/(\.\d+)//) 1 1451 1452
116 1 $x = $1;
117 }
118  
119 1452 ▶ if (length($epoch) == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $epoch == 10) 0 1452 1452
120 0 $epoch .= ' 00:00:00';
121 }
122  
123 1452 my @arr = reverse split /\D+/,$epoch;
124 1452 $arr[4]--;
125 1452 $epoch = Time::Local::timelocal(@arr);
126 1452 ▶ if ($x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x) 1 1451 1452
127 1 $epoch .= $x;
128 }
129 }
130  
131 1464 return bless \$epoch,$class;
132 }
133  
134 # -----------------------------------------------------------------------------
135  
136 =head2 Zeitkomponenten
137  
138 =head3 dayOfWeek() - Wochentagsnummer
139  
140 =head4 Synopsis
141  
142 $i = $t->dayOfWeek;
143  
144 =head4 Returns
145  
146 Integer
147  
148 =head4 Description
149  
150 Liefere Wochentagsnummer im Bereich 0-6, 0 = Sonntag.
151  
152 =cut
153  
154 # -----------------------------------------------------------------------------
155  
156 0 ▶sub dayOfWeek {
Subroutine dayOfWeek: not called CC=1 SCAR=6.9
Pod documented
157 0 my $self = shift;
158 0 return (localtime $$self)[6];
159 }
160  
161 # -----------------------------------------------------------------------------
162  
163 =head3 dayAbbr() - Abgekürzter Wochentagsname
164  
165 =head4 Synopsis
166  
167 $abbr = $ti->dayAbbr;
168  
169 =head4 Returns
170  
171 String
172  
173 =head4 Description
174  
175 Liefere abgekürzten Wochentagsnamen (So, Mo, Di, Mi, Do, Fr, Sa).
176  
177 =cut
178  
179 # -----------------------------------------------------------------------------
180  
181 270 our @DayAbbr = qw(So Mo Di Mi Do Fr Sa);
182  
183 0 ▶sub dayAbbr {
Subroutine dayAbbr: not called CC=1 SCAR=6.9
Pod documented
184 0 my $self = shift;
185 0 return $DayAbbr[$self->dayOfWeek];
186 }
187  
188 # -----------------------------------------------------------------------------
189  
190 =head3 dayName() - Wochentagsname
191  
192 =head4 Synopsis
193  
194 $name = $ti->dayName;
195  
196 =head4 Returns
197  
198 String
199  
200 =head4 Description
201  
202 Liefere Wochentagsname (Sonntag, Montag, Dienstag, Mittwoch, Donnerstag,
203 Freitag, Samstag).
204  
205 =cut
206  
207 # -----------------------------------------------------------------------------
208  
209 270 our @DayName = qw(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag);
210  
211 0 ▶sub dayName {
Subroutine dayName: not called CC=1 SCAR=6.9
Pod documented
212 0 my $self = shift;
213 0 return $DayName[$self->dayOfWeek];
214 }
215  
216 # -----------------------------------------------------------------------------
217  
218 =head3 year() - Jahr
219  
220 =head4 Synopsis
221  
222 $year = $t->year;
223  
224 =head4 Returns
225  
226 Integer
227  
228 =head4 Description
229  
230 Liefere (vierstellige) Jahreszahl.
231  
232 =cut
233  
234 # -----------------------------------------------------------------------------
235  
236 1 ▶sub year {
Subroutine year: called CC=1 SCAR=0.0
Pod documented
237 1 my $self = shift;
238 1 return (localtime $$self)[5]+1900;
239 }
240  
241 # -----------------------------------------------------------------------------
242  
243 =head3 month() - Monatsnummer
244  
245 =head4 Synopsis
246  
247 $month = $t->month;
248  
249 =head4 Returns
250  
251 Integer
252  
253 =head4 Description
254  
255 Liefere die ein- oder zweistellige Monatsnummer (1 .. 12).
256  
257 =cut
258  
259 # -----------------------------------------------------------------------------
260  
261 1 ▶sub month {
Subroutine month: called CC=1 SCAR=0.0
Pod documented
262 1 my $self = shift;
263 1 return (localtime $$self)[4]+1;
264 }
265  
266 # -----------------------------------------------------------------------------
267  
268 =head2 Zeit-Arithmetik
269  
270 =head3 minus() - Verschiebe Zeitpunkt in Vergangenheit
271  
272 =head4 Synopsis
273  
274 $t = $t->minus($duration);
275  
276 =head4 Arguments
277  
278 =over 4
279  
280 =item $duration
281  
282 Dauer, um die der Zeitpunkt in die Vergangenheit verschoben wird.
283 Die Dauer wird wie beim Konstruktor von Quiq::Duration angegeben.
284  
285 =back
286  
287 =head4 Returns
288  
289 Geändertes Epoch-Objekt (für Method-Chaining)
290  
291 =head4 Description
292  
293 Verschiebe den Zeitpunkt um Dauer $duration in die Vergangenheit.
294  
295 =cut
296  
297 # -----------------------------------------------------------------------------
298  
299 1 ▶sub minus {
Subroutine minus: called CC=1 SCAR=0.0
Pod documented
300 1 my ($self,$duration) = @_;
301 1 $$self -= Quiq::Duration->new($duration)->asSeconds;
302 1 return $self;
303 }
304  
305 # -----------------------------------------------------------------------------
306  
307 =head3 plus() - Verschiebe Zeitpunkt in Zukunft
308  
309 =head4 Synopsis
310  
311 $t = $t->plus($duration);
312  
313 =head4 Arguments
314  
315 =over 4
316  
317 =item $duration
318  
319 Dauer, um die der Zeitpunkt in die Zukunft verschoben wird. Die Dauer
320 wird wie beim Konstruktor von Quiq::Duration angegeben.
321  
322 =back
323  
324 =head4 Returns
325  
326 Geändertes Epoch-Objekt (für Method-Chaining)
327  
328 =head4 Description
329  
330 Verschiebe den Zeitpunkt um Dauer $duration in die Zukunft.
331  
332 =cut
333  
334 # -----------------------------------------------------------------------------
335  
336 1 ▶sub plus {
Subroutine plus: called CC=1 SCAR=0.0
Pod documented
337 1 my ($self,$duration) = @_;
338 1 $$self += Quiq::Duration->new($duration)->asSeconds;
339 1 return $self;
340 }
341  
342 # -----------------------------------------------------------------------------
343  
344 =head3 tzOffset() - Zeit-Offset der lokalen Zeitzone
345  
346 =head4 Synopsis
347  
348 $s = $this->tzOffset;
349  
350 =head4 Returns
351  
352 Anzahl Sekunden (Integer)
353  
354 =head4 Description
355  
356 Ermittele den aktuellen Offset der lokalen Zeitzone gegenüber UTC
357 in Sekunden und liefere diesen zurück.
358  
359 =head4 Example
360  
361 Quiq::Epoch->tzOffset; # MEST
362 ==>
363 7200
364  
365 (in Zeitzone MESZ)
366  
367 =cut
368  
369 # -----------------------------------------------------------------------------
370  
371 0 ▶sub tzOffset {
Subroutine tzOffset: not called CC=1 SCAR=6.9
Pod documented
372 0 my $this = shift;
373 0 return Time::Zone::tz_local_offset;
374 }
375  
376 # -----------------------------------------------------------------------------
377  
378 =head2 Externe Repräsentation
379  
380 =head3 epoch() - Liefere Epoch-Wert
381  
382 =head4 Synopsis
383  
384 $epoch = $t->epoch;
385  
386 =head4 Description
387  
388 Liefere den Epoch-Wert des Zeitpunkts.
389  
390 =head4 Example
391  
392 Quiq::Epoch->new->epoch;
393 ==>
394 1464342621.73231
395  
396 =cut
397  
398 # -----------------------------------------------------------------------------
399  
400 1441 ▶sub epoch {
Subroutine epoch: called CC=1 SCAR=0.0
Pod documented
401 1441 return ${(shift)}
402 }
403  
404 # -----------------------------------------------------------------------------
405  
406 =head3 localtime() - Zeitkomponenten in lokaler Zeit
407  
408 =head4 Synopsis
409  
410 ($s,$mi,$h,$d,$m,$y) = $t->localtime;
411  
412 =head4 Description
413  
414 Liefere die Zeitkomponenten Sekunden, Minuten, Stunden, Tag, Monat, Jahr
415 in lokaler Zeit. Im Unterschied zu localtime() aus dem Perl Core sind
416 Monat ($m) und Jahr (y) "richtig" wiedergegeben. d.h die Komponente $m
417 muss nicht um 1 erhöht und die Komponente $y muss nicht um 1900
418 erhöht werden.
419  
420 =head4 Example
421  
422 Quiq::Epoch->new(1559466751)->localtime;
423 ==>
424 (31,12,11,2,6,2019) # 2019-06-02 11:12:31
425  
426 (in Zeitzone MESZ)
427  
428 =cut
429  
430 # -----------------------------------------------------------------------------
431  
432 13 ▶sub localtime {
Subroutine localtime: called CC=1 SCAR=0.0
Pod documented
433 13 my $self = shift;
434  
435 13 my @arr = CORE::localtime $$self;
436 13 $arr[4]++;
437 13 $arr[5] += 1900;
438  
439 13 return @arr;
440 }
441  
442 # -----------------------------------------------------------------------------
443  
444 =head3 as() - Erzeuge externe Darstellung
445  
446 =head4 Synopsis
447  
448 $str = $t->as($fmt);
449  
450 =head4 Arguments
451  
452 =over 4
453  
454 =item $fmt
455  
456 Formatangabe. Folgende Formate sind definiert:
457  
458 =over 4
459  
460 =item YYYY-MM-DD
461  
462 Datum in ISO-Darstellung.
463  
464 =item YYYY-MM-DD HH:MI:SS
465  
466 Zeit in ISO-Darstellung.
467  
468 =item YYYY-MM-DD HH:MI:SS.XXX
469  
470 Zeit in ISO-Darstellung mit Nachkommastellen. Die Anzahl der X
471 gibt die Anzahl der Nachkommastellen an (in obiger Angabe drei).
472  
473 =back
474  
475 =back
476  
477 =head4 Returns
478  
479 Zeit-Darstellung (String)
480  
481 =head4 Description
482  
483 Liefere eine externe Darstellung des Zeitpunkts gemäß Formatangabe $fmt.
484 Der Zeitpunkt wird in der lokalen Zeitzone interpretiert.
485  
486 =head4 Example
487  
488 Quiq::Epoch->new->as('YYYY-MM-DD HH:MI:SS');
489 =>
490 2016-05-27 11:50:21
491  
492 =cut
493  
494 # -----------------------------------------------------------------------------
495  
496 4 ▶sub as {
Subroutine as: called CC=6 SCAR=18.4
Pod documented
497 4 my ($self,$fmt) = @_;
498  
499 4 my ($strFmt,$n);
500 4 ▶ if ($fmt eq 'YYYY-MM-DD HH:MI:SS') {
Branch: 3 branches
BranchTrueFalseTotal
if ($fmt eq 'YYYY-MM-DD HH:MI:SS') { } 3 1 4
elsif ($fmt eq 'YYYY-MM-DD') { } 0 1 1
elsif ($fmt =~ /^YYYY-MM-DD HH:MI:SS\.(X+)$/) { } 1 0 1
501 3 $strFmt = '%Y-%m-%d %H:%M:%S';
502 }
503 elsif ($fmt eq 'YYYY-MM-DD') {
504 0 $strFmt = '%Y-%m-%d';
505 }
506 elsif ($fmt =~ /^YYYY-MM-DD HH:MI:SS\.(X+)$/) {
507 1 $strFmt = '%Y-%m-%d %H:%M:%S';
508 1 $n = length($1);
509 }
510 else {
511 0 $self->throw(
512 'EPOCH-00001: Unknown time format',
513 Format => $fmt,
514 );
515 }
516
517 4 my $str = POSIX::strftime($strFmt,CORE::localtime $$self);
518 4 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 1 3 4
519 # Mit Nachkommastellen
520  
521 1 my ($x) = $$self =~ /\.(\d+)/;
522 1 ▶ $x //= '000000';
Condition50%1/2
$x //= "000000"
l!l
10
MC/DC0%0/1
$x //= "000000"
$x
Truth table50%1/2
A
A= $x
Aresult
11
00
523 1 $str .= '.'.substr $x,0,$n;
524 }
525  
526 4 return $str;
527 }
528  
529 # -----------------------------------------------------------------------------
530  
531 =head3 asIso() - Erzeuge ISO-Darstellung
532  
533 =head4 Synopsis
534  
535 $str = $t->asIso;
536 $str = $t->asIso($x);
537  
538 =head4 Arguments
539  
540 =over 4
541  
542 =item $x (Default: 0)
543  
544 Anzahl der Nachkommastellen.
545  
546 =back
547  
548 =head4 Returns
549  
550 Zeit-Darstellung (String)
551  
552 =cut
553  
554 # -----------------------------------------------------------------------------
555  
556 2 ▶sub asIso {
Subroutine asIso: called CC=2 SCAR=7.4
Pod documented
557 2 my ($self,$x) = @_;
558  
559 2 my $fmt = 'YYYY-MM-DD HH:MI:SS';
560 2 ▶ if ($x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x) 0 2 2
561 0 $fmt .= '.'.('X' x $x);
562 }
563  
564 2 return $self->as($fmt);
565 }
566  
567 # -----------------------------------------------------------------------------
568  
569 =head1 VERSION
570  
571 1.240
572  
573 =head1 AUTHOR
574  
575 Frank Seitz, L<http://fseitz.de/>
576  
577 =head1 COPYRIGHT
578  
579 Copyright (C) 2026 Frank Seitz
580  
581 =head1 LICENSE
582  
583 This code is free software; you can redistribute it and/or modify
584 it under the same terms as Perl itself.
585  
586 =cut
587  
588 # -----------------------------------------------------------------------------
589  
590 270 1;
591  
592 # eof
« Quiq/Html/Widget/Hidden.pm ↑ Summary Quiq/Html/List.pm »