Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a935bb

Browse files
bug #40003 [Uid] Fix time to float conversion (fancyweb)
This PR was merged into the 5.2 branch. Discussion ---------- [Uid] Fix time to float conversion | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - On 32b systems, when the timestamp is before the UNIX epoch, the result is currently shifted by 1. Inverting all the bits is not enough, we need to add 1. I guess https://en.wikipedia.org/wiki/Two%27s_complement is relevant here? Alternative: ```php $time = -1 * self::toBase(self::add($time ^ "\xff\xff\xff\xff\xff\xff\xff\xff", "\x00\x00\x00\x00\x00\x00\x00\x01"), self::BASE10); ``` Commits ------- 9680a27 [Uid] Fix time to float conversion
2 parents ab48b7b + 9680a27 commit 6a935bb

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Symfony/Component/Uid/BinaryUtil.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public static function timeToFloat(string $time): float
124124
$time = self::add($time, self::TIME_OFFSET_COM2);
125125

126126
if ($time >= self::TIME_OFFSET_COM2) {
127-
$time = -1 * self::toBase($time ^ "\xff\xff\xff\xff\xff\xff\xff\xff", self::BASE10);
127+
$time = -1 * (self::toBase($time ^ "\xff\xff\xff\xff\xff\xff\xff\xff", self::BASE10) + 1);
128128
} else {
129129
$time[0] = $time[0] & "\x7F";
130130
$time = self::toBase($time, self::BASE10);

src/Symfony/Component/Uid/Tests/UuidTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,13 @@ public function testFromStringOnExtendedClassReturnsStatic()
199199
{
200200
$this->assertInstanceOf(CustomUuid::class, CustomUuid::fromString(self::A_UUID_V4));
201201
}
202+
203+
public function testGetTime()
204+
{
205+
$this->assertSame(103072857660.6847, ((new UuidV1('ffffffff-ffff-1fff-a456-426655440000'))->getTime()));
206+
$this->assertSame(0.0000001, ((new UuidV1('13814001-1dd2-11b2-a456-426655440000'))->getTime()));
207+
$this->assertSame(0.0, (new UuidV1('13814000-1dd2-11b2-a456-426655440000'))->getTime());
208+
$this->assertSame(-0.0000001, (new UuidV1('13813fff-1dd2-11b2-a456-426655440000'))->getTime());
209+
$this->assertSame(-12219292800.0, ((new UuidV1('00000000-0000-1000-a456-426655440000'))->getTime()));
210+
}
202211
}

0 commit comments

Comments
 (0)