blib/lib/Time/Format.pm

filter: Statementstmt 88.8% 104 / 117 Branchbran 64.8% 35 / 54 Conditioncond 46.6% 7 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 92.8% 13 / 14 Podpod 100% 2 / 2 totaltotal 77.2% 163 / 211 CC 26 SCAR 18.1
CC
26
Cov
78%
import
12.9
__ANON__
6.0
_have
5.0
CRAP 6.1
↑ Summary
1 =for gpg
2 -----BEGIN PGP SIGNED MESSAGE-----
3 Hash: SHA1
4
5 =encoding utf8
6
7 =head1 NAME
8
9 Time::Format - Easy-to-use date/time formatting.
10
11 =head1 VERSION
12
13 This is version 1.16 of Time::Format, February 19, 2020.
14
15 =cut
16
17 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 package Time::Format;
19 25 $Time::Format::VERSION = '1.16';
20
21 # This module claims to be compatible with the following versions
22 # of Time::Format_XS.
23 25 %Time::Format::XSCOMPAT = map {$_ => 1} qw(1.01 1.02 1.03);
24
25 sub _croak
26 2 ▶{
Subroutine _croak: called CC=1 SCAR=0.0
27 2 require Carp;
28 2 goto &Carp::croak;
29 }
30
31 # Store the file offset of the __DATA__ region.
32 25 my $data_pos = tell DATA;
33 25 close DATA; # so we don't hold a lock on this file.
34
35 # Here we go through a bunch of tests to decide whether we can use the
36 # XS module, or if we need to load and compile the perl-only
37 # subroutines (which are stored in __DATA__).
38 25 my $load_perlonly = 0;
39 25 ▶$load_perlonly = 1 if defined $Time::Format::NOXS && $Time::Format::NOXS;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Time::Format::NOXS and $Time::Format::NOXS 17 8 25
Condition66%2/3
defined $Time::Format::NOXS and $Time::Format::NOXS
!ll&&!rl&&r
8017
MC/DC50%1/2
defined $Time::Format::NOXS and $Time::Format::NOXS
defined $Time::Format::NOXS $Time::Format::NOXS
Truth table66%2/3
A and B
A= defined $Time::Format::NOXSB= $Time::Format::NOXS
ABresult
0X0
100
111
40
41 25 ▶if (!$load_perlonly)
Branch: 1 branch
BranchTrueFalseTotal
unless ($load_perlonly) 8 17 25
42 {
43 # Check whether the optional XS module is installed.
44 8 eval { require Time::Format_XS };
45
46 8 ▶ if ($@ || !defined $Time::Format_XS::VERSION)
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $Time::Format_XS::VERSION) { } 8 0 8
Condition33%1/3
$@ or not defined $Time::Format_XS::VERSION
l!l&&r!l&&!r
800
MC/DC0%0/2
$@ or not defined $Time::Format_XS::VERSION
$@ not defined $Time::Format_XS::VERSION
Truth table33%1/3
A or not(B)
A= $@B= not defined $Time::Format_XS::VERSION
ABresult
1X1
011
000
47 {
48 8 $load_perlonly = 1;
49 }
50 else
51 {
52 # Check that we're compatible with them (backwards compatibility)
53 # or they're compatible with us (forwards compatibility).
54 0 ▶ unless ($Time::Format::XSCOMPAT{$Time::Format_XS::VERSION}
Branch: 1 branch
BranchTrueFalseTotal
unless ($Time::Format::XSCOMPAT{$Time::Format_XS::VERSION} or $Time::Format_XS::PLCOMPAT{$Time::Format::VERSION}) 0 0 0
Condition0%0/3
$Time::Format::XSCOMPAT{$Time::Format_XS::VERSION} or $Time::Format_XS::PLCOMPAT{$Time::Format::VERSION}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Time::Format::XSCOMPAT{$Time::Format_XS::VERSION} or $Time::Format_XS::PLCOMPAT{$Time::Format::VERSION}
$Time::Format::XSCOMPAT{$Time::Format_XS::VERSION} $Time::Format_XS::PLCOMPAT{$Time::Format::VERSION}
Truth table0%0/3
A or B
A= $Time::Format::XSCOMPAT{$Time::Format_XS::VERSION}B= $Time::Format_XS::PLCOMPAT{$Time::Format::VERSION}
ABresult
1X1
011
000
55 || $Time::Format_XS::PLCOMPAT{$Time::Format::VERSION})
56 {
57 0 warn "Your Time::Format_XS version ($Time::Format_XS::VERSION) "
58 . "is not compatible with Time::Format version ($Time::Format::VERSION).\n"
59 . "Using Perl-only functions.\n";
60 0 $load_perlonly = 1;
61 }
62 }
63
64 # Okay to use the XS version? Great. Wrap it.
65 8 ▶ if (!$load_perlonly)
Branch: 1 branch
BranchTrueFalseTotal
unless ($load_perlonly) 0 8 8
66 {
67 0 ▶ *time_format = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
68 0 my ($fmt, $t) = @_;
69 0 ▶ $t = 'time' if not defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 0 0 0
70 0 @_ = ($fmt, $t);
71 0 goto &Time::Format_XS::time_format;
72 0 };
73 }
74 }
75
76 25 ▶if ($load_perlonly)
Branch: 1 branch
BranchTrueFalseTotal
if ($load_perlonly) 25 0 25
77 {
78 # Time::Format_XS not installed, or version mismatch, or NOXS was set.
79 # The perl routines will need to be loaded.
80 # But let's defer this until someone actually calls time_format().
81 *time_format = sub
82 309 ▶ {
Subroutine __ANON__: called CC=4 SCAR=13.9
83 309 ▶ if (not defined &time_format_perlonly)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Time::Format::time_format_perlonly) 12 297 309
84 {
85 12 ▶ open DATA, '<', __FILE__
Branch: 1 branch
BranchTrueFalseTotal
open DATA, "<", __FILE__ or die q[Can't access code in /home/cpancover/.cpan/build/Time-Format-1.16-0/blib/lib/Time/Format.pm] . ": $!\n" 12 0 12
86 or die "Can't access code in " . __FILE__ . ": $!\n";;
87
88 12 flock DATA, 1; # LOCK_SH
89 12 seek DATA, $data_pos, 0;
90 12 local $^W = 0; # disable warning about subroutines redefined
91 12 local $/ = undef; # slurp
92 12 my $code = <DATA>;
93 12 flock DATA, 8; # LOCK_UN
94 12 close DATA;
95
96 12 eval $code;
97 12 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
98 }
99 309 *time_format = \&time_format_perlonly;
100 309 goto &time_format_perlonly;
101 25 };
102 25 undef $Time::Format_XS::VERSION; # Indicate that XS version is not available.
103 }
104
105
106 25 my @EXPORT = qw(%time time_format);
107 25 my @EXPORT_OK = qw(%time %strftime %manip time_format time_strftime time_manip);
108
109 # We don't need any of Exporter's fancy features, so it's quicker to
110 # do the import ourselves.
111 sub import
112 25 ▶{
Subroutine import: called CC=11 SCAR=25.6
113 25 my $pkg = shift;
114 25 my ($cpkg,$file,$line) = caller;
115 25 my @symbols;
116 25 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 19 6 25
117 {
118 19 ▶ if (grep $_ eq ':all', @_)
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($_ eq ':all'), @_)) { } 5 14 19
119 {
120 5 @symbols = (@EXPORT, @EXPORT_OK, grep $_ ne ':all', @_);
121 } else {
122 14 @symbols = @_;
123 }
124 19 my %seen;
125 19 @symbols = grep !$seen{$_}++, @symbols;
126 } else {
127 6 @symbols = @EXPORT;
128 }
129 25 my %ok;
130 25 @ok{@EXPORT_OK,@EXPORT} = ();
131 25 my @badsym = grep !exists $ok{$_}, @symbols;
132 25 ▶ if (@badsym)
Branch: 1 branch
BranchTrueFalseTotal
if (@badsym) 0 25 25
133 {
134 0 ▶ my $s = @badsym>1? 's' : '';
Branch: 1 branch
BranchTrueFalseTotal
@badsym > 1 ? : 0 0 0
135 0 ▶ my $v = @badsym>1? 'are' : 'is';
Branch: 1 branch
BranchTrueFalseTotal
@badsym > 1 ? : 0 0 0
136 0 _croak ("The symbol$s ", join(', ', @badsym), " $v not exported by Time::Format at $file line $line.\n");
137 }
138
139 25 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
140 25 foreach my $sym (@symbols)
141 {
142 66 $sym =~ s/^([\$\&\@\%])?//;
143 66 ▶ my $pfx = $1 || '&';
Condition100%2/2
$1 || '&'
l!l
3531
MC/DC100%1/1
$1 || '&'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
144 66 my $calsym = $cpkg . '::' . $sym;
145 66 my $mysym = $pkg . '::' . $sym;
146 66 ▶ if ($pfx eq '%')
Branch: 3 branches
BranchTrueFalseTotal
if ($pfx eq '%') { } 35 31 66
elsif ($pfx eq '@') { } 0 31 31
elsif ($pfx eq '$') { } 0 31 31
147 {
148 35 *$calsym = \%$mysym;
149 } elsif ($pfx eq '@') {
150 0 *$calsym = \@$mysym;
151 } elsif ($pfx eq '$') {
152 0 *$calsym = \$$mysym;
153 } else {
154 31 *$calsym = \&$mysym;
155 }
156 }
157 }
158
159 # Simple tied-hash implementation.
160
161 # Each hash is simply tied to a subroutine reference. "Fetching" a
162 # value from the hash invokes the subroutine. If a hash (tied or
163 # otherwise) has multiple comma-separated values but the leading
164 # character is a $, then Perl joins the values with $;. This makes it
165 # easy to simulate function calls with tied hashes -- we just split on
166 # $; to recreate the argument list.
167 #
168 # 2005/12/01: We must ensure that time_format gets two arguments, since
169 # the XS version cannot handle variable argument lists.
170
171 25 ▶use vars qw(%time %strftime %manip);
Subroutine BEGIN: called CC=1 SCAR=0.0
172 25 tie %time, 'Time::Format', \&time_format;
173 25 tie %strftime, 'Time::Format', \&time_strftime;
174 25 tie %manip, 'Time::Format', \&time_manip;
175
176 sub TIEHASH
177 75 ▶{
Subroutine TIEHASH: called CC=2 SCAR=7.1
178 75 my $class = shift;
179 75 ▶ my $func = shift || die "Bad call to $class\::TIEHASH";
Condition50%1/2
shift() || die("Bad call to ${class}::TIEHASH")
l!l
750
MC/DC0%0/1
shift() || die("Bad call to ${class}::TIEHASH")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
180 75 bless $func, $class;
181 }
182
183 sub FETCH
184 272 ▶{
Subroutine FETCH: called CC=1 SCAR=0.0
185 272 my $self = shift;
186 272 my $key = shift;
187 272 my @args = split $;, $key, -1;
188 272 $self->(@args);
189 }
190
191 25 ▶use subs qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
192 25 STORE EXISTS CLEAR FIRSTKEY NEXTKEY );
193 *STORE = *EXISTS = *CLEAR = *FIRSTKEY = *NEXTKEY = sub
194 2 ▶{
Subroutine __ANON__: called CC=1 SCAR=0.0
195 2 my ($pkg,$file,$line) = caller;
196 2 _croak "Invalid call to Time::Format internal function at $file line $line.";
197 25 };
198
199
200 # Module finder -- do we have the specified module available?
201 {
202 25 my %have;
203 sub _have
204 473 ▶ {
Subroutine _have: called CC=5 SCAR=16.2
205 473 ▶ my $module = shift || return;
Condition50%1/2
shift() || (return)
l!l
4730
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
206 473 ▶ return $have{$module} if exists $have{$module};
Branch: 1 branch
BranchTrueFalseTotal
if exists $have{$module} 453 20 473
207
208 20 my $incmod = $module;
209 20 $incmod =~ s!::!/!g;
210 20 ▶ return $have{$module} = 1 if exists $INC{"$incmod.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $INC{"$incmod.pm"} 6 14 20
211
212 14 $@ = '';
213 14 eval "require $module";
214 14 ▶ return $have{$module} = $@? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 14 14
215 }
216 }
217
218
219 # POSIX strftime, for people who like those weird % formats.
220 sub time_strftime
221 25 ▶{
Subroutine time_strftime: called CC=4 SCAR=13.9
Pod documented
222 # Check if POSIX is available (why wouldn't it be?)
223 15 ▶ return 'NO_POSIX' unless _have('POSIX');
Branch: 1 branch
BranchTrueFalseTotal
unless _have("POSIX") 0 15 15
224
225 15 my $fmt = shift;
226 15 my @time;
227
228 # If more than one arg, assume they're doing the whole arg list
229 15 ▶ if (@_ > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 1 14 15
230 {
231 1 @time = @_;
232 }
233 else # use unix time (current or passed)
234 {
235 14 ▶ my $time = @_? shift : time;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 12 2 14
236 14 @time = localtime $time;
237 }
238
239 15 return POSIX::strftime($fmt, @time);
240 }
241
242
243 # Date::Manip interface
244 sub time_manip
245 14 ▶{
Subroutine time_manip: called CC=4 SCAR=13.9
Pod documented
246 14 ▶ return "NO_DATEMANIP" unless _have('Date::Manip');
Branch: 1 branch
BranchTrueFalseTotal
unless _have("Date::Manip") 0 14 14
247
248 14 my $fmt = shift;
249 14 ▶ my $time = @_? shift : 'now';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 13 1 14
250
251 14 ▶ $time = $1 if $time =~ /^\s* (epoch \s+ \d+)/x;
Branch: 1 branch
BranchTrueFalseTotal
if $time =~ /^\s* (epoch \s+ \d+)/x 2 12 14
252
253 14 return Date::Manip::UnixDate($time, $fmt);
254 }
255
256
257 25 1;
258 __DATA__
↑ Summary