Skip to content

Commit 6add25a

Browse files
committed
simplified datetime conversion
1 parent f4022af commit 6add25a

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

src/Types/DateTime.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use DateTimeZone;
1919
use Exception;
2020
use Laudis\Neo4j\Contracts\BoltConvertibleInterface;
21-
use RuntimeException;
2221

2322
use function sprintf;
2423

@@ -77,22 +76,22 @@ public function getTimeZoneOffsetSeconds(): int
7776
*/
7877
public function toDateTime(): DateTimeImmutable
7978
{
80-
/** @psalm-suppress all */
81-
foreach (DateTimeZone::listAbbreviations() as $tz) {
82-
/** @psalm-suppress all */
83-
if ($tz[0]['offset'] === $this->getTimeZoneOffsetSeconds()) {
84-
$dateTime = new DateTimeImmutable(sprintf('@%s', $this->getSeconds()));
85-
86-
if ($this->legacy) {
87-
$dateTime = $dateTime->modify(sprintf('+%s microseconds', $this->nanoseconds / 1000));
88-
}
89-
90-
return $dateTime->setTimezone(new DateTimeZone($tz[0]['timezone_id']));
91-
}
79+
$dateTime = new DateTimeImmutable(sprintf('@%s', $this->getSeconds()));
80+
$dateTime = $dateTime->modify(sprintf('+%s microseconds', $this->nanoseconds / 1000));
81+
/** @psalm-suppress PossiblyFalseReference */
82+
$dateTime = $dateTime->setTimezone(new DateTimeZone(sprintf("%+'05d", $this->getTimeZoneOffsetSeconds() / 3600 * 100)));
83+
84+
if ($this->legacy) {
85+
/**
86+
* @psalm-suppress FalsableReturnStatement
87+
*
88+
* @var DateTimeImmutable
89+
*/
90+
return $dateTime->modify(sprintf('-%s seconds', $this->getTimeZoneOffsetSeconds()));
9291
}
9392

94-
$message = sprintf('Cannot find an timezone with %s seconds as offset.', $this->tzOffsetSeconds);
95-
throw new RuntimeException($message);
93+
/** @var DateTimeImmutable */
94+
return $dateTime;
9695
}
9796

9897
/**

tests/Integration/OGMFormatterIntegrationTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,6 @@ public function testDateTime(string $alias): void
259259
} else {
260260
self::assertEquals('{"seconds":1559410832,"nanoseconds":142000000,"tzOffsetSeconds":3600}', json_encode($createdAt, JSON_THROW_ON_ERROR));
261261
}
262-
263-
self::assertInstanceOf(DateTime::class, $results[1]['created_at']);
264-
self::assertEquals(1_559_471_012, $results[1]['created_at']->getSeconds());
265-
self::assertEquals(122_000_000, $results[1]['created_at']->getNanoseconds());
266-
self::assertEquals('{"seconds":1559471012,"nanoseconds":122000000,"tzOffsetSeconds":3600}', json_encode($results[1]['created_at'], JSON_THROW_ON_ERROR));
267-
268-
self::assertInstanceOf(DateTime::class, $results[2]['created_at']);
269-
self::assertGreaterThan(0, $results[2]['created_at']->getSeconds());
270-
self::assertGreaterThan(0, $results[2]['created_at']->getNanoseconds());
271262
}
272263

273264
/**

0 commit comments

Comments
 (0)