blib/lib/Time/Stamp.pm

filter: Statementstmt 100% 85 / 85 Branchbran 74.0% 80 / 108 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 27 / 27 Podpod n/a totaltotal 86.5% 206 / 238 CC 85 SCAR 13.4
CC
85
Cov
85%
_generate_code
8.2
__ANON__
7.0
_format
5.0
CRAP 3.8
↑ Summary
1 # vim: set sw=2 sts=2 ts=2 expandtab smarttab:
2 #
3 # This file is part of Time-Stamp
4 #
5 # This software is copyright (c) 2011 by Randy Stauner.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package Time::Stamp;
14 {
15 9 $Time::Stamp::VERSION = '1.300';
16 }
17 # git description: v1.200-5-gd4b4217
18  
19 12 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 $Time::Stamp::AUTHORITY = 'cpan:RWSTAUNER';
21 }
22 # ABSTRACT: Easy, readable, efficient timestamp functions
23  
24 # TODO: use collector?
25  
26 9 ▶use Sub::Exporter 0.982 -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
27 -as => 'do_import',
28 exports => [
29 localstamp => \'_build_localstamp',
30 gmstamp => \'_build_gmstamp',
31 parsegm => \'_build_parsestamp',
32 parselocal => \'_build_parsestamp',
33 ],
34 groups => [
35 stamps => [qw(localstamp gmstamp)],
36 parsers => [qw(parselocal parsegm)],
37 ]
38 12 };
39  
40 27 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
41 @_ = map {
42 24 /(local|gm)(?:stamp)?((?:-\w+)+)/
43 ? ($1.'stamp' => {
44 map {
45 9 ▶ /^([um]s)$/ ? ($1 => 1)
Branch: 1 branch
BranchTrueFalseTotal
/^([um]s)$/ ? : 6 21 27
46 : (format => $_)
47 }
48 150 ▶ grep { $_ }
Branch: 1 branch
BranchTrueFalseTotal
/(local|gm)(?:stamp)?((?:-\w+)+)/ ? : 13 156 169
49 split(/-/, $2)
50 })
51 : $_
52 } @_;
53 27 goto &do_import;
54 }
55  
56 # set up named formats with default values
57 9 my $formats = do {
58 # should we offer { prefix => '', suffix => '' } ? is that really useful?
59 # the stamps are easy enough to parse as is (the whole point of this module)
60 9 my %default = (
61 date_sep => '-',
62 dt_sep => 'T', # ISO 8601
63 time_sep => ':',
64 tz_sep => '',
65 tz => '',
66 );
67 6 my %blank = map { $_ => '' } keys %default;
68 6 my $n = {
69 default => {%default},
70 easy => {%default, dt_sep => ' ', tz_sep => ' '}, # easier to read
71 numeric => {%blank},
72 compact => {
73 %blank,
74 dt_sep => '_', # visual separation
75 },
76 };
77 # aliases
78 6 $n->{$_} = $n->{default} for qw(iso8601 rfc3339 w3cdtf);
79 6 $n;
80 };
81  
82 # we could offer a separate format_time_array() but currently
83 # I think the gain would be less than the cost of the extra function call:
84 # sub _build { return sub { format_time_array($arg, @_ or localtime) }; }
85 # sub format_time_array { sprintf(_format(shift), _ymdhms(@_)) }
86  
87 35 ▶sub _build_localstamp {
Subroutine _build_localstamp: called CC=1 SCAR=0.0
88 ##my ( $class, $name, $arg, $col ) = @_;
89 35 my ( undef, undef, $arg, undef ) = @_;
90  
91 35 return _generate_code(local => $arg);
92 }
93  
94 34 ▶sub _build_gmstamp {
Subroutine _build_gmstamp: called CC=3 SCAR=11.0
95 ##my ( $class, $name, $arg, $col ) = @_;
96 34 my ( undef, undef, $arg, undef ) = @_;
97  
98 # add the Z for UTC (Zulu) time zone unless the numeric format is requested
99 $arg = {tz => 'Z', %$arg}
100 35 ▶ unless $arg->{format} && $arg->{format} eq 'numeric';
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->{"format"} and $arg->{"format"} eq "numeric" 30 4 34
Condition100%3/3
$arg->{"format"} and $arg->{"format"} eq "numeric"
!ll&&!rl&&r
15132
MC/DC100%2/2
$arg->{"format"} and $arg->{"format"} eq "numeric"
$arg->{"format"} $arg->{"format"} eq "numeric"
Truth table100%3/3
A and B
A= $arg->{"format"}B= $arg->{"format"} eq "numeric"
ABresult
0X0
100
111
101  
102 36 return _generate_code(gm => $arg);
103 }
104  
105 # TODO: could these subs be faster with a no_args option? would only save 2 if's
106 65 ▶sub _generate_code {
Subroutine _generate_code: called CC=7 SCAR=21.0
107 67 my ($which, $arg) = @_;
108 69 $arg = { %$arg };
109 # note: mu is 03BC
110 71 ▶ $arg->{frac} ||= $arg->{us} ? 6 : $arg->{ms} ? 3 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$arg->{'us'} ? : 7 49 56
$arg->{'ms'} ? : 3 47 50
Condition100%3/3
$arg->{"frac"} ||= $arg->{"us"} ? 6 : ($arg->{"ms"} ? 3 : 0)
l!l&&r!l&&!r
9844
MC/DC100%2/2
$arg->{"frac"} ||= $arg->{"us"} ? 6 : ($arg->{"ms"} ? 3 : 0)
$arg->{"frac"} $arg->{"us"} ? 6 : ($arg->{"ms"} ? 3 : 0)
Truth table100%3/3
A ||= B
A= $arg->{"frac"}B= $arg->{"us"} ? 6 : ($arg->{"ms"} ? 3 : 0)
ABresult
1X1
011
000
111  
112 65 my $format = _format($arg);
113  
114 62 my $code;
115 62 my $vars = {
116 which => $which,
117 };
118 62 ▶ if( $arg->{frac} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{'frac'}) { } 18 46 64
119 18 $vars->{frac} = $arg->{frac};
120 # always display a fraction if requested
121 18 ▶ $vars->{gettime} = _have_hires()
Branch: 1 branch
BranchTrueFalseTotal
_have_hires() ? : 16 5 21
122 # gettimeofday() returns microseconds, so we need six digits to stringify
123 # which means we need a sprintf somewhere
124 ? 'do { my @t = Time::HiRes::gettimeofday(); $t[1] = sprintf "%06d", $t[1]; @t }'
125 # if HiRes fails to load use whole number precision
126 : '(time(), 0)';
127  
128 18 $code = <<'CODE';
129 sub {
130 # localtime() will not preserve the fraction, so separate it
131 my ($t, $f) = @_ ? (split(/\./, $_[0]), 0) : {{gettime}};
132  
133 my @lt = _ymdhms(@_ > 1 ? @_ : {{which}}time($t));
134  
135 # use %.6f for precision, but strip leading zero
136 return sprintf($format, @lt, substr(sprintf('%.{{frac}}f', '.'.$f), 1));
137 };
138 CODE
139 }
140 # if not using fraction return a more efficient sub
141 else {
142 45 $code = <<'CODE';
143 sub {
144 return sprintf($format,
145 _ymdhms(@_ > 1 ? @_ : {{which}}time(@_ ? $_[0] : time))
146 );
147 };
148 CODE
149 }
150 # poor man's template (easier than sprintf or escaping sigils)
151 62 $code =~ s/\{\{(\w+)\}\}/$vars->{$1}/g;
152  
153 62 ▶ return do { eval $code or die $@ }; ## no critic (StringyEval)
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Subroutine __ANON__: called CC=3 SCAR=12.5
Branch: 35 branches
BranchTrueFalseTotal
eval $code or die $@ 62 2 64
@_ > 1 ? : 1 4 5
@_ ? : 1 3 4
@_ > 1 ? : 1 4 5
@_ ? : 1 3 4
@_ ? : 1 3 4
@_ > 1 ? : 1 2 3
@_ > 1 ? : 0 1 1
@_ ? : 0 1 1
@_ ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ ? : 0 1 1
@_ ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ ? : 0 1 1
@_ > 1 ? : 0 1 1
@_ ? : 0 1 1
154 }
155  
156 31 ▶sub _build_parsestamp {
Subroutine _build_parsestamp: called CC=3 SCAR=11.0
157 ##my ($class, $name, $arg, $col) = @_;
158 28 my ( undef, $name, $arg, undef ) = @_;
159  
160 # pre-compile the regexp
161 my $regexp = exists $arg->{regexp}
162 28 ▶ ? qr/$arg->{regexp}/
Branch: 1 branch
BranchTrueFalseTotal
exists $arg->{'regexp'} ? : 7 24 31
163 : qr/^ (\d{4}) \D* (\d{2}) \D* (\d{2}) \D*
164 (\d{2}) \D* (\d{2}) \D* (\d{2}) (?:\.(\d+))? .* $/x;
165  
166 28 require Time::Local; # core
167 28 ▶ my $time = $name eq 'parsegm'
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'parsegm' ? : 15 15 30
168 ? \&Time::Local::timegm
169 : \&Time::Local::timelocal;
170  
171 88 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
172 85 my ($stamp) = @_;
173 85 my ($frac, @time) = reverse ($stamp =~ $regexp);
174  
175 # if the regexp didn't match (empty list) give up now
176 return
177 85 ▶ if !@time;
Branch: 1 branch
BranchTrueFalseTotal
unless @time 4 84 88
178  
179 # regexp didn't have 7th capture group (for fraction)
180 83 ▶ if( @time < 6 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@time < 6) 9 77 86
181 9 unshift @time, $frac;
182 # if there was a fraction in group 6 separate it
183 # or timelocal may produce something unexpected.
184 # if there was no fraction $frac will be undef
185 9 ($time[0], $frac) = split(/\./, $time[0]);
186 }
187  
188 # coerce strings into numbers (map { int } would not work for fractions)
189 83 @time = map { $_ + 0 } @time;
190  
191 83 $time[5] -= 1900; # year
192 83 $time[4] -= 1; # month
193  
194 # make sure it starts with a dot (whether it has one or not)
195 83 ▶ $frac =~ s/^0?\.?/./
Branch: 1 branch
BranchTrueFalseTotal
if defined $frac 38 48 86
196 if defined $frac;
197  
198 83 ▶ if( wantarray ){
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 22 64 86
199 22 ▶ $time[0] .= $frac
Branch: 1 branch
BranchTrueFalseTotal
if defined $frac 9 16 25
200 if defined $frac;
201 22 return @time;
202 }
203 else {
204 62 my $ts = &$time(@time);
205 62 ▶ $ts .= $frac
Branch: 1 branch
BranchTrueFalseTotal
if defined $frac 29 35 64
206 if defined $frac;
207 62 return $ts;
208 }
209 28 };
210 }
211  
212 98 ▶sub _format {
Subroutine _format: called CC=5 SCAR=16.1
213 95 my ($arg) = @_;
214  
215 95 ▶ my $name = $arg->{format} || ''; # avoid undef
Condition100%2/2
$arg->{'format'} || ''
l!l
5737
MC/DC100%1/1
$arg->{'format'} || ''
$arg->{'format'}
Truth table100%2/2
A
A= $arg->{'format'}
Aresult
11
00
216 # we could return $arg->{format} unless exists $formats->{$name}; warn if no % found?
217 # or just return $arg->{sprintf} if exists $arg->{sprintf};
218 $name = 'default'
219 95 ▶ unless exists $formats->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $formats->{$name} 42 56 98
220  
221 # start with named format, overwrite with any explicitly specified options
222 95 my %opt = (%{ $formats->{$name} }, %$arg);
223  
224 # TODO: $opt{tz} = tz_offset() if $opt{guess_tz};
225  
226 # sadly "%02.6f" does not zero-pad the integer portion, so we have to be trickier
227  
228 return
229 join($opt{date_sep}, qw(%04d %02d %02d)) .
230 $opt{dt_sep} .
231 join($opt{time_sep}, qw(%02d %02d %02d)) .
232 ($opt{frac} ? '%s' : '') .
233 95 ▶ ($opt{tz} ? $opt{tz_sep} . $opt{tz} : '')
Branch: 2 branches
BranchTrueFalseTotal
$opt{'frac'} ? : 29 68 97
$opt{'tz'} ? : 45 53 98
234 ;
235 }
236  
237 # convert *time() arrays to something ready to send to sprintf
238 85 ▶sub _ymdhms {
Subroutine _ymdhms: called CC=1 SCAR=0.0
239 85 return ($_[5] + 1900, $_[4] + 1, @_[3, 2, 1, 0]);
240 }
241  
242 6 my $_have_hires;
243 15 ▶sub _have_hires {
Subroutine _have_hires: called CC=3 SCAR=11.1
244 17 ▶ if( !defined($_have_hires) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_have_hires) 3 14 17
245 5 local $@;
246 5 ▶ $_have_hires = eval { require Time::HiRes; 1 } || do {
Condition33%1/3
eval { do { require Time::HiRes; 1 } } || do { warn "Time::HiRes requested but failed to load: $@"; 0 }
l!l&&r!l&&!r
200
MC/DC0%0/2
eval { do { require Time::HiRes; 1 } } || do { warn "Time::HiRes requested but failed to load: $@"; 0 }
eval { do { require Time::HiRes; 1 } } do { warn "Time::HiRes requested but failed to load: $@"; 0 }
Truth table33%1/3
A || B
A= eval { do { require Time::HiRes; 1 } }B= do { warn "Time::HiRes requested but failed to load: $@"; 0 }
ABresult
1X1
011
000
247 11 warn "Time::HiRes requested but failed to load: $@";
248 3 0;
249 };
250 }
251 17 return $_have_hires;
252 }
253  
254 # define default localstamp and gmstamp in this package
255 # so that exporting is not strictly required
256 6 __PACKAGE__->import(qw(
257 localstamp
258 gmstamp
259 parsegm
260 parselocal
261 ));
262  
263 6 1;
264  
265 __END__
↑ Summary