line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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 |
11
|
|
|
|
|
|
|
# Andy Wardley |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# COPYRIGHT |
14
|
|
|
|
|
|
|
# Copyright (C) 2000-2007 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
|
1
|
|
|
1
|
|
468
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
24
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
25
|
1
|
|
|
1
|
|
5
|
use base 'Template::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
441
|
|
26
|
|
|
|
|
|
|
|
27
|
1
|
|
|
1
|
|
1083
|
use POSIX (); |
|
1
|
|
|
|
|
7610
|
|
|
1
|
|
|
|
|
707
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our $VERSION = 2.78; |
30
|
|
|
|
|
|
|
our $FORMAT = '%H:%M:%S %d-%b-%Y'; # default strftime() format |
31
|
|
|
|
|
|
|
our @LOCALE_SUFFIX = qw( .ISO8859-1 .ISO_8859-15 .US-ASCII .UTF-8 ); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
35
|
|
|
|
|
|
|
# new(\%options) |
36
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub new { |
39
|
19
|
|
|
19
|
1
|
29
|
my ($class, $context, $params) = @_; |
40
|
19
|
100
|
|
|
|
143
|
bless { |
41
|
|
|
|
|
|
|
$params ? %$params : () |
42
|
|
|
|
|
|
|
}, $class; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
47
|
|
|
|
|
|
|
# now() |
48
|
|
|
|
|
|
|
# |
49
|
|
|
|
|
|
|
# Call time() to return the current system time in seconds since the epoch. |
50
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub now { |
53
|
5
|
|
|
5
|
0
|
17
|
return time(); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
58
|
|
|
|
|
|
|
# format() |
59
|
|
|
|
|
|
|
# format($time) |
60
|
|
|
|
|
|
|
# format($time, $format) |
61
|
|
|
|
|
|
|
# format($time, $format, $locale) |
62
|
|
|
|
|
|
|
# format($time, $format, $locale, $gmt_flag) |
63
|
|
|
|
|
|
|
# format(\%named_params); |
64
|
|
|
|
|
|
|
# |
65
|
|
|
|
|
|
|
# Returns a formatted time/date string for the specified time, $time, |
66
|
|
|
|
|
|
|
# (or the current system time if unspecified) using the $format, $locale, |
67
|
|
|
|
|
|
|
# and $gmt values specified as arguments or internal values set defined |
68
|
|
|
|
|
|
|
# at construction time). Specifying a Perl-true value for $gmt will |
69
|
|
|
|
|
|
|
# override the local time zone and force the output to be for GMT. |
70
|
|
|
|
|
|
|
# Any or all of the arguments may be specified as named parameters which |
71
|
|
|
|
|
|
|
# get passed as a hash array reference as the final argument. |
72
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub format { |
75
|
19
|
|
|
19
|
0
|
74
|
my $self = shift; |
76
|
19
|
100
|
|
|
|
68
|
my $params = ref($_[$#_]) eq 'HASH' ? pop(@_) : { }; |
77
|
|
|
|
|
|
|
my $time = shift(@_) || $params->{ time } || $self->{ time } |
78
|
19
|
|
66
|
|
|
146
|
|| $self->now(); |
79
|
|
|
|
|
|
|
my $format = @_ ? shift(@_) |
80
|
19
|
100
|
66
|
|
|
85
|
: ($params->{ format } || $self->{ format } || $FORMAT); |
81
|
|
|
|
|
|
|
my $locale = @_ ? shift(@_) |
82
|
19
|
50
|
100
|
|
|
95
|
: ($params->{ locale } || $self->{ locale }); |
83
|
|
|
|
|
|
|
my $gmt = @_ ? shift(@_) |
84
|
19
|
50
|
33
|
|
|
89
|
: ($params->{ gmt } || $self->{ gmt }); |
85
|
19
|
|
|
|
|
25
|
my (@date, $datestr); |
86
|
|
|
|
|
|
|
|
87
|
19
|
100
|
|
|
|
115
|
if ($time =~ /^-?\d+$/) { |
88
|
|
|
|
|
|
|
# $time is now in seconds since epoch |
89
|
12
|
50
|
|
|
|
27
|
if ($gmt) { |
90
|
0
|
|
|
|
|
0
|
@date = (gmtime($time))[0..6]; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
else { |
93
|
12
|
|
|
|
|
618
|
@date = (localtime($time))[0..6]; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
else { |
97
|
|
|
|
|
|
|
# if $time is numeric, then we assume it's seconds since the epoch |
98
|
|
|
|
|
|
|
# otherwise, we try to parse it as either a 'Y:M:D H:M:S' or a |
99
|
|
|
|
|
|
|
# 'H:M:S D:M:Y' string |
100
|
|
|
|
|
|
|
|
101
|
7
|
|
|
|
|
59
|
my @parts = (split(/\D/, $time)); |
102
|
|
|
|
|
|
|
|
103
|
7
|
100
|
|
|
|
22
|
if (@parts >= 6) { |
104
|
6
|
100
|
|
|
|
17
|
if (length($parts[0]) == 4) { |
105
|
|
|
|
|
|
|
# year is first; assume 'Y:M:D H:M:S' |
106
|
3
|
|
|
|
|
15
|
@date = @parts[reverse 0..5]; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
else { |
109
|
|
|
|
|
|
|
# year is last; assume 'H:M:S D:M:Y' |
110
|
3
|
|
|
|
|
12
|
@date = @parts[2,1,0,3..5]; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
7
|
100
|
|
|
|
18
|
if (!@date) { |
115
|
1
|
|
|
|
|
13
|
return (undef, Template::Exception->new('date', |
116
|
|
|
|
|
|
|
"bad time/date string: " . |
117
|
|
|
|
|
|
|
"expects 'h:m:s d:m:y' got: '$time'")); |
118
|
|
|
|
|
|
|
} |
119
|
6
|
|
|
|
|
14
|
$date[4] -= 1; # correct month number 1-12 to range 0-11 |
120
|
6
|
|
|
|
|
8
|
$date[5] -= 1900; # convert absolute year to years since 1900 |
121
|
6
|
|
|
|
|
299
|
$time = &POSIX::mktime(@date); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
18
|
100
|
|
|
|
50
|
if ($locale) { |
125
|
|
|
|
|
|
|
# format the date in a specific locale, saving and subsequently |
126
|
|
|
|
|
|
|
# restoring the current locale. |
127
|
5
|
|
|
|
|
61
|
my $old_locale = &POSIX::setlocale(&POSIX::LC_ALL); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# some systems expect locales to have a particular suffix |
130
|
5
|
|
|
|
|
16
|
for my $suffix ('', @LOCALE_SUFFIX) { |
131
|
25
|
|
|
|
|
41
|
my $try_locale = $locale.$suffix; |
132
|
25
|
|
|
|
|
1880
|
my $setlocale = &POSIX::setlocale(&POSIX::LC_ALL, $try_locale); |
133
|
25
|
50
|
33
|
|
|
78
|
if (defined $setlocale && $try_locale eq $setlocale) { |
134
|
0
|
|
|
|
|
0
|
$locale = $try_locale; |
135
|
0
|
|
|
|
|
0
|
last; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
} |
138
|
5
|
|
|
|
|
183
|
$datestr = &POSIX::strftime($format, @date); |
139
|
5
|
|
|
|
|
32
|
&POSIX::setlocale(&POSIX::LC_ALL, $old_locale); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
else { |
142
|
13
|
|
|
|
|
442
|
$datestr = &POSIX::strftime($format, @date); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
18
|
|
|
|
|
120
|
return $datestr; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub calc { |
149
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
150
|
0
|
|
|
|
|
|
eval { require "Date/Calc.pm" }; |
|
0
|
|
|
|
|
|
|
151
|
0
|
0
|
|
|
|
|
$self->throw("failed to load Date::Calc: $@") if $@; |
152
|
0
|
|
|
|
|
|
return Template::Plugin::Date::Calc->new('no context'); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub manip { |
156
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
157
|
0
|
|
|
|
|
|
eval { require "Date/Manip.pm" }; |
|
0
|
|
|
|
|
|
|
158
|
0
|
0
|
|
|
|
|
$self->throw("failed to load Date::Manip: $@") if $@; |
159
|
0
|
|
|
|
|
|
return Template::Plugin::Date::Manip->new('no context'); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub throw { |
164
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
165
|
0
|
|
|
|
|
|
die (Template::Exception->new('date', join(', ', @_))); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
package Template::Plugin::Date::Calc; |
170
|
1
|
|
|
1
|
|
8
|
use base qw( Template::Plugin ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
93
|
|
171
|
1
|
|
|
1
|
|
16
|
use vars qw( $AUTOLOAD ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
167
|
|
172
|
|
|
|
|
|
|
*throw = \&Template::Plugin::Date::throw; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub AUTOLOAD { |
175
|
0
|
|
|
0
|
|
|
my $self = shift; |
176
|
0
|
|
|
|
|
|
my $method = $AUTOLOAD; |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
$method =~ s/.*:://; |
179
|
0
|
0
|
|
|
|
|
return if $method eq 'DESTROY'; |
180
|
|
|
|
|
|
|
|
181
|
0
|
|
|
|
|
|
my $sub = \&{"Date::Calc::$method"}; |
|
0
|
|
|
|
|
|
|
182
|
0
|
0
|
|
|
|
|
$self->throw("no such Date::Calc method: $method") |
183
|
|
|
|
|
|
|
unless $sub; |
184
|
|
|
|
|
|
|
|
185
|
0
|
|
|
|
|
|
&$sub(@_); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
package Template::Plugin::Date::Manip; |
189
|
1
|
|
|
1
|
|
5
|
use base qw( Template::Plugin ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
59
|
|
190
|
1
|
|
|
1
|
|
5
|
use vars qw( $AUTOLOAD ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
156
|
|
191
|
|
|
|
|
|
|
*throw = \&Template::Plugin::Date::throw; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub AUTOLOAD { |
194
|
0
|
|
|
0
|
|
|
my $self = shift; |
195
|
0
|
|
|
|
|
|
my $method = $AUTOLOAD; |
196
|
|
|
|
|
|
|
|
197
|
0
|
|
|
|
|
|
$method =~ s/.*:://; |
198
|
0
|
0
|
|
|
|
|
return if $method eq 'DESTROY'; |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
|
|
|
|
my $sub = \&{"Date::Manip::$method"}; |
|
0
|
|
|
|
|
|
|
201
|
0
|
0
|
|
|
|
|
$self->throw("no such Date::Manip method: $method") |
202
|
|
|
|
|
|
|
unless $sub; |
203
|
|
|
|
|
|
|
|
204
|
0
|
|
|
|
|
|
&$sub(@_); |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
1; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
__END__ |