8000 [Uid] Replace getTime() with getDateTime() by fancyweb · Pull Request #40008 · symfony/symfony · GitHub
[go: up one dir, main page]

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions UPGRADE-5.3.md
8000
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ UPGRADE FROM 5.2 to 5.3
Asset
-----

* Deprecated `RemoteJsonManifestVersionStrategy`, use `JsonManifestVersionStrategy` instead.
* Deprecated `RemoteJsonManifestVersionStrategy`, use `JsonManifestVersionStrategy` instead

DomCrawler
----------

* Deprecated the `parents()` method, use `ancestors()` instead.
* Deprecated the `parents()` method, use `ancestors()` instead

Form
----

* Changed `$forms` parameter type of the `DataMapperInterface::mapDataToForms()` method from `iterable` to `\Traversable`.
* Changed `$forms` parameter type of the `DataMapperInterface::mapFormsToData()` method from `iterable` to `\Traversable`.
* Deprecated passing an array as the second argument of the `DataMapper::mapDataToForms()` method, pass `\Traversable` instead.
* Deprecated passing an array as the first argument of the `DataMapper::mapFormsToData()` method, pass `\Traversable` instead.
* Deprecated passing an array as the second argument of the `CheckboxListMapper::mapDataToForms()` method, pass `\Traversable` instead.
* Deprecated passing an array as the first argument of the `CheckboxListMapper::mapFormsToData()` method, pass `\Traversable` instead.
* Deprecated passing an array as the second argument of the `RadioListMapper::mapDataToForms()` method, pass `\Traversable` instead.
* Deprecated passing an array as the first argument of the `RadioListMapper::mapFormsToData()` method, pass `\Traversable` instead.
* Changed `$forms` parameter type of the `DataMapperInterface::mapDataToForms()` method from `iterable` to `\Traversable`
* Changed `$forms` parameter type of the `DataMapperInterface::mapFormsToData()` method from `iterable` to `\Traversable`
* Deprecated passing an array as the second argument of the `DataMapper::mapDataToForms()` method, pass `\Traversable` instead
* Deprecated passing an array as the first argument of the `DataMapper::mapFormsToData()` method, pass `\Traversable` instead
* Deprecated passing an array as the second argument of the `CheckboxListMapper::mapDataToForms()` method, pass `\Traversable` instead
* Deprecated passing an array as the first argument of the `CheckboxListMapper::mapFormsToData()` method, pass `\Traversable` instead
* Deprecated passing an array as the second argument of the `RadioListMapper::mapDataToForms()` method, pass `\Traversable` instead
* Deprecated passing an array as the first argument of the `RadioListMapper::mapFormsToData()` method, pass `\Traversable` instead

HttpFoundation
--------------
Expand All @@ -36,29 +36,37 @@ HttpKernel
Messenger
---------

* Deprecated the `prefetch_count` parameter in the AMQP bridge, it has no effect and will be removed in Symfony 6.0.
* Deprecated the `prefetch_count` parameter in the AMQP bridge, it has no effect and will be removed in Symfony 6.0

Notifier
-------
--------

* Changed the return type of `AbstractTransportFactory::getEndpoint()` from `?string` to `string`
* Changed the signature of `Dsn::__construct()` to accept a single `string $dsn` argument
* Removed the `Dsn::fromString()` method

* Changed the return type of `Symfony\Component\Notifier\Transport\AbstractTransportFactory::getEn 8000 dpoint()` from `?string` to `string`

PhpunitBridge
-------------

* Deprecated the `SetUpTearDownTrait` trait, use original methods with "void" return typehint.
* Deprecated the `SetUpTearDownTrait` trait, use original methods with "void" return typehint

PropertyInfo
------------

* Deprecated the `Type::getCollectionKeyType()` and `Type::getCollectionValueType()` methods, use `Type::getCollectionKeyTypes()` and `Type::getCollectionValueTypes()` instead.
* Deprecated the `Type::getCollectionKeyType()` and `Type::getCollectionValueType()` methods, use `Type::getCollectionKeyTypes()` and `Type::getCollectionValueTypes()` instead

Security
--------

* Deprecated voters that do not return a valid decision when calling the `vote` method.
* Deprecated voters that do not return a valid decision when calling the `vote` method

Serializer
----------

* Deprecated `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead.
* Deprecated `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead

Uid
---

* Replaced `UuidV1::getTime()`, `UuidV6::getTime()` and `Ulid::getTime()` by `UuidV1::getDateTime()`, `UuidV6::getDateTime()` and `Ulid::getDateTime()`
10 changes: 7 additions & 3 deletions src/Symfony/Component/Uid/BinaryUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ public static function add(string $a, string $b): string
/**
* @param string $time Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
*/
public static function timeToFloat(string $time): float
public static function timeToDateTime(string $time): \DateTimeImmutable
{
if (\PHP_INT_SIZE >= 8) {
$time = hexdec($time) - self::TIME_OFFSET_INT;
$time = (string) (hexdec($time) - self::TIME_OFFSET_INT);
} else {
$time = str_pad(hex2bin($time), 8, "\0", \STR_PAD_LEFT);

Expand All @@ -136,6 +136,10 @@ public static function timeToFloat(string $time): float
}
}

return $time / 10000000;
if (9 > \strlen($time)) {
$time = '-' === $time[0] ? '-'.str_pad(substr($time, 1), 8, '0', \STR_PAD_LEFT) : str_pad($time, 8, '0', \STR_PAD_LEFT);
}

return \DateTimeImmutable::createFromFormat('U.u?', substr_replace($time, '.', -7, 0));
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Uid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `AbstractUid::fromBinary()`, `AbstractUid::fromBase58()`, `AbstractUid::fromBase32()` and `AbstractUid::fromRfc4122()`
* [BC BREAK] Replace `UuidV1::getTime()`, `UuidV6::getTime()` and `Ulid::getTime()` by `UuidV1::getDateTime()`, `UuidV6::getDateTime()` and `Ulid::getDateTime()`

5.2.0
-----
Expand Down
9 changes: 7 additions & 2 deletions src/Symfony/Component/Uid/Tests/UlidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,18 @@ public function testBase58()
/**
* @group time-sensitive
*/
public function testGetTime()
public function testGetDateTime()
{
$time = microtime(false);
$ulid = new Ulid();
$time = substr($time, 11).substr($time, 1, 4);

$this->assertSame((float) $time, $ulid->getTime());
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', $time), $ulid->getDateTime());

$this->assertEquals(new \DateTimeImmutable('@0'), (new Ulid('000000000079KA1307SR9X4MV3'))->getDateTime());
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '0.001'), (new Ulid('000000000179KA1307SR9X4MV3'))->getDateTime());
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '281474976710.654'), (new Ulid('7ZZZZZZZZY79KA1307SR9X4MV3'))->getDateTime());
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '281474976710.655'), (new Ulid('7ZZZZZZZZZ79KA1307SR9X4MV3'))->getDateTime());
}

public function testIsValid()
Expand Down
18 changes: 10 additions & 8 deletions src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testV1()

$uuid = new UuidV1(self::A_UUID_V1);

$this->assertSame(1583245966.746458, $uuid->getTime());
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '1583245966.746458'), $uuid->getDateTime());
$this->assertSame('3499710062d0', $uuid->getNode());
}

Expand Down Expand Up @@ -95,7 +95,7 @@ public function testV6()

$uuid 67E6 = 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('3499710062d0', $uuid->getNode());
}

Expand Down Expand Up @@ -308,12 +308,14 @@ public function testFromStringOnExtendedClassReturnsStatic()
$this->assertInstanceOf(CustomUuid::class, CustomUuid::fromString(self::A_UUID_V4));
}

public function testGetTime()
public function testGetDateTime()
{
$this->assertSame(103072857660.6847, ((new UuidV1('ffffffff-ffff-1fff-a456-426655440000'))->getTime()));
$this->assertSame(0.0000001, ((new UuidV1('13814001-1dd2-11b2-a456-426655440000'))->getTime()));
$this->assertSame(0.0, (new UuidV1('13814000-1dd2-11b2-a456-426655440000'))->getTime());
$this->assertSame(-0.0000001, (new UuidV1('13813fff-1dd2-11b2-a456-426655440000'))->getTime());
$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).

$this->assertEquals(new \DateTimeImmutable('@0'), (new UuidV1('13814000-1dd2-11b2-a456-426655440000'))->getDateTime());
$this->assertEquals(new \DateTimeImmutable('@0'), (new UuidV1('13813fff-1dd2-11b2-a456-426655440000'))->getDateTime());
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '-0.000001'), ((new UuidV1('13813ff6-1dd2-11b2-a456-426655440000'))->getDateTime()));
$this->assertEquals(new \DateTimeImmutable('@-12219292800'), ((new UuidV1('00000000-0000-1000-a456-426655440000'))->getDateTime()));
}
}
24 changes: 13 additions & 11 deletions src/Symfony/Component/Uid/Ulid.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,26 @@ public function toBase32(): string
return $this->uid;
}

/**
* @return float Seconds since the Unix epoch 1970-01-01 00:00:00
*/
public function getTime(): float
public function getDateTime(): \DateTimeImmutable
{
$time = strtr(substr($this->uid, 0, 10), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');

if (\PHP_INT_SIZE >= 8) {
return hexdec(base_convert($time, 32, 16)) / 1000;
$time = (string) hexdec(base_convert($time, 32, 16));
} else {
$time = sprintf('%02s%05s%05s',
base_convert(substr($time, 0, 2), 32, 16),
base_convert(substr($time, 2, 4), 32, 16),
base_convert(substr($time, 6, 4), 32, 16)
);
$time = BinaryUtil::toBase(hex2bin($time), BinaryUtil::BASE10);
}

$time = sprintf('%02s%05s%05s',
base_convert(substr($time, 0, 2), 32, 16),
base_convert(substr($time, 2, 4), 32, 16),
base_convert(substr($time, 6, 4), 32, 16)
);
if (4 > \strlen($time)) {
$time = str_pad($time, 4, '0', \STR_PAD_LEFT);
}

return BinaryUtil::toBase(hex2bin($time), BinaryUtil::BASE10) / 1000;
return \DateTimeImmutable::createFromFormat('U.u', substr_replace($time, '.', -3, 0));
}

private static function generate(): string
Expand Down
9 changes: 2 additions & 7 deletions src/Symfony/Component/Uid/UuidV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,9 @@ public function __construct(string $uuid = null)
}
}

/**
* @return float Seconds since the Unix epoch 1970-01-01 00:00:00
*/
public function getTime(): float
public function getDateTime(): \DateTimeImmutable
{
$time = '0'.substr($this->uid, 15, 3).substr($this->uid, 9, 4).substr($this->uid, 0, 8);

return BinaryUtil::timeToFloat($time);
return BinaryUtil::timeToDateTime('0'.substr($this->uid, 15, 3).substr($this->uid, 9, 4).substr($this->uid, 0, 8));
}

public function getNode(): string
Expand Down
9 changes: 2 additions & 7 deletions src/Symfony/Component/Uid/UuidV6.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,9 @@ public function __construct(string $uuid = null)
}
}

/**
* @return float Seconds since the Unix epoch 1970-01-01 00:00:00
*/
public function get 55FC Time(): float
public function getDateTime(): \DateTimeImmutable
{
$time = '0'.substr($this->uid, 0, 8).substr($this->uid, 9, 4).substr($this->uid, 15, 3);

return BinaryUtil::timeToFloat($time);
return BinaryUtil::timeToDateTime('0'.substr($this->uid, 0, 8).substr($this->uid, 9, 4).substr($this->uid, 15, 3));
}

public function getNode(): string
Expand Down
0