From a5c7314f8da111523246e67cb019292ae05f2895 Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Mon, 20 Jan 2020 19:34:47 -0600 Subject: [PATCH] Fix string passed to DateTimeImmutable --- src/DeprecatedUuidMethodsTrait.php | 4 +++- src/Rfc4122/UuidV1.php | 4 +++- tests/UuidTest.php | 5 ++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/DeprecatedUuidMethodsTrait.php b/src/DeprecatedUuidMethodsTrait.php index b8585dc..23fec83 100644 --- a/src/DeprecatedUuidMethodsTrait.php +++ b/src/DeprecatedUuidMethodsTrait.php @@ -140,7 +140,9 @@ trait DeprecatedUuidMethodsTrait try { return new DateTimeImmutable( - date('Y-m-d H:i:s', (int) $time->getSeconds()->toString()) . '.' + '@' + . $time->getSeconds()->toString() + . '.' . str_pad($time->getMicroSeconds()->toString(), 6, '0', STR_PAD_LEFT) ); } catch (Throwable $e) { diff --git a/src/Rfc4122/UuidV1.php b/src/Rfc4122/UuidV1.php index d53bac9..40bcd8c 100644 --- a/src/Rfc4122/UuidV1.php +++ b/src/Rfc4122/UuidV1.php @@ -44,7 +44,9 @@ final class UuidV1 extends Uuid implements UuidInterface try { return new DateTimeImmutable( - date('Y-m-d H:i:s', (int) $time->getSeconds()->toString()) . '.' + '@' + . $time->getSeconds()->toString() + . '.' . str_pad($time->getMicroSeconds()->toString(), 6, '0', STR_PAD_LEFT) ); } catch (Throwable $e) { diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 694d751..2b974f2 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -1439,7 +1439,7 @@ class UuidTest extends TestCase $timeConverter ->shouldReceive('convertTime') ->once() - ->andReturn(new Time(1579476464, '1234567890')); + ->andReturn(new Time(1234567890, '1234567')); $builder = new DefaultUuidBuilder($numberConverter, $timeConverter); $codec = new StringCodec($builder); @@ -1452,8 +1452,7 @@ class UuidTest extends TestCase $this->expectException(DateTimeException::class); $this->expectExceptionMessage( 'DateTimeImmutable::__construct(): Failed to parse time string ' - . '(2020-01-19 23:27:44.1234567890) at position 11 (2): ' - . 'The timezone could not be found in the database' + . '(@1234567890.1234567) at position 18 (7): Unexpected character' ); $uuid->getDateTime();