Skip to content

Commit

Permalink
php date parse functions fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
insky committed Jun 5, 2018
1 parent 4f8221c commit efbde36
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
34 changes: 20 additions & 14 deletions php/Exchange.php
Expand Up @@ -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 ('/\.(?<milliseconds>[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;
}

Expand Down Expand Up @@ -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);
}

/*
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions php/test/ExchangeTest.php
Expand Up @@ -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'));
}
}

0 comments on commit efbde36

Please sign in to comment.