From efbde36d933a73a0e6d11d28880db47c01dac2bb Mon Sep 17 00:00:00 2001 From: Roman Zhbadynskyi Date: Tue, 5 Jun 2018 17:40:08 +0300 Subject: [PATCH] php date parse functions fixed --- php/Exchange.php | 34 ++++++++++++++++++++-------------- php/test/ExchangeTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/php/Exchange.php b/php/Exchange.php index 96b676e05f8c..0767203a4c3e 100644 --- a/php/Exchange.php +++ b/php/Exchange.php @@ -486,26 +486,32 @@ public function microseconds () { } public static function iso8601 ($timestamp) { - if (!isset ($timestamp)) - return $timestamp; + if (!is_numeric ($timestamp) || intval ($timestamp) != $timestamp) + return null; + $timestamp = (int) $timestamp; + if ($timestamp < 0) + return null; $result = date ('c', (int) round ($timestamp / 1000)); $msec = (int) $timestamp % 1000; return str_replace ('+', sprintf (".%03d+", $msec), $result); } public static function parse_date ($timestamp) { - if (!isset ($timestamp)) - return $timestamp; - if (strstr ($timestamp, 'GMT')) - return strtotime ($timestamp) * 1000; return static::parse8601 ($timestamp); } public static function parse8601 ($timestamp) { - $time = strtotime ($timestamp) * 1000; - if (preg_match ('/\.(?[0-9]{1,3})/', $timestamp, $match)) { - $time += (int) str_pad($match['milliseconds'], 3, '0', STR_PAD_RIGHT); - } + if (!$timestamp || !is_string ($timestamp)) + return null; + $timedata = date_parse ($timestamp); + if (!$timedata || $timedata['error_count'] > 0 || $timedata['warning_count'] > 0 || (isset ($timedata['relative']) && count ($timedata['relative']) > 0)) + return null; + if ($timedata['hour'] === false || $timedata['minute'] === false || $timedata['second'] === false || $timedata['year'] === false || $timedata['month'] === false || $timedata['day'] === false) + return null; + $time = strtotime($timestamp); + if ($time === false) + return null; + $time *= 1000; return $time; } @@ -903,7 +909,7 @@ public function fetch ($url, $method = 'GET', $headers = null, $body = null) { // we probably only need to set it once on startup if ($this->curlopt_interface) { - curl_setopt ($this->curl, CURLOPT_INTERFACE, $this->curlopt_interface); + curl_setopt ($this->curl, CURLOPT_INTERFACE, $this->curlopt_interface); } /* @@ -1472,9 +1478,9 @@ public function fetchBalance () { return $this->fetch_balance (); } - public function fetch_balance ($params = array ()) { - throw new NotSupported ($this->id . ' fetch_balance() not implemented yet'); - } + public function fetch_balance ($params = array ()) { + throw new NotSupported ($this->id . ' fetch_balance() not implemented yet'); + } public function fetchOrderBook ($symbol, $limit = null, $params = array ()) { return $this->fetch_order_book ($symbol, $limit, $params); diff --git a/php/test/ExchangeTest.php b/php/test/ExchangeTest.php index b8786f417a50..cd47b1b1ab48 100644 --- a/php/test/ExchangeTest.php +++ b/php/test/ExchangeTest.php @@ -154,4 +154,31 @@ public function negativeNumbers () { ['-0.123456', ROUND, 5, DECIMAL_PLACES, '-0.12346'], ]; } + + public function testDatetimeFunctions () { + $this->assertSame (null, Exchange::iso8601 (null)); + $this->assertSame (null, Exchange::iso8601 (false)); + $this->assertSame (null, Exchange::iso8601 ([])); + $this->assertSame (null, Exchange::iso8601 ('abracadabra')); + $this->assertSame (null, Exchange::iso8601 ('1.2')); + $this->assertSame (null, Exchange::iso8601 (-1)); + $this->assertSame (null, Exchange::iso8601 ('-1')); + $this->assertSame ('1970-01-01T00:00:00.000+00:00', Exchange::iso8601 (0)); + $this->assertSame ('1970-01-01T00:00:00.000+00:00', Exchange::iso8601 ('0')); + $this->assertSame ('1986-04-25T21:23:47.000+00:00', Exchange::iso8601 (514848227000)); + $this->assertSame ('1986-04-25T21:23:47.000+00:00', Exchange::iso8601 ('514848227000')); + + $this->assertSame (null, Exchange::parse_date (null)); + $this->assertSame (null, Exchange::parse_date (0)); + $this->assertSame (null, Exchange::parse_date ('0')); + $this->assertSame (null, Exchange::parse_date ('+1 day')); + $this->assertSame (null, Exchange::parse_date ('1986-04-25T21:23:47+00:00 + 1 week')); + $this->assertSame (null, Exchange::parse_date ('1 february')); + $this->assertSame (null, Exchange::parse_date ('1986-04-26')); + $this->assertSame (0, Exchange::parse_date ('1970-01-01T00:00:00.000+00:00')); + $this->assertSame (514848227000, Exchange::parse_date ('1986-04-25T21:23:47+00:00')); + $this->assertSame (514848227000, Exchange::parse_date ('1986-04-26T01:23:47+04:00')); + $this->assertSame (514848227000, Exchange::parse_date ('25 Apr 1986 21:23:47 GMT')); + $this->assertSame (514862627000, Exchange::parse_date ('1986-04-26T01:23:47.000Z')); + } }