Quiq/Time.pm

filter: Statementstmt 82.7% 196 / 237 Branchbran 69.3% 61 / 88 Conditioncond 71.1% 79 / 111 MC/DCmcdc 31.2% 15 / 48 Subroutinesub 76.4% 39 / 51 Podpod 100% 43 / 43 totaltotal 74.9% 433 / 578 CC 86 SCAR 32.1
CC
86
Cov
77%
set
65.7
setTime
14.0
truncate
8.6
CRAP 24.7
« Quiq/Object.pm ↑ Summary Quiq/AsciiTable.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Time - Klasse zur Repräsentation von Datum und Uhrzeit
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse Quiq::Time repräsentiert eine Zeitangabe,
16 bestehend aus den Komponenten Jahr (Y), Monat (M), Tag (D),
17 Stunde (h), Minute (m) und Sekunde (s). Die Klasse stellt Methoden
18 zur Manipulation der Zeitangabe zur Verfügung.
19  
20 Die Zeitangabe ist keiner bestimmten Zeitzone zugeordnet, alle Tage
21 haben 24 Stunden (keine Sommerzeit- und Winterzeit-Umschaltung) und
22 alle Tage haben genau 86400 Sekunden (keine Schaltsekunden).
23  
24 =cut
25  
26 # -----------------------------------------------------------------------------
27  
28 package Quiq::Time;
29 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 270 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 270 our $VERSION = '1.240';
37  
38 270 ▶use Time::Local ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 270 ▶use Quiq::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # -----------------------------------------------------------------------------
42  
43 =head1 METHODS
44  
45 =head2 Konstruktor
46  
47 =head3 new() - Instantiiere Zeitobjekt
48  
49 =head4 Synopsis
50  
51 $ti = $class->new($year,$month,$day,$hour,$minute,$second);
52 $ti = $class->new($year,$month,$day,$hour,$minute);
53 $ti = $class->new($year,$month,$day,$hour);
54 $ti = $class->new($year,$month,$day);
55 $ti = $class->new($year,$month);
56 $ti = $class->new($year);
57 $ti = $class->new(local=>$epoch);
58 $ti = $class->new(utc=>$epoch);
59 $ti = $class->new(dmy=>'D M Y');
60 $ti = $class->new(dmyhms=>'D M Y H M S');
61 $ti = $class->new(ymd=>'Y M D');
62 $ti = $class->new(ymdhm=>'Y M D');
63 $ti = $class->new(ymdhms=>'Y M D H M S');
64 $ti = $class->new(parse=>'D.M.Y ...'|'M/D/Y ...'|'Y-M-D ...');
65 $ti = $class->new;
66  
67 =head4 Description
68  
69 Instantiiere Zeitobjekt, setze die Zeitkomponenten auf die
70 angegebenen Werte und liefere das Objekt zurück.
71  
72 Aufrufargumente siehe $ti->set().
73  
74 =cut
75  
76 # -----------------------------------------------------------------------------
77  
78 120 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
79 120 my $class = shift;
80  
81 120 my $self = bless [],$class;
82 120 $self->set(@_);
83  
84 116 return $self;
85 }
86  
87 # -----------------------------------------------------------------------------
88  
89 =head3 copy() - Kopiere Zeitobjekt
90  
91 =head4 Synopsis
92  
93 $ti2 = $ti->copy;
94  
95 =head4 Description
96  
97 Kopiere Zeitobjekt und liefere die Kopie zurück.
98  
99 =cut
100  
101 # -----------------------------------------------------------------------------
102  
103 1 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
104 1 my $self = shift;
105 1 return bless [@$self],ref($self);
106 }
107  
108 # -----------------------------------------------------------------------------
109  
110 =head3 asArray() - Liefere Zeit als Array
111  
112 =head4 Synopsis
113  
114 ($year,$month,$day,$hour,$minute,$second) = $ti->asArray;
115  
116 =cut
117  
118 # -----------------------------------------------------------------------------
119  
120 1 ▶sub asArray {
Subroutine asArray: called CC=1 SCAR=0.0
Pod documented
121 1 my $self = shift;
122 1 return @$self;
123 }
124  
125 # -----------------------------------------------------------------------------
126  
127 =head2 Zeit/Datum setzen
128  
129 =head3 set() - Setze Zeit neu
130  
131 =head4 Synopsis
132  
133 $ti = $class->set($year,$month,$day,$hour,$minute,$second);
134 $ti = $class->set($year,$month,$day,$hour,$minute);
135 $ti = $class->set($year,$month,$day,$hour);
136 $ti = $class->set($year,$month,$day);
137 $ti = $class->set($year,$month);
138 $ti = $class->set($year);
139 $ti = $class->set(local=>$epoch);
140 $ti = $class->set(utc=>$epoch);
141 $ti = $class->set(dmy=>'D M Y');
142 $ti = $class->set(dmyhm=>'D M Y H M');
143 $ti = $class->set(dmyhms=>'D M Y H M S');
144 $ti = $class->set(ymd=>'Y M D');
145 $ti = $class->set(ymdhm=>'Y M D');
146 $ti = $class->set(ymdhms=>'Y M D H M S');
147 $ti = $class->set(parse=>'D.M.Y ...'|'M/D/Y ...'|'Y-M-D ...');
148 $ti = $class->set;
149  
150 =head4 Description
151  
152 Setze die Zeit gemäß der angegebenen Zeitkomponenten und liefere
153 das Zeitobjekt zurück.
154  
155 Alle nicht angegebenen Komponenten werden auf ihren kleinsten Wert
156 initialisiert (1 bei bei Monat und Tag, 0 bei Stunde, Minute,
157 Sekunde).
158  
159 Ein Aufruf ohne Argument setzt das Objekt auf den Beginn der Epoche
160 (1.1.1970 0 Uhr).
161  
162 Bei den Formaten dmy, dmyhms, ... sind beliebige Trennzeichen
163 zwischen den einzelnen Zahlen erlaubt.
164  
165 =cut
166  
167 # -----------------------------------------------------------------------------
168  
169 145 ▶sub set {
Subroutine set: called CC=45 SCAR=41.9
Pod documented
170 145 my $self = shift;
171  
172 145 ▶ if (!@_ || $_[0] eq 'utc' || $_[0] eq 'local') {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ or $_[0] eq "utc" or $_[0] eq "local") 21 124 145
Condition83%5/6
not @_ or $_[0] eq "utc" or $_[0] eq "local"
l!l&&r!l&&!r
210124
not @_ or $_[0] eq "utc"
l!l&&r!l&&!r
219124
MC/DC66%2/3
not @_ or $_[0] eq "utc" or $_[0] eq "local"
not @_ $_[0] eq "utc" $_[0] eq "local"
Truth table75%3/4
not(A) or B or C
A= not @_B= $_[0] eq "utc"C= $_[0] eq "local"
ABCresult
1XX1
01X1
0011
0000
173 21 ▶ my $zone = shift || 'utc';
Condition100%2/2
shift() || 'utc'
l!l
192
MC/DC100%1/1
shift() || 'utc'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
174 21 ▶ my $t = shift || 0;
Condition100%2/2
shift() || 0
l!l
156
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
175  
176 21 ▶ my @t = $zone eq 'utc'? gmtime($t): localtime($t);
Branch: 1 branch
BranchTrueFalseTotal
$zone eq 'utc' ? : 21 0 21
177 21 $#t = 5;
178 21 $t[4]++;
179 21 $t[5] += 1900;
180  
181 21 @$self = reverse @t;
182  
183 21 return $self;
184 }
185  
186 124 my ($y,$mo,$d,$h,$mi,$s);
187  
188 124 ▶ if ($_[0] eq 'parse') {
Branch: 6 branches
BranchTrueFalseTotal
if ($_[0] eq 'parse') { } 7 117 124
elsif ($_[0] eq 'ymd') { } 0 117 117
elsif ($_[0] eq 'ymdhm') { } 0 117 117
elsif ($_[0] eq 'ymdhms') { } 1 116 117
elsif ($_[0] eq 'dmy') { } 1 115 116
elsif ($_[0] eq 'dmyhms') { } 3 112 115
189 7 $_[1] =~ /^\d+(.)/;
190 7 ▶ if ($1 eq '.') {
Branch: 3 branches
BranchTrueFalseTotal
if ($1 eq '.') { } 2 5 7
elsif ($1 eq '/') { } 2 3 5
elsif ($1 eq '-') { } 2 1 3
191 # deutsches Datum
192 2 ($d,$mo,$y,$h,$mi,$s) = $_[1] =~ /\d+/g;
193 }
194 elsif ($1 eq '/') {
195 # amerikanisches Datum
196 2 ($mo,$d,$y,$h,$mi,$s) = $_[1] =~ /\d+/g;
197 }
198 elsif ($1 eq '-') {
199 # ISO Datum
200 2 ($y,$mo,$d,$h,$mi,$s) = $_[1] =~ /\d+/g;
201 }
202 else {
203 # unbekanntes Format
204 1 $self->throw('Not implemented');
205 }
206 }
207 elsif ($_[0] eq 'ymd') {
208 0 ($y,$mo,$d) = $_[1] =~ /^(\d+)\D+(\d+)\D+(\d+)$/;
209 0 ▶ if (!$y) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y) 0 0 0
210 0 ($y,$mo,$d) = $_[1] =~ /^(\d{4})(\d{2})(\d{2})$/;
211 }
212 0 $h = $mi = $s = 0;
213 }
214 elsif ($_[0] eq 'ymdhm') {
215 0 ($y,$mo,$d,$h,$mi) =
216 $_[1] =~ /^(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)$/;
217 # split /\D+/,$_[1];
218 }
219 elsif ($_[0] eq 'ymdhms') {
220 1 ($y,$mo,$d,$h,$mi,$s) =
221 $_[1] =~ /^(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)$/;
222 # split /\D+/,$_[1];
223 }
224 elsif ($_[0] eq 'dmy') {
225 1 ($d,$mo,$y) = $_[1] =~ /^(\d+)\D+(\d+)\D+(\d+)$/;
226 1 $h = $mi = $s = 0;
227 }
228 elsif ($_[0] eq 'dmyhms') {
229 3 ($d,$mo,$y,$h,$mi,$s) =
230 $_[1] =~ /^(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)$/;
231 # split /\D+/,$_[1];
232 }
233 else {
234 112 ($y,$mo,$d,$h,$mi,$s) = @_;
235 }
236  
237 123 ▶ $mo ||= 1;
Condition100%2/2
$mo ||= 1
l!l
10518
MC/DC100%1/1
$mo ||= 1
$mo
Truth table100%2/2
A
A= $mo
Aresult
11
00
238 123 ▶ $d ||= 1;
Condition100%2/2
$d ||= 1
l!l
10221
MC/DC100%1/1
$d ||= 1
$d
Truth table100%2/2
A
A= $d
Aresult
11
00
239 123 ▶ $h ||= 0;
Condition100%2/2
$h ||= 0
l!l
5865
MC/DC100%1/1
$h ||= 0
$h
Truth table100%2/2
A
A= $h
Aresult
11
00
240 123 ▶ $mi ||= 0;
Condition100%2/2
$mi ||= 0
l!l
5766
MC/DC100%1/1
$mi ||= 0
$mi
Truth table100%2/2
A
A= $mi
Aresult
11
00
241 123 ▶ $s ||= 0;
Condition100%2/2
$s ||= 0
l!l
5865
MC/DC100%1/1
$s ||= 0
$s
Truth table100%2/2
A
A= $s
Aresult
11
00
242  
243 # Prüfe Angaben auf Korrektheit
244  
245 123 ▶ if (!defined $y || $y !~ /^\d\d\d\d$/ ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/ or $h < 0 or $h > 24 or not defined $mi or not $mi =~ /^\d+$/ or $mi < 0 or $mi > 60 or not defined $s or not $s =~ /^\d+$/ or $s < 0 or $s > 60) 3 120 123
Condition68%43/63
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/ or $h < 0 or $h > 24 or not defined $mi or not $mi =~ /^\d+$/ or $mi < 0 or $mi > 60 or not defined $s or not $s =~ /^\d+$/ or $s < 0 or $s > 60
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/ or $h < 0 or $h > 24 or not defined $mi or not $mi =~ /^\d+$/ or $mi < 0 or $mi > 60 or not defined $s or not $s =~ /^\d+$/ or $s < 0
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/ or $h < 0 or $h > 24 or not defined $mi or not $mi =~ /^\d+$/ or $mi < 0 or $mi > 60 or not defined $s or not $s =~ /^\d+$/
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/ or $h < 0 or $h > 24 or not defined $mi or not $mi =~ /^\d+$/ or $mi < 0 or $mi > 60 or not defined $s
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/ or $h < 0 or $h > 24 or not defined $mi or not $mi =~ /^\d+$/ or $mi < 0 or $mi > 60
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/ or $h < 0 or $h > 24 or not defined $mi or not $mi =~ /^\d+$/ or $mi < 0
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/ or $h < 0 or $h > 24 or not defined $mi or not $mi =~ /^\d+$/
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/ or $h < 0 or $h > 24 or not defined $mi
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/ or $h < 0 or $h > 24
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/ or $h < 0
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12
l!l&&r!l&&!r
30120
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1
l!l&&r!l&&!r
30120
defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/
!ll&&!rl&&r
30120
defined $y and $y =~ /^\d\d\d\d$/ and defined $mo
!ll&&!rl&&r
30120
defined $y and $y =~ /^\d\d\d\d$/
!ll&&!rl&&r
21120
MC/DC0%0/22
not defined $y && $y =~ /^\d\d\d\d$/ && defined $mo && $mo =~ /^\d+$/ or $mo < 1 or $mo > 12 or not defined $d or not $d =~ /^\d+$/ or $d < 1 or $d > 31 or not defined $h or not $h =~ /^\d+$/ or $h < 0 or $h > 24 or not defined $mi or not $mi =~ /^\d+$/ or $mi < 0 or $mi > 60 or not defined $s or not $s =~ /^\d+$/ or $s < 0 or $s > 60
too many conditions
246 !defined $mo || $mo !~ /^\d+$/ || $mo < 1 || $mo > 12 ||
247 !defined $d || $d !~ /^\d+$/ || $d < 1 || $d > 31 ||
248 !defined $h || $h !~ /^\d+$/ || $h < 0 || $h > 24 ||
249 !defined $mi || $mi !~ /^\d+$/ || $mi < 0 || $mi > 60 ||
250 !defined $s || $s !~ /^\d+$/ || $s < 0 || $s > 60) {
251 3 $self->throw(
252 'TIME-00002: Ungültige Zeitangabe',
253 Time => join(', ',@_),
254 );
255 }
256  
257 120 eval {Time::Local::timegm($s,$mi,$h,$d,$mo-1,$y-1900)};
258 120 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 120 120
259 0 $self->throw(
260 'TIME-00002: Ungültige Zeitangabe',
261 Time => join(',',@_),
262 InternalError => $@,
263 );
264 }
265  
266 # +0 um führende 0en wegzubekommen
267 120 @$self = ($y+0,$mo+0,$d+0,$h+0,$mi+0,$s+0);
268  
269 120 return $self;
270 }
271  
272 # -----------------------------------------------------------------------------
273  
274 =head3 setTime() - Setze Zeitanteil
275  
276 =head4 Synopsis
277  
278 $ti = $ti->setTime($hour,$minute,$second);
279  
280 =head4 Description
281  
282 Setze die Zeit auf die angegebenen Stunden, Minuten, Sekunden
283 und liefere das Zeitobjekt zurück. Wird für eine Zeitkomponente CL<lt>undef>
284 angegeben, wird diese nicht gesetzt.
285  
286 =cut
287  
288 # -----------------------------------------------------------------------------
289  
290 3 ▶sub setTime {
Subroutine setTime: called CC=10 SCAR=26.4
Pod documented
291 3 my $self = shift;
292 3 my $h = shift;
293 3 my $mi = shift;
294 3 my $s = shift;
295  
296 3 ▶ if (defined $h) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h) 3 0 3
297 3 ▶ if ($h < 0 || $h > 24) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h < 0 or $h > 24) 0 3 3
Condition33%1/3
$h < 0 or $h > 24
l!l&&r!l&&!r
003
MC/DC0%0/2
$h < 0 or $h > 24
$h < 0 $h > 24
Truth table33%1/3
A or B
A= $h < 0B= $h > 24
ABresult
1X1
011
000
298 0 $self->throw('TIME-00003: Illegale Stundenangabe',Hour=>$h);
299 }
300 3 $self->[3] = $h;
301 }
302 3 ▶ if (defined $mi) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mi) 2 1 3
303 2 ▶ if ($mi < 0 || $mi > 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mi < 0 or $mi > 60) 0 2 2
Condition33%1/3
$mi < 0 or $mi > 60
l!l&&r!l&&!r
002
MC/DC0%0/2
$mi < 0 or $mi > 60
$mi < 0 $mi > 60
Truth table33%1/3
A or B
A= $mi < 0B= $mi > 60
ABresult
1X1
011
000
304 0 $self->throw('TIME-00004: Illegale Minutenangabe',Minute=>$mi);
305 }
306 2 $self->[4] = $mi;
307 }
308 3 ▶ if (defined $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) 1 2 3
309 1 ▶ if ($s < 0 || $s > 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s < 0 or $s > 60) 0 1 1
Condition33%1/3
$s < 0 or $s > 60
l!l&&r!l&&!r
001
MC/DC0%0/2
$s < 0 or $s > 60
$s < 0 $s > 60
Truth table33%1/3
A or B
A= $s < 0B= $s > 60
ABresult
1X1
011
000
310 0 $self->throw('TIME-00005: Illegale Sekundenangabe',Second=>$s);
311 }
312 1 $self->[5] = $s;
313 }
314  
315 3 return $self;
316 }
317  
318 # -----------------------------------------------------------------------------
319  
320 =head3 truncate() - Kürze Zeitkomponenten
321  
322 =head4 Synopsis
323  
324 $ti = $ti->truncate($unit);
325  
326 =head4 Description
327  
328 Kürze Zeit auf Jahr (Y), Monat (M), Tag (D), Stunde (h) oder Minute (m),
329 d.h. setze alle kleineren Zeitkomponenten auf ihren kleinsten Wert
330 und liefere das Zeitobjekt zurück.
331  
332 =head4 Example
333  
334 $ti = Quiq::Time->new(2005,12,28,22,56,37);
335 $ti->truncate('D');
336 ==>
337 2005-12-28-00-00-00
338  
339 =cut
340  
341 # -----------------------------------------------------------------------------
342  
343 1 ▶sub truncate {
Subroutine truncate: called CC=7 SCAR=21.5
Pod documented
344 1 my $self = shift;
345 1 my $unit = shift;
346  
347 1 ▶ unless ($unit =~ tr/mhDMY//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($unit =~ tr/DMYhm//) 0 1 1
348 0 $self->throw('TIME-00006: Ungültige Zeitkomponente',Unit=>$unit);
349 }
350  
351 1 ▶ $self->[1] = 1 if $unit =~ tr/Y//; # M
Branch: 1 branch
BranchTrueFalseTotal
if $unit =~ tr/Y// 0 1 1
352 1 ▶ $self->[2] = 1 if $unit =~ tr/MY//; # D
Branch: 1 branch
BranchTrueFalseTotal
if $unit =~ tr/MY// 0 1 1
353 1 ▶ $self->[3] = 0 if $unit =~ tr/DMY//; # h
Branch: 1 branch
BranchTrueFalseTotal
if $unit =~ tr/DMY// 1 0 1
354 1 ▶ $self->[4] = 0 if $unit =~ tr/hDMY//; # m
Branch: 1 branch
BranchTrueFalseTotal
if $unit =~ tr/DMYh// 1 0 1
355 1 ▶ $self->[5] = 0 if $unit =~ tr/mhDMY//; # s
Branch: 1 branch
BranchTrueFalseTotal
if $unit =~ tr/DMYhm// 1 0 1
356  
357 1 return $self;
358 }
359  
360 # -----------------------------------------------------------------------------
361  
362 =head2 Abfrage von Zeitkomponenten
363  
364 =head3 year() - Liefere Jahr
365  
366 =head4 Synopsis
367  
368 $year = $ti->year;
369  
370 =cut
371  
372 # -----------------------------------------------------------------------------
373  
374 22 ▶sub year {
Subroutine year: called CC=1 SCAR=0.0
Pod documented
375 22 return shift->[0];
376 }
377  
378 # -----------------------------------------------------------------------------
379  
380 =head3 month() - Liefere Monat (Nummer)
381  
382 =head4 Synopsis
383  
384 $month = $ti->month;
385  
386 =head4 Description
387  
388 Liefere die Nummer des Monats. Wertebereich: 1-12.
389  
390 =cut
391  
392 # -----------------------------------------------------------------------------
393  
394 1 ▶sub month {
Subroutine month: called CC=1 SCAR=0.0
Pod documented
395 1 return shift->[1];
396 }
397  
398 # -----------------------------------------------------------------------------
399  
400 =head3 day() - Liefere Tag des Monats (Nummer)
401  
402 =head4 Synopsis
403  
404 $day = $ti->day;
405  
406 =cut
407  
408 # -----------------------------------------------------------------------------
409  
410 1 ▶sub day {
Subroutine day: called CC=1 SCAR=0.0
Pod documented
411 1 return shift->[2];
412 }
413  
414 # -----------------------------------------------------------------------------
415  
416 =head3 dayAbbr() - Abgekürzter Wochentagsname
417  
418 =head4 Synopsis
419  
420 $str = $ti->dayAbbr;
421  
422 =head4 Description
423  
424 Liefere abgekürzten Wochentagsnamen (Mo, Di, Mi, Do, Fr, Sa, So).
425  
426 =cut
427  
428 # -----------------------------------------------------------------------------
429  
430 270 our @DayAbbr = qw(Mo Di Mi Do Fr Sa So);
431  
432 1 ▶sub dayAbbr {
Subroutine dayAbbr: called CC=1 SCAR=0.0
Pod documented
433 1 my $self = shift;
434 1 return $DayAbbr[$self->dayOfWeek-1];
435 }
436  
437 # -----------------------------------------------------------------------------
438  
439 =head3 dayName() - Wochentagsname
440  
441 =head4 Synopsis
442  
443 $n = $ti->dayName;
444  
445 =head4 Description
446  
447 Liefere Wochentagsname (Montag, Dienstag, Mittwoch, Donnerstag,
448 Freitag, Samstag, Sonntag).
449  
450 =cut
451  
452 # -----------------------------------------------------------------------------
453  
454 270 our @DayName = qw(Montag Dienstag Mittwoch Donnerstag Freitag Samstag Sonntag);
455  
456 1 ▶sub dayName {
Subroutine dayName: called CC=1 SCAR=0.0
Pod documented
457 1 my $self = shift;
458 1 return $DayName[$self->dayOfWeek-1];
459 }
460  
461 # -----------------------------------------------------------------------------
462  
463 =head3 monthName() - Monatsname
464  
465 =head4 Synopsis
466  
467 $str = $ti->monthName;
468  
469 =head4 Description
470  
471 Liefere Monatsnamen (Januar, Februar, ..., Dezember).
472  
473 =cut
474  
475 # -----------------------------------------------------------------------------
476  
477 270 our %MonthName = (
478 english => [qw/
479 January
480 February
481 March
482 April
483 May
484 June
485 July
486 August
487 September
488 October
489 November
490 December
491 /],
492 german => [qw/
493 Januar
494 Februar
495 März
496 April
497 Mai
498 Juni
499 Juli
500 August
501 September
502 Oktober
503 November
504 Dezember
505 /],
506 );
507  
508 0 ▶sub monthName {
Subroutine monthName: not called CC=2 SCAR=17.9
Pod documented
509 0 my $self = shift;
510 0 ▶ my $lang = shift // 'german';
Condition0%0/2
shift() // "german"
l!l
00
MC/DC0%0/1
shift() // "german"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
511 0 return $MonthName{$lang}->[$self->month-1];
512 }
513  
514 # -----------------------------------------------------------------------------
515  
516 =head3 dayOfWeek() - Wochentagsnummer
517  
518 =head4 Synopsis
519  
520 $n = $ti->dayOfWeek;
521  
522 =head4 Description
523  
524 Liefere Wochentagsnummer im Bereich 1-7, 1 = Montag.
525  
526 =cut
527  
528 # -----------------------------------------------------------------------------
529  
530 11 ▶sub dayOfWeek {
Subroutine dayOfWeek: called CC=2 SCAR=6.9
Pod documented
531 11 my $self = shift;
532 11 my $t = Time::Local::timegm(0,0,0,$self->[2],$self->[1]-1,
533 $self->[0]-1900);
534 11 ▶ return (gmtime($t))[6] || 7;
Condition100%2/2
(gmtime $t)[6] || 7
l!l
83
MC/DC100%1/1
(gmtime $t)[6] || 7
(gmtime $t)[6]
Truth table100%2/2
A
A= (gmtime $t)[6]
Aresult
11
00
535 }
536  
537 # -----------------------------------------------------------------------------
538  
539 =head3 daysOfMonth() - Anzahl der Tage des Monats
540  
541 =head4 Synopsis
542  
543 $n = $ti->daysOfMonth;
544  
545 =head4 Description
546  
547 Liefere die Anzahl der Tage des Monats, also 31 für Januar,
548 28 oder 29 für Februar usw.
549  
550 =cut
551  
552 # -----------------------------------------------------------------------------
553  
554 270 our @DaysOfMonth = (31,28,31,30,31,30,31,31,30,31,30,31);
555  
556 3 ▶sub daysOfMonth {
Subroutine daysOfMonth: called CC=3 SCAR=11.0
Pod documented
557 3 my $self = shift;
558  
559 3 my $mo = $self->[1];
560 3 ▶ return 29 if $mo == 2 && $self->isLeapyear;
Branch: 1 branch
BranchTrueFalseTotal
if $mo == 2 and $self->isLeapyear 1 2 3
Condition100%3/3
$mo == 2 and $self->isLeapyear
!ll&&!rl&&r
111
MC/DC100%2/2
$mo == 2 and $self->isLeapyear
$mo == 2 $self->isLeapyear
Truth table100%3/3
A and B
A= $mo == 2B= $self->isLeapyear
ABresult
0X0
100
111
561 2 return $DaysOfMonth[$mo-1];
562 }
563  
564 # -----------------------------------------------------------------------------
565  
566 =head3 dayOfYear() - Tag des Jahres (Nummer)
567  
568 =head4 Synopsis
569  
570 $n = $ti->dayOfYear;
571  
572 =head4 Description
573  
574 Liefere die Tagesnummer innerhalb des Jahres.
575  
576 =cut
577  
578 # -----------------------------------------------------------------------------
579  
580 13 ▶sub dayOfYear {
Subroutine dayOfYear: called CC=1 SCAR=0.0
Pod documented
581 13 my $self = shift;
582  
583 13 my $t1 = $self->epoch;
584 13 my $t0 = ref($self)->new($self->year)->epoch;
585 13 my $days = int(($t1-$t0)/86400)+1;
586  
587 13 return $days;
588 }
589  
590 # -----------------------------------------------------------------------------
591  
592 =head3 weekOfYear() - Kalenderwoche (Jahr, Wochennummer)
593  
594 =head4 Synopsis
595  
596 ($year,$n) = $ti->weekOfYear;
597  
598 =head4 Description
599  
600 Liefere die Kalenderwoche, bestehend aus Jahr und Wochennummer
601 gemäß DIN 1355.
602  
603 =cut
604  
605 # -----------------------------------------------------------------------------
606  
607 8 ▶sub weekOfYear {
Subroutine weekOfYear: called CC=3 SCAR=11.0
Pod documented
608 8 my $self = shift;
609  
610 8 my $year = $self->year;
611 8 my $day = $self->dayOfYear;
612 8 my $dayOfWeek = ref($self)->new($year,1,1)->dayOfWeek;
613 8 ▶ if ($dayOfWeek > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dayOfWeek > 4) { } 6 2 8
614 6 $day -= 8-$dayOfWeek;
615 }
616 else {
617 2 $day += $dayOfWeek-1;
618 }
619 8 ▶ if ($day <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day <= 0) 2 6 8
620 2 return ($year-1,int(ref($self)->new($year-1,12,31)->dayOfYear/7)+1);
621 }
622  
623 6 return ($year,int($day/7)+1);
624 }
625  
626 # -----------------------------------------------------------------------------
627  
628 =head3 epoch() - Epoch-Zeit
629  
630 =head4 Synopsis
631  
632 $epoch = $ti->epoch('local');
633 $epoch = $ti->epoch('utc');
634 $epoch = $ti->epoch;
635  
636 =head4 Description
637  
638 Liefere Epoch-Zeit. Ein Aufruf ohne Argument ist äquivalent zu
639 $ti->epoch('utc').
640  
641 =cut
642  
643 # -----------------------------------------------------------------------------
644  
645 73 ▶sub epoch {
Subroutine epoch: called CC=3 SCAR=11.3
Pod documented
646 73 my $self = shift;
647 73 ▶ my $zone = shift || 'utc';
Condition50%1/2
shift() || 'utc'
l!l
073
MC/DC0%0/1
shift() || 'utc'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
648  
649 73 my @t = reverse @$self;
650 73 $t[4]--;
651 # NEIN: $t[5] -= 1900;
652  
653 73 ▶ return $zone eq 'utc'? Time::Local::timegm(@t):
Branch: 1 branch
BranchTrueFalseTotal
$zone eq 'utc' ? : 73 0 73
654 Time::Local::timelocal(@t);
655 }
656  
657 # -----------------------------------------------------------------------------
658  
659 =head3 isLeapyear() - Prüfe auf Schaltjahr
660  
661 =head4 Synopsis
662  
663 $bool = $ti->isLeapyear;
664  
665 =head4 Description
666  
667 Prüfe, ob Jahr ein Schaltjahr ist. Wenn ja, liefere "wahr",
668 andernfalls "falsch".
669  
670 =cut
671  
672 # -----------------------------------------------------------------------------
673  
674 4 ▶sub isLeapyear {
Subroutine isLeapyear: called CC=4 SCAR=14.2
Pod documented
675 4 my $y = shift->[0];
676 4 ▶ return $y%4 == 0 && $y%100 != 0 || $y%400 == 0? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$y % 4 == 0 && $y % 100 != 0 || $y % 400 == 0 ? : 2 2 4
Condition66%4/6
$y % 4 == 0 and $y % 100 != 0 or $y % 400 == 0
l!l&&r!l&&!r
202
$y % 4 == 0 and $y % 100 != 0
!ll&&!rl&&r
202
MC/DC33%1/3
$y % 4 == 0 and $y % 100 != 0 or $y % 400 == 0
$y % 4 == 0 $y % 100 != 0 $y % 400 == 0
Truth table40%2/5
A and B or C
A= $y % 4 == 0B= $y % 100 != 0C= $y % 400 == 0
ABCresult
11X1
0X11
1011
0X00
1000
677 }
678  
679 # -----------------------------------------------------------------------------
680  
681 =head2 Ausgabe von Zeit/Datum
682  
683 =head3 strftime() - Formatiere per strftime formatierte Zeit
684  
685 =head4 Synopsis
686  
687 $str = $ti->strftime($fmt);
688  
689 =cut
690  
691 # -----------------------------------------------------------------------------
692  
693 1 ▶sub strftime {
Subroutine strftime: called CC=1 SCAR=0.0
Pod documented
694 1 my ($self,$fmt) = @_;
695  
696 1 require POSIX;
697 1 return POSIX::strftime $fmt,
698 $self->[5], # s
699 $self->[4], # mi
700 $self->[3], # h
701 $self->[2], # d
702 $self->[1]-1, # mo
703 $self->[0]-1900; # y
704 }
705  
706 # -----------------------------------------------------------------------------
707  
708 =head3 ddmmyyyy() - Liefere formatiertes Datum
709  
710 =head4 Synopsis
711  
712 $str = $ti->ddmmyyyy;
713 $str = $ti->ddmmyyyy($sep);
714  
715 =head4 Description
716  
717 Liefere Datum im Format DD.MM.YYYY, wobei Tag (DD) und Monat (MM) mit
718 führender 0 angegeben werden. Ist $sep angegeben, wird anstelle des
719 Punktes (.) die betreffende Zeichenkette als Trenner verwendet.
720  
721 =cut
722  
723 # -----------------------------------------------------------------------------
724  
725 0 ▶sub ddmmyyyy {
Subroutine ddmmyyyy: not called CC=2 SCAR=17.9
Pod documented
726 0 my $self = shift;
727 0 ▶ my $sep = @_? shift: '.';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
728  
729 0 return sprintf '%02d%s%02d%s%04d',
730 $self->[2],$sep,$self->[1],$sep,$self->[0];
731 }
732  
733 # -----------------------------------------------------------------------------
734  
735 =head3 ddmmyyyyhhmmss() - Liefere formatiertes Datum+Zeit
736  
737 =head4 Synopsis
738  
739 $str = $ti->ddmmyyyyhhmmss;
740  
741 =head4 Description
742  
743 Liefere Datum/Zeit im Format "DD.MM.YYYY HH:MI:SS". Der Aufruf ist
744 äquvalent zu
745  
746 $str = $ti->ddmmyyyy.' '.$ti->hhmmss;
747  
748 =cut
749  
750 # -----------------------------------------------------------------------------
751  
752 0 ▶sub ddmmyyyyhhmmss {
Subroutine ddmmyyyyhhmmss: not called CC=1 SCAR=6.9
Pod documented
753 0 my $self = shift;
754 0 return $self->ddmmyyyy.' '.$self->hhmmss;
755 }
756  
757 # -----------------------------------------------------------------------------
758  
759 =head3 dmy() - Liefere formatiertes Datum
760  
761 =head4 Synopsis
762  
763 $str = $ti->dmy;
764 $str = $ti->dmy($sep);
765  
766 =head4 Description
767  
768 Liefere Datum im Format D.M.YYYY, wobei Tag (D) und Monat (M) ohne
769 führende 0 angegeben werden. Ist $sep angegeben, wird anstelle des
770 Punktes (.) die betreffende Zeichenkette als Trenner verwendet.
771  
772 =cut
773  
774 # -----------------------------------------------------------------------------
775  
776 0 ▶sub dmy {
Subroutine dmy: not called CC=2 SCAR=17.9
Pod documented
777 0 my $self = shift;
778 0 ▶ my $sep = @_? shift: '.';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
779  
780 0 return sprintf '%d%s%d%s%d',
781 $self->[2],$sep,$self->[1],$sep,$self->[0];
782 }
783  
784 # -----------------------------------------------------------------------------
785  
786 =head3 dmyhhmmss() - Liefere formatiertes Datum+Zeit
787  
788 =head4 Synopsis
789  
790 $str = $ti->dmyhhmmss;
791  
792 =head4 Description
793  
794 Liefere Datum/Zeit im Format "D.M.YYYY HH:MI:SS". Der Aufruf ist
795 äquvalent zu
796  
797 $str = $ti->dmy.' '.$ti->hhmmss;
798  
799 =cut
800  
801 # -----------------------------------------------------------------------------
802  
803 0 ▶sub dmyhhmmss {
Subroutine dmyhhmmss: not called CC=1 SCAR=6.9
Pod documented
804 0 my $self = shift;
805 0 return $self->dmy.' '.$self->hhmmss;
806 }
807  
808 # -----------------------------------------------------------------------------
809  
810 =head3 dump() - Liefere Zeitobjekt als Zeichenkette
811  
812 =head4 Synopsis
813  
814 $str = $ti->dump;
815 $str = $ti->dump($sep);
816  
817 =head4 Description
818  
819 Liefere den internen Zustand des Zeitobjekts als Zeichenkette
820 im Format
821  
822 YYYY-MM-DD-hh-mm-ss
823  
824 Ist $sep angegeben, verwende diesen String anstelle von '-' als
825 Trenner.
826  
827 =cut
828  
829 # -----------------------------------------------------------------------------
830  
831 31 ▶sub dump {
Subroutine dump: called CC=2 SCAR=7.0
Pod documented
832 31 my ($self,$sep) = @_;
833  
834 31 ▶ if (!defined $sep) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sep) 31 0 31
835 31 $sep = '-';
836 }
837  
838 31 return sprintf "%4d$sep%02d$sep%02d$sep%02d$sep%02d$sep%02d",@$self;
839 }
840  
841 # -----------------------------------------------------------------------------
842  
843 =head3 hhmmss() - Liefere formatierte Zeit
844  
845 =head4 Synopsis
846  
847 $str = $ti->hhmmss;
848 $str = $ti->hhmmss($sep);
849  
850 =head4 Description
851  
852 Liefere Zeit im Format HH:MM:SS, wobei alle Angaben zweistellig sind,
853 also ggf. eine führende 0 vorangestellt wird. Ist $sep angegeben, wird
854 anstelle des Doppelpunkts (:) die betreffende Zeichenkette als Trenner
855 verwendet.
856  
857 =cut
858  
859 # -----------------------------------------------------------------------------
860  
861 14 ▶sub hhmmss {
Subroutine hhmmss: called CC=2 SCAR=6.9
Pod documented
862 14 my $self = shift;
863 14 ▶ my $sep = @_? shift: ':';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 13 14
864  
865 14 return sprintf '%02d%s%02d%s%02d',
866 $self->[3],$sep,$self->[4],$sep,$self->[5];
867 }
868  
869 # -----------------------------------------------------------------------------
870  
871 =head3 hhmm() - Liefere formatierte Zeit
872  
873 =head4 Synopsis
874  
875 $str = $ti->hhmm;
876 $str = $ti->hhmm($sep);
877  
878 =head4 Description
879  
880 Liefere Zeit im Format HH:MM, wobei alle Angaben zweistellig sind,
881 also ggf. eine führende 0 vorangestellt wird. Ist $sep angegeben, wird
882 anstelle des Doppelpunkts (:) die betreffende Zeichenkette als Trenner
883 verwendet.
884  
885 =cut
886  
887 # -----------------------------------------------------------------------------
888  
889 0 ▶sub hhmm {
Subroutine hhmm: not called CC=2 SCAR=17.9
Pod documented
890 0 my $self = shift;
891 0 ▶ my $sep = @_? shift: ':';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
892  
893 0 return sprintf '%02d%s%02d',$self->[3],$sep,$self->[4];
894 }
895  
896 # -----------------------------------------------------------------------------
897  
898 =head3 yymmdd() - Liefere formatiertes Datum
899  
900 =head4 Synopsis
901  
902 $str = $ti->yymmdd;
903 $str = $ti->yymmdd($sep);
904  
905 =head4 Description
906  
907 Liefere Datum im Format YY-MM-DD, wobei das Jahr zweistellig angegeben
908 ist und Tag (DD) und Monat (MM) mit führender 0 angegeben werden. Ist
909 $sep angegeben, wird anstelle des Bindestrichs (-) die betreffende
910 Zeichenkette als Trenner verwendet.
911  
912 =cut
913  
914 # -----------------------------------------------------------------------------
915  
916 2 ▶sub yymmdd {
Subroutine yymmdd: called CC=1 SCAR=0.0
Pod documented
917 2 return substr(shift->yyyymmdd(@_),2);
918 }
919  
920 # -----------------------------------------------------------------------------
921  
922 =head3 yyyymmdd() - Liefere formatiertes Datum
923  
924 =head4 Synopsis
925  
926 $str = $ti->yyyymmdd;
927 $str = $ti->yyyymmdd($sep);
928  
929 =head4 Description
930  
931 Liefere Datum im Format YYYY-MM-DD, wobei Tag (DD) und Monat (MM) mit
932 führender 0 angegeben werden. Ist $sep angegeben, wird anstelle des
933 Bindestrichs (-) die betreffende Zeichenkette als Trenner verwendet.
934  
935 =cut
936  
937 # -----------------------------------------------------------------------------
938  
939 16 ▶sub yyyymmdd {
Subroutine yyyymmdd: called CC=2 SCAR=6.9
Pod documented
940 16 my $self = shift;
941 16 ▶ my $sep = @_? shift: '-';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 14 16
942  
943 16 return sprintf '%04d%s%02d%s%02d',
944 $self->[0],$sep,$self->[1],$sep,$self->[2];
945 }
946  
947 # -----------------------------------------------------------------------------
948  
949 =head3 yyyymmddhhmmss() - Liefere formatiertes Datum+Zeit
950  
951 =head4 Synopsis
952  
953 $str = $ti->yyyymmddhhmmss;
954 $str = $ti->yyyymmddhhmmss($sep);
955  
956 =head4 Alias
957  
958 iso()
959  
960 =head4 Description
961  
962 Liefere Datum/Zeit im Format "YYYY-MM-DDXHH:MI:SS", wobei X
963 das Trennzeichen zwischen Datum und Uhrzeit ist. Der Aufruf ist
964 äquvalent zu
965  
966 $str = $ti->yyyymmdd.$sep.$ti->hhmmss;
967  
968 Ist $sep nicht angegeben, wird ein Leerzeichen als Trenner genommen.
969  
970 =cut
971  
972 # -----------------------------------------------------------------------------
973  
974 12 ▶sub yyyymmddhhmmss {
Subroutine yyyymmddhhmmss: called CC=2 SCAR=6.9
Pod documented
975 12 my $self = shift;
976 12 ▶ my $sep = @_? shift: ' ';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 11 1 12
977 12 return $self->yyyymmdd.$sep.$self->hhmmss;
978 }
979  
980 {
981 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
982 270 *iso = \&yyyymmddhhmmss;
983 }
984  
985 # -----------------------------------------------------------------------------
986  
987 =head3 yyyymmddhhmm() - Liefere formatiertes Datum + Stunde + Minute
988  
989 =head4 Synopsis
990  
991 $str = $ti->yyyymmddhhmm;
992 $str = $ti->yyyymmddhhmm($sep);
993  
994 =head4 Description
995  
996 Liefere Datum/Zeit im Format "YYYY-MM-DDXHH:MI", wobei X
997 das Trennzeichen zwischen Datum und Uhrzeit ist. Der Aufruf ist
998 äquvalent zu
999  
1000 $str = $ti->yyyymmdd.$sep.$ti->hhmm;
1001  
1002 Ist $sep nicht angegeben, wird ein Leerzeichen als Trenner genommen.
1003  
1004 =cut
1005  
1006 # -----------------------------------------------------------------------------
1007  
1008 0 ▶sub yyyymmddhhmm {
Subroutine yyyymmddhhmm: not called CC=2 SCAR=17.9
Pod documented
1009 0 my $self = shift;
1010 0 ▶ my $sep = @_? shift: ' ';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
1011 0 return $self->yyyymmdd.$sep.$self->hhmm;
1012 }
1013  
1014 # -----------------------------------------------------------------------------
1015  
1016 =head3 yyyymmddxhhmmss() - Liefere formatiertes Datum+Zeit mit +-Trenner
1017  
1018 =head4 Synopsis
1019  
1020 $str = $ti->yyyymmddxhhmmss;
1021  
1022 =head4 Description
1023  
1024 Liefere Datum/Zeit im Format "YYYY-MM-DD+HH:MI:SS". Der Aufruf ist
1025 äquvalent zu
1026  
1027 $str = $ti->yyyymmddhhmmss('+');
1028  
1029 =cut
1030  
1031 # -----------------------------------------------------------------------------
1032  
1033 9 ▶sub yyyymmddxhhmmss {
Subroutine yyyymmddxhhmmss: called CC=1 SCAR=0.0
Pod documented
1034 9 return shift->yyyymmddhhmmss('+');
1035 }
1036  
1037 # -----------------------------------------------------------------------------
1038  
1039 =head2 Zeitarithmetik
1040  
1041 =head3 add() - Addiere Zeitkomponenten
1042  
1043 =head4 Synopsis
1044  
1045 $ti = $ti->add($n1,$unit1,$n2,$unit2,...);
1046  
1047 =head4 Description
1048  
1049 Addiere die angegebenen Zeitkomponenten ($nI, $unitI) für Jahr (Y),
1050 Monat (M) oder Tag (D) zur Zeit hinzu und liefere das modifizierte
1051 Zeitobjekt zurück. Die $nI sind ganze Zahlen, können also auch negativ
1052 sein.
1053  
1054 =head4 Example
1055  
1056 $ti = Quiq::Time->new(2005,12,28,22,56,37);
1057 $ti->add(3,'Y',5,'M',-1,'D');
1058 ==>
1059 2009-05-27-22-56-37
1060  
1061 =cut
1062  
1063 # -----------------------------------------------------------------------------
1064  
1065 1 ▶sub add {
Subroutine add: called CC=5 SCAR=16.2
Pod documented
1066 1 my $self = shift;
1067  
1068 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 1 4
1069 3 my $n = shift;
1070 3 my $unit = shift;
1071  
1072 3 ▶ if ($unit eq 'Y') { $self->addYears($n) }
Branch: 3 branches
BranchTrueFalseTotal
if ($unit eq 'Y') { } 1 2 3
elsif ($unit eq 'M') { } 1 1 2
elsif ($unit eq 'D') { } 1 0 1
1073 1 elsif ($unit eq 'M') { $self->addMonths($n) }
1074 1 elsif ($unit eq 'D') { $self->addDays($n) }
1075 else {
1076 0 $self->throw('TIME-00001: Ungültige Zeitkomponente',Unit=>$unit);
1077 }
1078 }
1079  
1080 1 return $self;
1081 }
1082  
1083 # -----------------------------------------------------------------------------
1084  
1085 =head3 addYears() - Addiere n Jahre
1086  
1087 =head4 Synopsis
1088  
1089 $ti = $ti->addYears($n);
1090  
1091 =head4 Description
1092  
1093 Addiere $n Jahre zum Zeitobjekt hinzu ($n ist eine ganze Zahl, kann
1094 also auch negativ sein) und liefere das modifizierte Zeitobjekt
1095 zurück.
1096  
1097 =head4 Example
1098  
1099 =over 2
1100  
1101 =item *
1102  
1103 Jahre hinzuaddieren
1104  
1105 $ti = Quiq::Time->new(2005,12,28,22,56,37);
1106 $ti->addYears(5);
1107 ==>
1108 2010-12-28-00-00-00
1109  
1110 =item *
1111  
1112 Jahre abziehen
1113  
1114 $ti = Quiq::Time->new(2005,12,28,22,56,37);
1115 $ti->addYears(-6);
1116 ==>
1117 1999-12-28-00-00-00
1118  
1119 =back
1120  
1121 =cut
1122  
1123 # -----------------------------------------------------------------------------
1124  
1125 3 ▶sub addYears {
Subroutine addYears: called CC=1 SCAR=0.0
Pod documented
1126 3 my $self = shift;
1127 3 my $n = shift;
1128  
1129 3 $self->[0] += $n;
1130  
1131 3 return $self;
1132 }
1133  
1134 # -----------------------------------------------------------------------------
1135  
1136 =head3 addMonths() - Addiere n Monate
1137  
1138 =head4 Synopsis
1139  
1140 $ti = $ti->addMonths($n);
1141  
1142 =head4 Description
1143  
1144 Addiere $n Monate zum Zeitobjekt hinzu ($n ist eine ganze Zahl, kann
1145 also auch negativ sein) und liefere das modifizierte Zeitobjekt
1146 zurück.
1147  
1148 =cut
1149  
1150 # -----------------------------------------------------------------------------
1151  
1152 7 ▶sub addMonths {
Subroutine addMonths: called CC=2 SCAR=6.9
Pod documented
1153 7 my $self = shift;
1154 7 my $n = shift;
1155  
1156 # $sum ==
1157 # -13 -12 -11 -10 -9 ... -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1158 # $m =
1159 # 11 12 1 2 3 ... 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2
1160 # $y +=
1161 # -2 -2 -1 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 +1 +1
1162  
1163 7 my $sum = $self->[1]+$n;
1164 7 $self->[0] += POSIX::floor(($sum-1)/12);
1165 7 ▶ $self->[1] = $sum%12 || 12;
Condition100%2/2
$sum % 12 || 12
l!l
43
MC/DC100%1/1
$sum % 12 || 12
$sum % 12
Truth table100%2/2
A
A= $sum % 12
Aresult
11
00
1166  
1167 7 return $self;
1168 }
1169  
1170 # -----------------------------------------------------------------------------
1171  
1172 =head3 addDays() - Addiere n Tage
1173  
1174 =head4 Synopsis
1175  
1176 $ti = $ti->addDays($n);
1177  
1178 =head4 Description
1179  
1180 Addiere $n Tage zum Zeitobjekt hinzu ($n ist eine ganze Zahl, kann
1181 also auch negativ sein) und liefere das modifizierte Zeitobjekt
1182 zurück.
1183  
1184 =cut
1185  
1186 # -----------------------------------------------------------------------------
1187  
1188 2 ▶sub addDays {
Subroutine addDays: called CC=1 SCAR=0.0
Pod documented
1189 2 my $self = shift;
1190 2 my $n = shift;
1191  
1192 2 my $t = $self->epoch;
1193 2 $t += 86400*$n;
1194 2 $self->set(utc=>$t);
1195  
1196 2 return $self;
1197 }
1198  
1199 # -----------------------------------------------------------------------------
1200  
1201 =head3 addHours() - Addiere n Stunden
1202  
1203 =head4 Synopsis
1204  
1205 $ti = $ti->addHours($n);
1206  
1207 =head4 Description
1208  
1209 Addiere $n Stunden zum Zeitobjekt hinzu ($n ist eine ganze Zahl, kann
1210 also auch negativ sein) und liefere das modifizierte Zeitobjekt
1211 zurück.
1212  
1213 =cut
1214  
1215 # -----------------------------------------------------------------------------
1216  
1217 2 ▶sub addHours {
Subroutine addHours: called CC=1 SCAR=0.0
Pod documented
1218 2 my $self = shift;
1219 2 my $n = shift;
1220  
1221 2 my $t = $self->epoch;
1222 2 $t += $n*3600;
1223 2 $self->set(utc=>$t);
1224  
1225 2 return $self;
1226 }
1227  
1228 # -----------------------------------------------------------------------------
1229  
1230 =head3 addMinutes() - Addiere n Minuten
1231  
1232 =head4 Synopsis
1233  
1234 $ti = $ti->addMinutes($n);
1235  
1236 =head4 Description
1237  
1238 Addiere $n Minuten zum Zeitobjekt hinzu ($n ist eine ganze Zahl, kann
1239 also auch negativ sein) und liefere das modifizierte Zeitobjekt
1240 zurück.
1241  
1242 =cut
1243  
1244 # -----------------------------------------------------------------------------
1245  
1246 0 ▶sub addMinutes {
Subroutine addMinutes: not called CC=1 SCAR=6.9
Pod documented
1247 0 my $self = shift;
1248 0 my $n = shift;
1249  
1250 0 my $t = $self->epoch;
1251 0 $t += $n*60;
1252 0 $self->set(utc=>$t);
1253  
1254 0 return $self;
1255 }
1256  
1257 # -----------------------------------------------------------------------------
1258  
1259 =head3 addSeconds() - Addiere n Sekunden
1260  
1261 =head4 Synopsis
1262  
1263 $ti = $ti->addSeconds($n);
1264  
1265 =head4 Description
1266  
1267 Addiere $n Sekunden zum Zeitobjekt hinzu ($n ist eine ganze Zahl, kann
1268 also auch negativ sein) und liefere das modifizierte Zeitobjekt
1269 zurück.
1270  
1271 =cut
1272  
1273 # -----------------------------------------------------------------------------
1274  
1275 2 ▶sub addSeconds {
Subroutine addSeconds: called CC=1 SCAR=0.0
Pod documented
1276 2 my $self = shift;
1277 2 my $n = shift;
1278  
1279 2 my $t = $self->epoch;
1280 2 $t += $n;
1281 2 $self->set(utc=>$t);
1282  
1283 2 return $self;
1284 }
1285  
1286 # -----------------------------------------------------------------------------
1287  
1288 =head2 Vergleich von Zeiten
1289  
1290 =head3 equal() - Prüfe auf Gleichheit
1291  
1292 =head4 Synopsis
1293  
1294 $bool = $ti1->equal($ti2);
1295  
1296 =head4 Description
1297  
1298 Liefere "wahr", wenn der Zeitpunkt $ti1 gleich dem Zeitpunkt $ti2
1299 ist, andernfalls "falsch".
1300  
1301 =cut
1302  
1303 # -----------------------------------------------------------------------------
1304  
1305 0 ▶sub equal {
Subroutine equal: not called CC=1 SCAR=6.9
Pod documented
1306 0 return shift->epoch == shift->epoch;
1307 }
1308  
1309 # -----------------------------------------------------------------------------
1310  
1311 =head3 less() - Prüfe auf kleiner
1312  
1313 =head4 Synopsis
1314  
1315 $bool = $ti1->less($ti2);
1316  
1317 =head4 Description
1318  
1319 Liefere "wahr", wenn der Zeitpunkt $ti1 vor dem Zeitpunkt $ti2
1320 liegt, andernfalls "falsch".
1321  
1322 =cut
1323  
1324 # -----------------------------------------------------------------------------
1325  
1326 0 ▶sub less {
Subroutine less: not called CC=1 SCAR=6.9
Pod documented
1327 0 return shift->epoch < shift->epoch;
1328 }
1329  
1330 # -----------------------------------------------------------------------------
1331  
1332 =head3 lessOrEqual() - Prüfe auf kleinergleich
1333  
1334 =head4 Synopsis
1335  
1336 $bool = $ti1->lessOrEqual($ti2);
1337  
1338 =head4 Description
1339  
1340 Liefere "wahr", wenn der Zeitpunkt $ti1 vor oder gleich dem Zeitpunkt
1341 $ti2 ist, andernfalls "falsch".
1342  
1343 =cut
1344  
1345 # -----------------------------------------------------------------------------
1346  
1347 0 ▶sub lessOrEqual {
Subroutine lessOrEqual: not called CC=1 SCAR=6.9
Pod documented
1348 0 return shift->epoch <= shift->epoch;
1349 }
1350  
1351 # -----------------------------------------------------------------------------
1352  
1353 =head2 Zeitdifferenz
1354  
1355 =head3 diff() - Differenz zweier Zeiten
1356  
1357 =head4 Synopsis
1358  
1359 $dur = $ti1->diff($ti2);
1360  
1361 =head4 Description
1362  
1363 Bilde die Differenz zweier Zeit-Objekte $ti2-$ti1 und liefere ein
1364 Zeitdauer-Objekt $dur zurück.
1365  
1366 Dauer in Sekunden:
1367  
1368 $sec = $dur->asSeconds;
1369  
1370 Dauer als Zeichenkette (I<D>dI<H>hI<M>mI<S.X>s):
1371  
1372 $str = $dur->asString;
1373  
1374 =cut
1375  
1376 # -----------------------------------------------------------------------------
1377  
1378 0 ▶sub diff {
Subroutine diff: not called CC=1 SCAR=6.9
Pod documented
1379 0 my ($t1,$t2) = @_;
1380 0 return Quiq::Duration->new($t2->epoch-$t1->epoch);
1381 }
1382  
1383 # -----------------------------------------------------------------------------
1384  
1385 =head2 Konvertierung (Klassenmethoden)
1386  
1387 =head3 monthAbbrToNum() - Liefere Monatsnummer zu Monats-Abkürzung
1388  
1389 =head4 Synopsis
1390  
1391 $n = $class->monthAbbrToNum($abbr);
1392 $n = $class->monthAbbrToNum($abbr,$lang);
1393  
1394 =head4 Description
1395  
1396 Liefere Monatsnummer (1, ..., 12) zur Monatsabkürzung der
1397 Sprache $lang. Default für $lang ist 'en'.
1398  
1399 =cut
1400  
1401 # -----------------------------------------------------------------------------
1402  
1403 270 our $MonthAbbr = {
1404 de => {
1405 Jan => 1,
1406 Feb => 2,
1407 Mär => 3,
1408 Apr => 4,
1409 Mai => 5,
1410 Jun => 6,
1411 Jul => 7,
1412 Aug => 8,
1413 Sep => 9,
1414 Okt => 10,
1415 Nov => 11,
1416 Dez => 12,
1417 },
1418 en => {
1419 Jan => 1,
1420 Feb => 2,
1421 Mar => 3,
1422 Apr => 4,
1423 May => 5,
1424 Jun => 6,
1425 Jul => 7,
1426 Aug => 8,
1427 Sep => 9,
1428 Oct => 10,
1429 Nov => 11,
1430 Dec => 12,
1431 },
1432 };
1433  
1434 3 ▶sub monthAbbrToNum {
Subroutine monthAbbrToNum: called CC=3 SCAR=11.0
Pod documented
1435 3 my $self = shift;
1436 3 my $abbr = shift;
1437 3 ▶ my $lang = shift || 'en';
Condition100%2/2
shift() || 'en'
l!l
21
MC/DC100%1/1
shift() || 'en'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1438  
1439 3 my $n = $MonthAbbr->{$lang}->{$abbr};
1440 3 ▶ if (!$n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 1 2 3
1441 1 $self->throw(
1442 'TIME-00099: Unknown month abbreviation',
1443 Abbreviation => $abbr,
1444 Language => $lang,
1445 );
1446 }
1447  
1448 2 return $n;
1449 }
1450  
1451 # -----------------------------------------------------------------------------
1452  
1453 =head1 VERSION
1454  
1455 1.240
1456  
1457 =head1 AUTHOR
1458  
1459 Frank Seitz, L<http://fseitz.de/>
1460  
1461 =head1 COPYRIGHT
1462  
1463 Copyright (C) 2026 Frank Seitz
1464  
1465 =head1 LICENSE
1466  
1467 This code is free software; you can redistribute it and/or modify
1468 it under the same terms as Perl itself.
1469  
1470 =cut
1471  
1472 # -----------------------------------------------------------------------------
1473  
1474 270 1;
1475  
1476 # eof
« Quiq/Object.pm ↑ Summary Quiq/AsciiTable.pm »