Skip to content

[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

Merged
merged 1 commit into from
Jan 27, 2021

Conversation

fancyweb
Copy link
Contributor

@fancyweb fancyweb commented Jan 27, 2021

Q A
Branch? 5.x
Bug fix? no
New feature? yes
Deprecations? no
Tickets #39507 (review)
License MIT
Doc PR -

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 in UuidFactory and UlidFactory (see #39507) btw.

The breaking change is allowed because the component is experimental.

@@ -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());
Copy link
Contributor Author

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());
Copy link
Contributor Author

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).

@nicolas-grekas nicolas-grekas force-pushed the uid/get-date-time branch 3 times, most recently from 1587c03 to 1023e26 Compare January 27, 2021 20:52
@nicolas-grekas
Copy link
Member

Thank you @fancyweb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants