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:
?>
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:
--- /dev/null
+<?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:
+?>