Quiq/Formatter.pm

filter: Statementstmt 96.6% 58 / 60 Branchbran 86.6% 26 / 30 Conditioncond 76.4% 13 / 17 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 91.6% 11 / 12 Podpod 100% 5 / 5 totaltotal 88.1% 119 / 135 CC 22 SCAR 23.3
CC
22
Cov
91%
reducedIsoTime
17.9
readableNumber
4.0
normalizeNumber
3.0
CRAP 10.3
« Quiq/Assert.pm ↑ Summary Quiq/Json/Code.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Formatter - Formatierung von Werten
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::Formatter;
18 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 270 our $VERSION = '1.240';
25  
26 270 ▶use Quiq::Math;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 270 ▶use Quiq::Formatter;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 270 ▶use Quiq::Epoch;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # -----------------------------------------------------------------------------
31  
32 =head1 METHODS
33  
34 =head2 Konstruktor
35  
36 =head3 new() - Instantiiere Objekt
37  
38 =head4 Synopsis
39  
40 $f = $class->new;
41  
42 =head4 Returns
43  
44 Path-Objekt
45  
46 =head4 Description
47  
48 Instantiiere ein Objekt der Klasse und liefere eine Referenz auf
49 dieses Objekt zurück. Da die Klasse ausschließlich Klassenmethoden
50 enthält, hat das Objekt ausschließlich die Funktion, eine abkürzende
51 Aufrufschreibweise zu ermöglichen.
52  
53 =cut
54  
55 # -----------------------------------------------------------------------------
56  
57 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
58 0 my $class = shift;
59 0 return bless \(my $dummy),$class;
60 }
61  
62 # -----------------------------------------------------------------------------
63  
64 =head2 Zahlen
65  
66 =head3 geldbetrag() - Formatiere Zahl als Geldbetrag
67  
68 =head4 Synopsis
69  
70 $str = $class->geldbetrag($x);
71  
72 =head4 Arguments
73  
74 =over 4
75  
76 =item $x
77  
78 Zahl
79  
80 =back
81  
82 =head4 Returns
83  
84 (String) Zahl als Geldbetrag
85  
86 =head4 Description
87  
88 Formatiere eine Zahl $x als deutschen Geldbetrag, d.h. mit Komma
89 als Dezimaltrennzeichen, zwei Nachkommastellen, Punkt als
90 Tausender-Trennzeichen und liefere das Resultat zurück.
91  
92 =cut
93  
94 # -----------------------------------------------------------------------------
95  
96 3 ▶sub geldbetrag {
Subroutine geldbetrag: called CC=1 SCAR=0.0
Pod documented
97 3 my ($class,$x) = @_;
98  
99 3 $x = Quiq::Math->roundTo($x,2);
100 3 $x = Quiq::Formatter->readableNumber($x);
101  
102 3 return $x;
103 }
104  
105 # -----------------------------------------------------------------------------
106  
107 =head3 normalizeNumber() - Normalisiere Zahldarstellung
108  
109 =head4 Synopsis
110  
111 $x = $class->normalizeNumber($x);
112  
113 =head4 Description
114  
115 Entferne unnötige Nullen von einer Zahl, forciere als Dezimaltrennzeichen
116 einen Punkt (anstelle eines Komma) und liefere das Resultat zurück.
117  
118 =head4 Example
119  
120 123.456000 -> 123.456
121 70.00 -> 70
122 0.0 -> 0
123 -0.0 -> 0
124 007 -> 7
125 23,7 -> 23.7
126  
127 =cut
128  
129 # -----------------------------------------------------------------------------
130  
131 65 ▶sub normalizeNumber {
Subroutine normalizeNumber: called CC=3 SCAR=11.0
Pod documented
132 65 my ($class,$x) = @_;
133  
134 # Wandele Komma in Punkt
135 65 $x =~ s/,/./;
136  
137 # Entferne führende 0en
138 65 $x =~ s/^(-?)0+(?=\d)/$1/;
139  
140 65 ▶ if (index($x,'.') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($x, ".") != -1) 25 40 65
141 # Bei einer Kommazahl entferne 0en und ggf. Punkt am Ende
142 25 $x =~ s/\.?0+$//;
143 }
144  
145 65 ▶ if ($x eq '-0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x eq "-0") 1 64 65
146 1 $x = 0;
147 }
148  
149 65 return $x;
150 }
151  
152 # -----------------------------------------------------------------------------
153  
154 =head3 readableNumber() - Zahl mit Trenner an Tausender-Stellen
155  
156 =head4 Synopsis
157  
158 $str = $class->readableNumber($x);
159 $str = $class->readableNumber($x,$sep);
160  
161 =head4 Description
162  
163 Formatiere eine Zahl $x mit Tausender-Trennzeichen $sep. Per
164 Default ist $sep ein Punkt (C<.>). Handelt es sich bei $x um eine
165 Zahl mit Nachkomma-Stellen, wird der Punkt durch ein Komma (C<,>)
166 ersetzt.
167  
168 =head4 Example
169  
170 1 -> 1
171 12 -> 12
172 12345 -> 12.345
173 -12345678 -> -12.345.678
174 -12345.678 -> -12.345,678
175  
176 =cut
177  
178 # -----------------------------------------------------------------------------
179  
180 15 ▶sub readableNumber {
Subroutine readableNumber: called CC=4 SCAR=13.9
Pod documented
181 15 my $class = shift;
182 15 my $x = shift;
183 15 ▶ my $sep = shift // '.';
Condition100%2/2
shift() // "."
l!l
69
MC/DC100%1/1
shift() // "."
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
184  
185 15 ▶ if ($sep eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sep eq ".") 9 6 15
186 9 $x =~ s/\./,/;
187 }
188 15 ▶ 1 while $x =~ s/^([-+]?\d+)(\d{3})/$1$sep$2/;
Branch: 1 branch
BranchTrueFalseTotal
$x =~ s/^([-+]?\d+)(\d{3})/$1$sep$2/ and do { '???' } 9 15 24
189  
190 15 return $x;
191 }
192  
193 # -----------------------------------------------------------------------------
194  
195 =head2 Datums/Zeitangaben
196  
197 =head3 reducedIsoTime() - Erzeuge reduzierte ISO-Zeitdarstellung
198  
199 =head4 Synopsis
200  
201 $str = $class->reducedIsoTime($now,$time);
202  
203 =head4 Arguments
204  
205 =over 4
206  
207 =item $now
208  
209 Bezugszeitpunkt in Unix Epoch oder als ISO-Datum.
210  
211 =item $time
212  
213 Zeit in Unix Epoch oder als ISO-Datum.
214  
215 =back
216  
217 =head4 Returns
218  
219 Reduzierte ISO-Zeitdarstellung (String)
220  
221 =head4 Description
222  
223 Erzeuge eine "reduzierte" ISO-Zeitdarstellung für Zeitpunkt $time
224 relativ zu Bezugszeitpunkt $now. Die I<unreduzierte> ISO-Zeitdarstellung
225 hat das Format:
226  
227 YYYY-MM-DD HH:MM:SS
228  
229 Die I<reduzierte> Dastellung ist identisch aufgebaut, nur dass alle
230 führenden Zeitkomponenten fehlen, die zum Bezugszeitpunkt $now
231 identisch sind.
232  
233 Diese Darstellung ist nützlich, um in einer Liste von Zeiten die
234 nah am aktuellen Zeipunkt liegenden Zeiten leichter erkennen zu können,
235 z.B. in einer Verzeichnisliste:
236  
237 $ quiq-ls ~/dvl
238 | rwxr-xr-x | xv882js | rvgroup | 2018-07-07 07:08:17 | | d | ~/dvl/.cotedo |
239 | rwxr-xr-x | xv882js | rvgroup | 2018-06-29 11:06:38 | | d | ~/dvl/.jaz |
240 | rwxr-xr-x | xv882js | rvgroup | 17 07:29:51 | | d | ~/dvl/Blob |
241 | rwxr-xr-x | xv882js | rvgroup | 17 07:29:52 | | d | ~/dvl/Export |
242 | rwxr-xr-x | xv882js | rvgroup | 17 07:29:52 | | d | ~/dvl/Language |
243 | rwxr-xr-x | xv882js | rvgroup | 17 07:29:52 | | d | ~/dvl/Library |
244 | rwxr-xr-x | xv882js | rvgroup | 37:47 | | d | ~/dvl/Package |
245  
246 =head4 Examples
247  
248 Keine gemeinsame Zeitkomponente:
249  
250 Quiq::Formatter->reducedIsoTime(1558593179,1530940097);
251 ==>
252 2018-07-07 07:08:17
253  
254 Jahr und Monat sind gemeinsam:
255  
256 Quiq::Formatter->reducedIsoTime(1558593179,1558070991);
257 ==>
258 17 07:29:51
259  
260 Alle Komponenten, bis auf die Sekunden, sind identisch:
261  
262 Quiq::Formatter->reducedIsoTime(1558593179,1558593168);
263 ==>
264 48
265  
266 (alles in Zeitzone MESZ)
267  
268 =cut
269  
270 # -----------------------------------------------------------------------------
271  
272 6 ▶sub reducedIsoTime {
Subroutine reducedIsoTime: called CC=17 SCAR=28.9
Pod documented
273 6 my ($class,$now,$time) = @_;
274  
275 6 my @now = Quiq::Epoch->new($now)->localtime;
276 6 my @time = Quiq::Epoch->new($time)->localtime;
277  
278 6 my $str = '';
279 6 ▶ if ($time[5] != $now[5]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time[5] != $now[5]) 2 4 6
280 2 $str .= $time[5];
281 }
282 6 ▶ if ($str || $time[4] != $now[4]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str or $time[4] != $now[4]) 2 4 6
Condition66%2/3
$str or $time[4] != $now[4]
l!l&&r!l&&!r
204
MC/DC50%1/2
$str or $time[4] != $now[4]
$str $time[4] != $now[4]
Truth table66%2/3
A or B
A= $strB= $time[4] != $now[4]
ABresult
1X1
011
000
283 2 ▶ $str .= sprintf '%s%02d',$str? '-': '',$time[4];
Branch: 1 branch
BranchTrueFalseTotal
$str ? : 2 0 2
284 }
285 6 ▶ if ($str || $time[3] != $now[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str or $time[3] != $now[3]) 4 2 6
Condition100%3/3
$str or $time[3] != $now[3]
l!l&&r!l&&!r
222
MC/DC100%2/2
$str or $time[3] != $now[3]
$str $time[3] != $now[3]
Truth table100%3/3
A or B
A= $strB= $time[3] != $now[3]
ABresult
1X1
011
000
286 4 ▶ $str .= sprintf '%s%02d',$str? '-': '',$time[3];
Branch: 1 branch
BranchTrueFalseTotal
$str ? : 2 2 4
287 }
288 6 ▶ if ($str || $time[2] != $now[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str or $time[2] != $now[2]) 4 2 6
Condition66%2/3
$str or $time[2] != $now[2]
l!l&&r!l&&!r
402
MC/DC50%1/2
$str or $time[2] != $now[2]
$str $time[2] != $now[2]
Truth table66%2/3
A or B
A= $strB= $time[2] != $now[2]
ABresult
1X1
011
000
289 4 ▶ $str .= sprintf '%s%02d',$str? ' ': '',$time[2];
Branch: 1 branch
BranchTrueFalseTotal
$str ? : 4 0 4
290 }
291 6 ▶ if ($str || $time[1] != $now[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str or $time[1] != $now[1]) 4 2 6
Condition66%2/3
$str or $time[1] != $now[1]
l!l&&r!l&&!r
402
MC/DC50%1/2
$str or $time[1] != $now[1]
$str $time[1] != $now[1]
Truth table66%2/3
A or B
A= $strB= $time[1] != $now[1]
ABresult
1X1
011
000
292 4 ▶ $str .= sprintf '%s%02d',$str? ':': '',$time[1];
Branch: 1 branch
BranchTrueFalseTotal
$str ? : 4 0 4
293 }
294 6 ▶ if ($str || $time[0] != $now[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str or $time[0] != $now[0]) 6 0 6
Condition66%2/3
$str or $time[0] != $now[0]
l!l&&r!l&&!r
420
MC/DC0%0/2
$str or $time[0] != $now[0]
$str $time[0] != $now[0]
Truth table66%2/3
A or B
A= $strB= $time[0] != $now[0]
ABresult
1X1
011
000
295 6 ▶ $str .= sprintf '%s%02d',$str? ':': '',$time[0];
Branch: 1 branch
BranchTrueFalseTotal
$str ? : 4 2 6
296 }
297  
298 6 return $str;
299 }
300  
301 # -----------------------------------------------------------------------------
302  
303 =head1 VERSION
304  
305 1.240
306  
307 =head1 AUTHOR
308  
309 Frank Seitz, L<http://fseitz.de/>
310  
311 =head1 COPYRIGHT
312  
313 Copyright (C) 2026 Frank Seitz
314  
315 =head1 LICENSE
316  
317 This code is free software; you can redistribute it and/or modify
318 it under the same terms as Perl itself.
319  
320 =cut
321  
322 # -----------------------------------------------------------------------------
323  
324 270 1;
325  
326 # eof
« Quiq/Assert.pm ↑ Summary Quiq/Json/Code.pm »