Add new function for parsing dates
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Fri, 7 May 2010 13:01:23 +0000 (15:01 +0200)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Fri, 7 May 2010 13:01:23 +0000 (15:01 +0200)
* reads from DateTime / string / int objects
* replaces code in modifier.date_format
* complete with UTs :P

Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
include/misc.inc.php
plugins/modifier.date_format.php
ut/dateparsertest.php [new file with mode: 0644]

index 77be2b0..6e4314b 100644 (file)
@@ -189,5 +189,25 @@ function uint_to_ip($uint)
     return long2ip($uint);
 }
 
+/** Converts DateTime / string / timestamp to DateTime object
+ */
+function make_datetime($date)
+{
+    if ($date instanceof DateTime) {
+        return $date;
+    } elseif (preg_match('/^\d{14}$/', $date) || preg_match('/^\d{8}$/', $date)) {
+        return new DateTime($date);
+    } elseif (is_int($date) || is_numeric($date)) {
+        return new DateTime("@$date");
+    } else {
+        try {
+            $d = new DateTime($date);
+            return $d;
+        } catch (Exception $e) {
+            return null;
+        }
+    }
+}
+
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>
index 7a5c64b..a2e5bec 100644 (file)
@@ -25,23 +25,8 @@ function smarty_modifier_date_format($string, $format = '%x', $default_date=null
     if (empty($format) || preg_match('/^[ 0\-]*$/', $d)) return;
     $f = str_replace('%X', '%T', str_replace('%x', '%e %B %Y', $format));
 
-    if (preg_match('/^\d{14}$/', $d)) {
-        $t = mktime(substr($d,8,2), substr($d,10,2), substr($d,12,2), substr($d,4,2), substr($d,6,2), substr($d,0,4));
-    } elseif (preg_match('/^\d{8}$/', $d)) {
-        $t = mktime(0, 0, 0, substr($d,4,2), substr($d,6,2), substr($d,0,4));
-    } elseif (is_numeric($d)) {
-        $t = intval($d);
-    } else {
-        $t = strtotime($d);
-    }
-
-    if ( $t != -1 ) {
-        return strftime($f , $t);
-    } else {
-        require_once('Date.php');
-        $date = new Date($d);
-        return $date->format($f);
-    }
+    $d = make_datetime($d);
+    return $d->format($f);
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
diff --git a/ut/dateparsertest.php b/ut/dateparsertest.php
new file mode 100644 (file)
index 0000000..19fca15
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+require_once dirname(__FILE__) . '/../include/test.inc.php';
+
+class DateParserTest extends PlTestCase
+{
+    public function testNumeric()
+    {
+        $this->assertEquals(make_datetime('12000101'), new DateTime('1200-01-01'));
+        $this->assertEquals(make_datetime('20100101'), new DateTime('2010-01-01'));
+        $this->assertEquals(make_datetime('20100101124213'), new DateTime('2010-01-01 12:42:13'));
+        $this->assertEquals(make_datetime('1273232546'), new DateTime('2010-05-07 13:42:26'));
+        $this->assertEquals(make_datetime(1273232546), new DateTime('2010-05-07 13:42:42'));
+    }
+
+    public function testText()
+    {
+        $this->assertEquals(make_datetime('2010-01-01'), new DateTime('2010-01-01'));
+        $this->assertEquals(make_datetime('1600-01-01'), new DateTime('1600-01-01'));
+        $this->assertEquals(make_datetime('2010-01-01 08:09:10'), new DateTime('2010-01-01 08:09:10'));
+    }
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>