blib/lib/Template/Plugin/Date.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 226 CC 29 SCAR 53.2
CC
29
Cov
0%
format
462.0
AUTOLOAD
12.0
AUTOLOAD
12.0
CRAP 205.2
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 use strict;
24 use warnings;
25 use base 'Template::Plugin';
26  
27 use POSIX ();
28  
29 use Config ();
30  
31 use constant HAS_SETLOCALE => $Config::Config{d_setlocale};
32  
33 our $VERSION = '3.100';
34 our $FORMAT = '%H:%M:%S %d-%b-%Y'; # default strftime() format
35 our @LOCALE_SUFFIX = qw( .ISO8859-1 .ISO_8859-15 .US-ASCII .UTF-8 );
36  
37  
38 #------------------------------------------------------------------------
39 # new(\%options)
40 #------------------------------------------------------------------------
41  
42 sub new {
43 my ($class, $context, $params) = @_;
44 bless {
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 sub now {
57 return time();
58 }
59  
60 sub _strftime {
61 my ( @args ) = @_;
62  
63 my $str = POSIX::strftime( @args );
64  
65 # POSIX.pm now utf8-flags the output of strftime since perl 5.22
66 if ( $] < 5.022 ) {
67 require Encode;
68 Encode::_utf8_on( $str );
69 }
70  
71 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 sub format {
92 my $self = shift;
93 my $params = ref($_[$#_]) eq 'HASH' ? pop(@_) : { };
94  
95 my $time = shift(@_);
96 $time = $params->{ time } || $self->{ time } || $self->now() if !defined $time;
97  
98 my $format = @_
99 ? shift(@_)
100 : ($params->{ format } || $self->{ format } || $FORMAT);
101 my $locale = @_
102 ? shift(@_)
103 : ($params->{ locale } || $self->{ locale });
104 my $gmt = @_
105 ? shift(@_)
106 : ($params->{ gmt } || $self->{ gmt });
107 my $offset = @_
108 ? shift(@_)
109 : ( $params->{ use_offset } || $self->{ use_offset });
110 my (@date, $datestr);
111  
112 if ($time =~ /^-?\d+$/) {
113 # $time is now in seconds since epoch
114 if ($gmt) {
115 @date = (gmtime($time))[ 0 .. ( $offset ? 6 : 8 ) ];
116 }
117 else {
118 @date = (localtime($time))[ 0 .. ( $offset ? 6 : 8 ) ];
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 my @parts = (split(/\D/, $time));
127  
128 if (@parts >= 6) {
129 if (length($parts[0]) == 4) {
130 # year is first; assume 'Y:M:D H:M:S'
131 @date = @parts[reverse 0..5];
132 }
133 else {
134 # year is last; assume 'H:M:S D:M:Y'
135 @date = @parts[2,1,0,3..5];
136 }
137 }
138  
139 if (!@date) {
140 return (
141 undef,
142 Template::Exception->new(
143 'date',
144 "bad time/date string: " .
145 "expects 'h:m:s d:m:y' got: '$time'"
146 )
147 );
148 }
149 $date[4] -= 1; # correct month number 1-12 to range 0-11
150 $date[5] -= 1900; # convert absolute year to years since 1900
151 $time = &POSIX::mktime(@date);
152  
153 if ($offset) {
154 push @date, $gmt
155 ? (gmtime($time))[6..8] : (localtime($time))[6..8];
156 }
157 }
158  
159 if ($locale) {
160 # format the date in a specific locale, saving and subsequently
161 # restoring the current locale.
162 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 for my $suffix ('', @LOCALE_SUFFIX) {
168 my $try_locale = $locale.$suffix;
169 my $setlocale = HAS_SETLOCALE
170 ? &POSIX::setlocale(&POSIX::LC_ALL, $try_locale)
171 : undef;
172 if (defined $setlocale && $try_locale eq $setlocale) {
173 $locale = $try_locale;
174 last;
175 }
176 }
177 $datestr = _strftime($format, @date);
178 &POSIX::setlocale(&POSIX::LC_ALL, $old_locale) if HAS_SETLOCALE;
179 }
180 else {
181 $datestr = _strftime($format, @date);
182 }
183  
184 return $datestr;
185 }
186  
187 sub calc {
188 my $self = shift;
189 eval { require "Date/Calc.pm" };
190 $self->throw("failed to load Date::Calc: $@") if $@;
191 return Template::Plugin::Date::Calc->new('no context');
192 }
193  
194 sub manip {
195 my $self = shift;
196 eval { require "Date/Manip.pm" };
197 $self->throw("failed to load Date::Manip: $@") if $@;
198 return Template::Plugin::Date::Manip->new('no context');
199 }
200  
201  
202 sub throw {
203 my $self = shift;
204 die (Template::Exception->new('date', join(', ', @_)));
205 }
206  
207  
208 package Template::Plugin::Date::Calc;
209 use base qw( Template::Plugin );
210 our $AUTOLOAD;
211 *throw = \&Template::Plugin::Date::throw;
212  
213 sub AUTOLOAD {
214 my $self = shift;
215 my $method = $AUTOLOAD;
216  
217 $method =~ s/.*:://;
218 return if $method eq 'DESTROY';
219  
220 my $sub = \&{"Date::Calc::$method"};
221 $self->throw("no such Date::Calc method: $method")
222 unless $sub;
223  
224 &$sub(@_);
225 }
226  
227 package Template::Plugin::Date::Manip;
228 use base qw( Template::Plugin );
229 our $AUTOLOAD;
230 *throw = \&Template::Plugin::Date::throw;
231  
232 sub AUTOLOAD {
233 my $self = shift;
234 my $method = $AUTOLOAD;
235  
236 $method =~ s/.*:://;
237 return if $method eq 'DESTROY';
238  
239 my $sub = \&{"Date::Manip::$method"};
240 $self->throw("no such Date::Manip method: $method")
241 unless $sub;
242  
243 &$sub(@_);
244 }
245  
246  
247 1;
248  
249 __END__