lib/Template/Plugin/Date.pm

filter: Statementstmt 68.8% 73 / 106 Branchbran 54.1% 26 / 48 Conditioncond 74.0% 20 / 27 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 70.5% 12 / 17 Podpod 16.6% 1 / 6 totaltotal 62.1% 136 / 219 CC 35 SCAR 29.7
CC
35
Cov
66%
format
32.7
AUTOLOAD
12.0
AUTOLOAD
12.0
CRAP 19.5
« lib/Template/View.pm ↑ Summary lib/Template/Parser.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::Date
4 #
5 # DESCRIPTION
6 #
7 # Plugin to generate formatted date strings.
8 #
9 # AUTHORS
10 # Thierry-Michel Barral <kktos@electron-libre.com>
11 # Andy Wardley <abw@wardley.org>
12 #
13 # COPYRIGHT
14 # Copyright (C) 2000-2022 Thierry-Michel Barral, Andy Wardley.
15 #
16 # This module is free software; you can redistribute it and/or
17 # modify it under the same terms as Perl itself.
18 #
19 #============================================================================
20  
21 package Template::Plugin::Date;
22  
23 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use base 'Template::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 3 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 3 ▶use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 3 ▶use constant HAS_SETLOCALE => $Config::Config{d_setlocale};
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 3 our $VERSION = '3.106';
34 3 our $FORMAT = '%H:%M:%S %d-%b-%Y'; # default strftime() format
35 3 our @LOCALE_SUFFIX = qw( .ISO8859-1 .ISO_8859-15 .US-ASCII .UTF-8 );
36  
37  
38 #------------------------------------------------------------------------
39 # new(\%options)
40 #------------------------------------------------------------------------
41  
42 22 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
43 22 my ($class, $context, $params) = @_;
44 22 ▶ bless {
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 12 10 22
45 $params ? %$params : ()
46 }, $class;
47 }
48  
49  
50 #------------------------------------------------------------------------
51 # now()
52 #
53 # Call time() to return the current system time in seconds since the epoch.
54 #------------------------------------------------------------------------
55  
56 5 ▶sub now {
Subroutine now: called CC=1 SCAR=0.0
Pod undocumented
57 5 return time();
58 }
59  
60 21 ▶sub _strftime {
Subroutine _strftime: called CC=2 SCAR=7.9
61 21 my ( @args ) = @_;
62  
63 21 my $str = POSIX::strftime( @args );
64  
65 # POSIX.pm now utf8-flags the output of strftime since perl 5.22
66 21 ▶ if ( $] < 5.022 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.022") 0 21 21
67 0 require Encode;
68 0 Encode::_utf8_on( $str );
69 }
70  
71 21 return $str;
72 }
73  
74 #------------------------------------------------------------------------
75 # format()
76 # format($time)
77 # format($time, $format)
78 # format($time, $format, $locale)
79 # format($time, $format, $locale, $gmt_flag)
80 # format(\%named_params);
81 #
82 # Returns a formatted time/date string for the specified time, $time,
83 # (or the current system time if unspecified) using the $format, $locale,
84 # and $gmt values specified as arguments or internal values set defined
85 # at construction time). Specifying a Perl-true value for $gmt will
86 # override the local time zone and force the output to be for GMT.
87 # Any or all of the arguments may be specified as named parameters which
88 # get passed as a hash array reference as the final argument.
89 # ------------------------------------------------------------------------
90  
91 22 ▶sub format {
Subroutine format: called CC=27 SCAR=34.9
Pod undocumented
92 22 my $self = shift;
93 22 ▶ my $params = ref($_[$#_]) eq 'HASH' ? pop(@_) : { };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[$#_] eq 'HASH' ? : 8 14 22
94  
95 22 my $time = shift(@_);
96 22 ▶ $time //= $params->{ time } || $self->{ time } || $self->now();
Condition77%7/9
$time //= $params->{"time"} || $self->{"time"} || $self->now
l!l&&r!l&&!r
12100
$params->{'time'} || $self->{'time'} || $self->now
l!l&&r!l&&!r
550
$params->{"time"} or $self->{"time"}
l!l&&r!l&&!r
235
MC/DC0%0/4
$time //= $params->{"time"} || $self->{"time"} || $self->now
$time $params->{"time"} $self->{"time"} $self->now
Truth table0%0/5
A //= B or C || D
A= $timeB= $params->{"time"}C= $self->{"time"}D= $self->now
ABCDresult
1XXX1
01XX1
001X1
00011
00000
97  
98 my $format = @_
99 ? shift(@_)
100 22 ▶ : ($params->{ format } || $self->{ format } || $FORMAT);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 8 14 22
Condition83%5/6
$params->{"format"} or $self->{"format"} or $Template::Plugin::Date::FORMAT
l!l&&r!l&&!r
1220
$params->{"format"} or $self->{"format"}
l!l&&r!l&&!r
752
MC/DC0%0/3
$params->{"format"} or $self->{"format"} or $Template::Plugin::Date::FORMAT
$params->{"format"} $self->{"format"} $Template::Plugin::Date::FORMAT
Truth table75%3/4
A or B or C
A= $params->{"format"}B= $self->{"format"}C= $Template::Plugin::Date::FORMAT
ABCresult
1XX1
01X1
0011
0000
101 my $locale = @_
102 ? shift(@_)
103 22 ▶ : ($params->{ locale } || $self->{ locale });
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 22 22
Condition100%3/3
$params->{"locale"} or $self->{"locale"}
l!l&&r!l&&!r
2317
MC/DC100%2/2
$params->{"locale"} or $self->{"locale"}
$params->{"locale"} $self->{"locale"}
Truth table100%3/3
A or B
A= $params->{"locale"}B= $self->{"locale"}
ABresult
1X1
011
000
104 my $gmt = @_
105 ? shift(@_)
106 22 ▶ : ($params->{ gmt } || $self->{ gmt });
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 22 22
Condition33%1/3
$params->{"gmt"} or $self->{"gmt"}
l!l&&r!l&&!r
0022
MC/DC0%0/2
$params->{"gmt"} or $self->{"gmt"}
$params->{"gmt"} $self->{"gmt"}
Truth table33%1/3
A or B
A= $params->{"gmt"}B= $self->{"gmt"}
ABresult
1X1
011
000
107 my $offset = @_
108 ? shift(@_)
109 22 ▶ : ( $params->{ use_offset } || $self->{ use_offset });
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 22 22
Condition100%3/3
$params->{"use_offset"} or $self->{"use_offset"}
l!l&&r!l&&!r
1219
MC/DC100%2/2
$params->{"use_offset"} or $self->{"use_offset"}
$params->{"use_offset"} $self->{"use_offset"}
Truth table100%3/3
A or B
A= $params->{"use_offset"}B= $self->{"use_offset"}
ABresult
1X1
011
000
110 22 my (@date, $datestr);
111  
112 22 ▶ if ($time =~ /^-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time =~ /^-?\d+$/) { } 12 10 22
113 # $time is now in seconds since epoch
114 12 ▶ if ($gmt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gmt) { } 0 12 12
115 0 ▶ @date = (gmtime($time))[ 0 .. ( $offset ? 6 : 8 ) ];
Branch: 1 branch
BranchTrueFalseTotal
$offset ? : 0 0 0
116 }
117 else {
118 12 ▶ @date = (localtime($time))[ 0 .. ( $offset ? 6 : 8 ) ];
Branch: 1 branch
BranchTrueFalseTotal
$offset ? : 0 12 12
119 }
120 }
121 else {
122 # if $time is numeric, then we assume it's seconds since the epoch
123 # otherwise, we try to parse it as either a 'Y:M:D H:M:S' or a
124 # 'H:M:S D:M:Y' string
125  
126 10 my @parts = (split(/\D/, $time));
127  
128 10 ▶ if (@parts >= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts >= 6) 9 1 10
129 9 ▶ if (length($parts[0]) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $parts[0] == 4) { } 6 3 9
130 # year is first; assume 'Y:M:D H:M:S'
131 6 @date = @parts[reverse 0..5];
132 }
133 else {
134 # year is last; assume 'H:M:S D:M:Y'
135 3 @date = @parts[2,1,0,3..5];
136 }
137 }
138  
139 10 ▶ if (!@date) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@date) 1 9 10
140 return (
141 undef,
142 1 Template::Exception->new(
143 'date',
144 "bad time/date string: " .
145 "expects 'h:m:s d:m:y' got: '$time'"
146 )
147 );
148 }
149 9 $date[4] -= 1; # correct month number 1-12 to range 0-11
150 9 $date[5] -= 1900; # convert absolute year to years since 1900
151 9 $time = &POSIX::mktime(@date);
152  
153 9 ▶ if ($offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) 3 6 9
154 3 ▶ push @date, $gmt
Branch: 1 branch
BranchTrueFalseTotal
$gmt ? : 0 3 3
155 ? (gmtime($time))[6..8] : (localtime($time))[6..8];
156 }
157 }
158  
159 21 ▶ if ($locale) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locale) { } 5 16 21
160 # format the date in a specific locale, saving and subsequently
161 # restoring the current locale.
162 5 my $old_locale = HAS_SETLOCALE
163 ? &POSIX::setlocale(&POSIX::LC_ALL)
164 : undef;
165  
166 # some systems expect locales to have a particular suffix
167 5 for my $suffix ('', @LOCALE_SUFFIX) {
168 25 my $try_locale = $locale.$suffix;
169 25 my $setlocale = HAS_SETLOCALE
170 ? &POSIX::setlocale(&POSIX::LC_ALL, $try_locale)
171 : undef;
172 25 ▶ if (defined $setlocale && $try_locale eq $setlocale) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $setlocale and $try_locale eq $setlocale) 0 25 25
Condition33%1/3
defined $setlocale and $try_locale eq $setlocale
!ll&&!rl&&r
2500
MC/DC0%0/2
defined $setlocale and $try_locale eq $setlocale
defined $setlocale $try_locale eq $setlocale
Truth table33%1/3
A and B
A= defined $setlocaleB= $try_locale eq $setlocale
ABresult
0X0
100
111
173 0 $locale = $try_locale;
174 0 last;
175 }
176 }
177 5 $datestr = _strftime($format, @date);
178 5 &POSIX::setlocale(&POSIX::LC_ALL, $old_locale) if HAS_SETLOCALE;
179 }
180 else {
181 16 $datestr = _strftime($format, @date);
182 }
183  
184 21 return $datestr;
185 }
186  
187 0 ▶sub calc {
Subroutine calc: not called CC=2 SCAR=17.9
Pod undocumented
188 0 my $self = shift;
189 0 eval { require "Date/Calc.pm" };
190 0 ▶ $self->throw("failed to load Date::Calc: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
191 0 return Template::Plugin::Date::Calc->new('no context');
192 }
193  
194 0 ▶sub manip {
Subroutine manip: not called CC=2 SCAR=17.9
Pod undocumented
195 0 my $self = shift;
196 0 eval { require "Date/Manip.pm" };
197 0 ▶ $self->throw("failed to load Date::Manip: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
198 0 return Template::Plugin::Date::Manip->new('no context');
199 }
200  
201  
202 0 ▶sub throw {
Subroutine throw: not called CC=1 SCAR=6.9
Pod undocumented
203 0 my $self = shift;
204 0 die (Template::Exception->new('date', join(', ', @_)));
205 }
206  
207  
208 package Template::Plugin::Date::Calc;
209 3 ▶use base qw( Template::Plugin );
Subroutine BEGIN: called CC=1 SCAR=0.0
210 3 our $AUTOLOAD;
211 3 *throw = \&Template::Plugin::Date::throw;
212  
213 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=24.8
214 0 my $self = shift;
215 0 my $method = $AUTOLOAD;
216  
217 0 $method =~ s/.*:://;
218 0 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 0 0 0
219  
220 0 my $sub = \&{"Date::Calc::$method"};
221 0 ▶ $self->throw("no such Date::Calc method: $method")
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 0 0 0
222 unless $sub;
223  
224 0 &$sub(@_);
225 }
226  
227 package Template::Plugin::Date::Manip;
228 3 ▶use base qw( Template::Plugin );
Subroutine BEGIN: called CC=1 SCAR=0.0
229 3 our $AUTOLOAD;
230 3 *throw = \&Template::Plugin::Date::throw;
231  
232 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=24.8
233 0 my $self = shift;
234 0 my $method = $AUTOLOAD;
235  
236 0 $method =~ s/.*:://;
237 0 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 0 0 0
238  
239 0 my $sub = \&{"Date::Manip::$method"};
240 0 ▶ $self->throw("no such Date::Manip method: $method")
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 0 0 0
241 unless $sub;
242  
243 0 &$sub(@_);
244 }
245  
246  
247 3 1;
248  
249 __END__
« lib/Template/View.pm ↑ Summary lib/Template/Parser.pm »