File Coverage

blib/lib/Template/Plugin/TimeDate.pm
Criterion Covered Total %
statement 39 41 95.1
branch 4 4 100.0
condition n/a
subroutine 12 13 92.3
pod 6 6 100.0
total 61 64 95.3


line stmt bran cond sub pod time code
1             package Template::Plugin::TimeDate;
2              
3             ###############################################################################
4             # Required inclusions.
5             ###############################################################################
6 1     1   56903 use strict;
  1         3  
  1         39  
7 1     1   6 use warnings;
  1         3  
  1         29  
8 1     1   1176 use Date::Parse qw();
  1         8879  
  1         25  
9 1     1   879 use Date::Format qw();
  1         2988  
  1         32  
10 1     1   9 use overload '""' => \&stringify;
  1         2  
  1         12  
11              
12             ###############################################################################
13             # Version number.
14             ###############################################################################
15             our $VERSION = '1.02';
16              
17             ###############################################################################
18             # Derive ourselves from our base class.
19             ###############################################################################
20 1     1   937 use Template::Plugin;
  1         627  
  1         28  
21 1     1   6 use base qw( Template::Plugin );
  1         2  
  1         374  
22              
23             ###############################################################################
24             # Subroutine: new(string)
25             ###############################################################################
26             # Creates a new TimeDate plug-in object, returning it to the caller. An
27             # optional date/time string may be passed in, which is parsed automatically.
28             ###############################################################################
29             sub new {
30 11     11 1 110150 my ($class, $context, $string) = @_;
31 11         30 my $self = {};
32 11         39 bless $self, $class;
33 11 100       46 if ($string) {
34 1         4 $self->parse( $string );
35             }
36 11         29 return $self;
37             }
38              
39             ###############################################################################
40             # Subroutine: now
41             ###############################################################################
42             # Sets the current time to "now", and returns it as "the number of seconds
43             # since the epoch".
44             ###############################################################################
45             sub now {
46 2     2 1 39 my $self = shift;
47 2         104 $self->{'epoch'} = time();
48 2         11 return $self->{'epoch'};
49             }
50              
51             ###############################################################################
52             # Subroutine: epoch
53             ###############################################################################
54             # Returns the currently set time as "the number of seconds since the epoch".
55             # If a date/time hasn't explicitly been parsed, we default to the current time.
56             ###############################################################################
57             sub epoch {
58 9     9 1 102 my $self = shift;
59 9 100       37 unless ($self->{'epoch'}) {
60 1         4 $self->now();
61             }
62 9         45 return $self->{'epoch'};
63             }
64              
65             ###############################################################################
66             # Subroutine: parse(string, zone)
67             ###############################################################################
68             # Parses the given date/time 'string' and sets that as the current time value
69             # for further operations. An optional time 'zone' is used if there is no time
70             # zone information present in the provided date string.
71             ###############################################################################
72             sub parse {
73 8     8 1 360 my ($self, $string, $zone) = @_;
74 8         33 $self->{'epoch'} = Date::Parse::str2time( $string, $zone );
75 8         2571 return $self;
76             }
77              
78             ###############################################################################
79             # Subroutine: str2time(string, zone)
80             ###############################################################################
81             # An alternate name for the 'parse' method above.
82             ###############################################################################
83             *str2time = \&parse;
84              
85             ###############################################################################
86             # Subroutine: format(format, zone)
87             ###############################################################################
88             # Formats the current time value using the given strftime 'format', optionally
89             # converting it into the given time 'zone'. If a date/time hasn't explicitly
90             # been parsed, we default to the current time.
91             #
92             # You may also refer to this method as 'time2str'; its original name from the
93             # 'Date::Format' module.
94             ###############################################################################
95             sub format {
96 5     5 1 12 my ($self, $format, $zone) = @_;
97 5         18 return Date::Format::time2str( $format, $self->epoch(), $zone );
98             }
99              
100             ###############################################################################
101             # Subroutine: time2str(format, zone)
102             ###############################################################################
103             # An alternate name for the 'format' method above.
104             ###############################################################################
105             *time2str = \&format;
106              
107             ###############################################################################
108             # Subroutine: stringify
109             ###############################################################################
110             # Stringifies the object, in ISO8601 format (%Y-%m-%d %H:%M:%S).
111             #
112             # This method is overloaded, so that simply turning the TimeDate object into a
113             # string will output it in ISO8601 format.
114             ###############################################################################
115             sub stringify {
116 0     0 1   my $self = shift;
117 0           return $self->format( '%Y-%m-%d %H:%M:%S' );
118             }
119              
120             1;
121              
122             =head1 NAME
123              
124             Template::Plugin::TimeDate - TT plugin to parse/format dates using TimeDate
125              
126             =head1 SYNOPSIS
127              
128             [% USE TimeDate %]
129              
130             # get current time, as "seconds since the epoch"
131             [% TimeDate.now %]
132              
133             # parse date string and show in default format (ISO8601)
134             [% TimeDate.parse('2007-09-02 12:34:56 PDT') %]
135              
136             # parse date string with explicit time zone
137             [% TimeDate.parse('2007-09-02 12:34:56', 'EDT') %]
138              
139             # get current time, with custom format
140             [% TimeDate.format('%b %e %Y @ %l:%M %p') %]
141              
142             # parse/display
143             [% USE mydate = TimeDate('2007-09-02 12:34:56 PDT') %]
144             [% mydate.format('%b %e %Y @ %l:%M %p') %]
145              
146             # method chaining
147             [% USE mydate = TimeDate %]
148             [% mydate.parse('2007-09-02 12:34:56 PDT').format('%Y-%m-%d %H:%M:%S %z') %]
149              
150             =head1 DESCRIPTION
151              
152             C is a TT plug-in that makes of the C
153             and C modules from the C distribution, to help deal
154             with parsing/formatting dates.
155              
156             Why another date/time TT plug-in? C doesn't handle
157             output in different timezones, and C didn't give me
158             a means of easily parsing dates before turning them into C objects.
159             I'd been using the C module elsewhere to parse dates, and so this
160             plug-in was built to help capture the parse/format cycle that I wanted to use in
161             my templates.
162              
163             The plug-in should be loaded via the USE directive:
164              
165             [% USE TimeDate %]
166              
167             This creates a plug-in object with the default name of 'TimeDate'. An
168             alternate name can be specified such as:
169              
170             [% USE mydate = TimeDate %]
171              
172             =head1 METHODS
173              
174             =over
175              
176             =item new(string)
177              
178             Creates a new TimeDate plug-in object, returning it to the caller. An
179             optional date/time string may be passed in, which is parsed automatically.
180              
181             =item now
182              
183             Sets the current time to "now", and returns it as "the number of seconds
184             since the epoch".
185              
186             =item epoch
187              
188             Returns the currently set time as "the number of seconds since the epoch".
189             If a date/time hasn't explicitly been parsed, we default to the current
190             time.
191              
192             =item parse(string, zone)
193              
194             Parses the given date/time C and sets that as the current time
195             value for further operations. An optional time C is used if there is
196             no time zone information present in the provided date string.
197              
198             =item str2time(string, zone)
199              
200             An alternate name for the C method above.
201              
202             =item format(format, zone)
203              
204             Formats the current time value using the given strftime C,
205             optionally converting it into the given time C. If a date/time hasn't
206             explicitly been parsed, we default to the current time.
207              
208             You may also refer to this method as C; its original name from
209             the C module.
210              
211             =item time2str(format, zone)
212              
213             An alternate name for the C method above.
214              
215             =item stringify
216              
217             Stringifies the object, in ISO8601 format (%Y-%m-%d %H:%M:%S).
218              
219             This method is overloaded, so that simply turning the TimeDate object into
220             a string will output it in ISO8601 format.
221              
222             =back
223              
224             =head1 AUTHOR
225              
226             Graham TerMarsch (cpan@howlingfrog.com)
227              
228             =head1 COPYRIGHT
229              
230             Copyright (C) 2007, Graham TerMarsch. All Rights Reserved.
231              
232             This is free software; you can redistribute it and/or modify it under the same
233             terms as Perl itself.
234              
235             =head1 SEE ALSO
236              
237             L,
238             L,
239             L