From 5ab78106b0f9170d5e4930d0901862e4373a686f Mon Sep 17 00:00:00 2001 From: Andreas Gohr <andi@splitbrain.org> Date: Wed, 9 Jun 2010 21:43:10 +0200 Subject: [PATCH] Make XMLRPC date parsing more flexible FS#1966 Since the specs aren't 100% clear, dates might be passed in different formats by various XMLRPC clients. This patch makes date parsing a bit more flexible. Unit tests included. --- _test/cases/inc/IXR_Library_date.test.php | 34 +++++++++++++++++++++++ inc/IXR_Library.php | 14 ++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 _test/cases/inc/IXR_Library_date.test.php diff --git a/_test/cases/inc/IXR_Library_date.test.php b/_test/cases/inc/IXR_Library_date.test.php new file mode 100644 index 000000000..a2b6154d8 --- /dev/null +++ b/_test/cases/inc/IXR_Library_date.test.php @@ -0,0 +1,34 @@ +<?php +require_once DOKU_INC.'inc/IXR_Library.php'; + +class ixr_library_date_test extends UnitTestCase { + + + function test_parseIso(){ + // multiple tests + $tests = array( + // full datetime, different formats + array('2010-08-17T09:23:14', 1282036994), + array('20100817T09:23:14', 1282036994), + array('2010-08-17 09:23:14', 1282036994), + array('20100817 09:23:14', 1282036994), + array('2010-08-17T09:23:14Z', 1282036994), + array('20100817T09:23:14Z', 1282036994), + + // no seconds + array('2010-08-17T09:23', 1282036980), + array('20100817T09:23', 1282036980), + + // no time + array('2010-08-17', 1282003200), + //array('20100817', 1282003200), #this will NOT be parsed, but is assumed to be timestamp + ); + + foreach($tests as $test){ + $dt = new IXR_Date($test[0]); + $this->assertEqual($dt->getTimeStamp(),$test[1]); + } + } + +} +//Setup VIM: ex: et ts=4 enc=utf-8 : diff --git a/inc/IXR_Library.php b/inc/IXR_Library.php index b82952d74..2fb7dfe68 100644 --- a/inc/IXR_Library.php +++ b/inc/IXR_Library.php @@ -615,12 +615,14 @@ class IXR_Date { $this->second = gmdate('s', $timestamp); } function parseIso($iso) { - $this->year = substr($iso, 0, 4); - $this->month = substr($iso, 5, 2); - $this->day = substr($iso, 8, 2); - $this->hour = substr($iso, 11, 2); - $this->minute = substr($iso, 14, 2); - $this->second = substr($iso, 17, 2); + if(preg_match('/^(\d\d\d\d)-?(\d\d)-?(\d\d)([T ](\d\d):(\d\d)(:(\d\d))?)?/',$iso,$match)){ + $this->year = (int) $match[1]; + $this->month = (int) $match[2]; + $this->day = (int) $match[3]; + $this->hour = (int) $match[5]; + $this->minute = (int) $match[6]; + $this->second = (int) $match[8]; + } } function getIso() { return $this->year.$this->month.$this->day.'T'.$this->hour.':'.$this->minute.':'.$this->second; -- GitLab