-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Uid] Replace getTime() with getDateTime() #40008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
e2bfcbe
to
df032e7
Compare
@@ -95,7 +95,7 @@ public function testV6() | |||
|
|||
$uuid = new UuidV6(substr_replace(self::A_UUID_V1, '6', 14, 1)); | |||
|
|||
$this->assertSame(85916308548.27832, $uuid->getTime()); | |||
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '85916308548.278321'), $uuid->getDateTime()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a case where we are more precise now.
$this->assertSame(-12219292800.0, ((new UuidV1('00000000-0000-1000-a456-426655440000'))->getTime())); | ||
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '103072857660.684697'), ((new UuidV1('ffffffff-ffff-1fff-a456-426655440000'))->getDateTime())); | ||
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '0.000001'), ((new UuidV1('1381400a-1dd2-11b2-a456-426655440000'))->getDateTime())); | ||
$this->assertEquals(new \DateTimeImmutable('@0'), (new UuidV1('13814001-1dd2-11b2-a456-426655440000'))->getDateTime()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a case where we are less precise now (0.0000001
is floored to 0.000000
, ie 0
).
1587c03
to
1023e26
Compare
1023e26
to
360c900
Compare
Thank you @fancyweb. |
Getting a
\DateTimeImmutable
has two benefits: easier to use and no float precision problems.There is however one drawback for UUIDs: we technically loose some precision in the output because datetimes do not handle nanoseconds (only microseconds) and uuid timestamps increment every 100 nanoseconds (0.1 microseconds). However, this is theoretical since the increment is only there to generate more entropy. Also, if an end user really want the precision, he can still do the conversion him/herself from the raw data. Finally, because of some rounding problems with floats even on 64b platforms, precision is actually won most of the time thanks to the datetime.
The idea is to also accept
\DateTimeInterface
as input inUuidFactory
andUlidFactory
(see #39507) btw.The breaking change is allowed because the component is experimental.