Skip to content

Commit dd2bbf4

Browse files
committed
Ok, here's a compromise: We either keep microseconds when converting to DateTime, or negative epochs. I can't find a way for supporting both.
1 parent 8b336de commit dd2bbf4

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

rdb/Datum/ObjectDatum.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ public function toNative($opts)
6767
if ((!isset($opts['timeFormat']) || $opts['timeFormat'] == "native")
6868
&& isset($native['$reql_type$']) && $native['$reql_type$'] == 'TIME') {
6969
$time = $native['epoch_time'];
70-
//$format = (strpos($time, '.') !== false) ? 'Y-m-d H:i:s.u' : 'Y-m-d H:i:s';
71-
$format = (strpos($time, '.') !== false) ? 'Y-m-d\TH:i:s.u' : 'Y-m-d\TH:i:s';
72-
$datetime = new \DateTime(date($format, $time) . $native['timezone'], new \DateTimeZone('UTC'));
70+
// This is really stupid. It looks like we can either use `date`, which ignores microseconds,
71+
// or we can use `createFromFormat` which cannot handle negative epoch times.
72+
if ($time < 0) {
73+
$format = (strpos($time, '.') !== false) ? 'Y-m-d\TH:i:s.u' : 'Y-m-d\TH:i:s';
74+
$datetime = new \DateTime(date($format, $time) . $native['timezone'], new \DateTimeZone('UTC'));
75+
} else {
76+
$format = (strpos($time, '.') !== false) ? '!U.u T' : '!U T';
77+
$datetime = \DateTime::createFromFormat($format, $time . " " . $native['timezone'], new \DateTimeZone('UTC'));
78+
}
7379

7480
// This is horrible. Just because in PHP 5.3.something parsing "+01:00" as a DateTimeZone doesn't work. :(
7581
$tzSign = $native['timezone'][0];

0 commit comments

Comments
 (0)