From: Raphaël Barrois Date: Fri, 7 May 2010 13:01:23 +0000 (+0200) Subject: Add new function for parsing dates X-Git-Tag: core/1.1.0~21 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=707b65dc805f31c3d82fbf3cc13f0a4ab5b653e8;p=platal.git Add new function for parsing dates * reads from DateTime / string / int objects * replaces code in modifier.date_format * complete with UTs :P Signed-off-by: Raphaël Barrois --- diff --git a/include/misc.inc.php b/include/misc.inc.php index 77be2b0..6e4314b 100644 --- a/include/misc.inc.php +++ b/include/misc.inc.php @@ -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: ?> diff --git a/plugins/modifier.date_format.php b/plugins/modifier.date_format.php index 7a5c64b..a2e5bec 100644 --- a/plugins/modifier.date_format.php +++ b/plugins/modifier.date_format.php @@ -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 index 0000000..19fca15 --- /dev/null +++ b/ut/dateparsertest.php @@ -0,0 +1,44 @@ +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: +?>