[Templates] [patch] Template::Plugin::Date accept Y-M-D H:M:S

Ronald J Kimball rkimball+tt@pangeamedia.com
Fri, 06 Jul 2007 13:49:45 -0400


This is a multi-part message in MIME format.
--------------080109010203090800040408
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

I've just started using Template::Plugin::Date.  I was surprised to 
discover that it only accepts dates in the format 'H:M:S D/M/Y'.  (I 
shouldn't have to format my dates in order to pass them to the date 
formatter! :)

The first attached patch allows Template::Plugin::Date to also accept 
dates in the format 'Y-M-D H:M:S', which is the default format for many 
databases.  The code checks the length of the first and last parts to 
determine which format the date is in.  I also added a couple tests.

The other attached patch updates the documentation for 
Template::Plugin::Date in docsrc.

Feedback welcome.


(In the meantime, I've written a subclass of Template::Plugin::Date that 
wraps the format() function and converts the date to the accepted format.)

Ronald

--------------080109010203090800040408
Content-Type: text/plain;
 name="template_plugin_date.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="template_plugin_date.patch"

Only in Template-Toolkit-2.19: .defaults.cfg
diff -r -u Template-Toolkit-2.19.orig/lib/Template/Plugin/Date.pm Template-Toolkit-2.19/lib/Template/Plugin/Date.pm
--- Template-Toolkit-2.19.orig/lib/Template/Plugin/Date.pm	2007-04-27 13:56:05.000000000 -0400
+++ Template-Toolkit-2.19/lib/Template/Plugin/Date.pm	2007-07-06 12:45:08.000000000 -0400
@@ -98,11 +98,27 @@
     }
     else {
         # if $time is numeric, then we assume it's seconds since the epoch
-        # otherwise, we try to parse it as a 'H:M:S D:M:Y' string
-        @date = (split(/(?:\/| |:|-)/, $time))[2,1,0,3..5];
-        return (undef, Template::Exception->new('date',
-               "bad time/date string:  expects 'h:m:s d:m:y'  got: '$time'"))
-            unless @date >= 6 && defined $date[5];
+        # otherwise, we try to parse it as either a 'Y:M:D H:M:S' or a
+        # 'H:M:S D:M:Y' string
+
+        my @parts = (split(/(?:\/| |:|-)/, $time));
+
+        if (@parts >= 6) {
+            if (length($parts[0]) == 4) {
+                # year is first; assume 'Y:M:D H:M:S'
+                @date = @parts[reverse 0..5];
+            }
+            elsif (length($parts[5]) == 4) {
+                # year is last; assume 'H:M:S D:M:Y'
+                @date = @parts[2,1,0,3..5];
+            }
+        }
+
+        if (!@date) {
+            return (undef, Template::Exception->new('date',
+                   "bad time/date string:  " .
+                   "expects 'h:m:s d:m:y'  got: '$time'"));
+        }
         $date[4] -= 1;     # correct month number 1-12 to range 0-11
         $date[5] -= 1900;  # convert absolute year to years since 1900
         $time = &POSIX::mktime(@date);
diff -r -u Template-Toolkit-2.19.orig/t/date.t Template-Toolkit-2.19/t/date.t
--- Template-Toolkit-2.19.orig/t/date.t	2007-03-13 15:23:31.000000000 -0400
+++ Template-Toolkit-2.19/t/date.t	2007-07-06 12:36:57.000000000 -0400
@@ -197,7 +197,14 @@
 
 -- test --
 [% USE date %]
-[% date.format('4:20:00 6-13-2000', '%H') %]
+[% date.format('4:20:00 13-6-2000', '%H') %]
+
+-- expect --
+04
+
+-- test --
+[% USE date %]
+[% date.format('2000-6-13 4:20:00', '%H') %]
 
 -- expect --
 04
@@ -249,5 +256,11 @@
 -%]
 -- expect --
 12:59
+-- test --
+[% USE date;
+   date.format('2001/09/30 12:59:00', '%H:%M')
+-%]
+-- expect --
+12:59
 
 

--------------080109010203090800040408
Content-Type: text/plain;
 name="template_plugin_date_docs.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="template_plugin_date_docs.patch"

diff -u -r docsrc.orig/src/Modules/Template/Plugin/Date.tt2 docsrc/src/Modules/Template/Plugin/Date.tt2
--- docsrc.orig/src/Modules/Template/Plugin/Date.tt2	2002-08-20 19:29:17.000000000 -0400
+++ docsrc/src/Modules/Template/Plugin/Date.tt2	2007-07-06 12:49:14.000000000 -0400
@@ -6,9 +6,11 @@
     # use current time and default format
     [% date.format %]
 
-    # specify time as seconds since epoch or 'h:m:s d-m-y' string
+    # specify time as seconds since epoch
+    # or as a 'h:m:s d-m-y' or 'y-m-d h:m:s' string
     [% date.format(960973980) %]
     [% date.format('4:20:36 21/12/2000') %]
+    [% date.format('2000/12/21 4:20:36') %]
 
     # specify format
     [% date.format(mytime, '%H:%M:%S') %]
@@ -64,8 +66,9 @@
 
     File last modified: [% date.format(filemod_time) %]
 
-The time/date can also be specified as a string of the form 'h:m:s d/m/y'.
-Any of the characters : / - or space may be used to delimit fields.
+The time/date can also be specified as a string of the form 'h:m:s d/m/y'
+or 'y/m/d h:m:s'.  Any of the characters : / - or space may be used to
+delimit fields.
 
     [% USE day = date(format => '%A', locale => 'en_GB') %]
     [% day.format('4:20:00 9-13-2000') %]  

--------------080109010203090800040408--