Skip to content

Commit 3ba6c93

Browse files
authored
Fix UUIDv6 getTime() near Unix Epoch (#36)
* Fix UUIDv6 `getTime()` near Unix Epoch
1 parent 389e788 commit 3ba6c93

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/UUID.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ public static function getTime(string $uuid): ?string
260260
$timehex = '0' . substr($uuid, 0, 12) . substr($uuid, 13, 3);
261261
$retval = null;
262262
if ($version === 6) {
263-
$retval = substr_replace(strval(hexdec($timehex) - self::TIME_OFFSET_INT), '.', -7, 0);
263+
$retval = strval(hexdec($timehex) - self::TIME_OFFSET_INT);
264+
$retval = substr_replace(str_pad($retval, 8, '0', \STR_PAD_LEFT), '.', -7, 0);
264265
} elseif ($version === 7) {
265266
$unixts = hexdec(substr($timehex, 0, 10));
266267
$subsec = str_pad(strval(self::decodeSubsec(hexdec(substr($timehex, 10)))), 7, '0', \STR_PAD_LEFT);

tests/UuidTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,10 @@ public function testGetTimeNull()
212212
$uuid4_time = UUID::getTime(UUID::uuid4());
213213
$this->assertNull($uuid4_time);
214214
}
215+
216+
public function testGetTimeNearEpoch()
217+
{
218+
$uuid6_time = UUID::getTime('1b21dd21-3814-6001-76fa-54fb559c5fcd');
219+
$this->assertSame($uuid6_time, '0.0000001');
220+
}
215221
}

0 commit comments

Comments
 (0)