Skip to content

Commit f625a0c

Browse files
authored
Fix getTime() with UUIDv7 (#51)
1 parent b366dd9 commit f625a0c

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/UUID.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public static function getTime(string $uuid): ?string
271271
$retval .= substr_replace(str_pad(strval($ts), 8, '0', \STR_PAD_LEFT), '.', -7, 0);
272272
} elseif ($version === 7) {
273273
$unixts = hexdec(substr($timehex, 0, 13));
274-
$subsec = self::decodeSubsec(hexdec(substr($timehex, 13)) + (hexdec(substr($uuid, 16, 1)) >> 4 & 0x03));
274+
$subsec = self::decodeSubsec((hexdec(substr($timehex, 13)) << 2) + (hexdec(substr($uuid, 16, 1)) & 0x03));
275275
$retval = strval($unixts * self::V7_SUBSEC_RANGE + $subsec);
276276
$retval = substr_replace(str_pad($retval, 8, '0', \STR_PAD_LEFT), '.', -7, 0);
277277
}

tests/UuidTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public function testCanGenerateValidVersion6()
5858
$uuid1,
5959
$uuid2
6060
);
61+
$this->assertGreaterThan(
62+
UUID::getTime($uuid1),
63+
UUID::getTime($uuid2)
64+
);
6165
$this->assertLessThan(
6266
0,
6367
UUID::cmp($uuid1, $uuid2)
@@ -79,6 +83,10 @@ public function testCanGenerateValidVersion7()
7983
$uuid1,
8084
$uuid2
8185
);
86+
$this->assertGreaterThan(
87+
UUID::getTime($uuid1),
88+
UUID::getTime($uuid2)
89+
);
8290
$this->assertLessThan(
8391
0,
8492
UUID::cmp($uuid1, $uuid2)
@@ -238,6 +246,6 @@ public function testGetTimeMax()
238246
$uuid6_time = UUID::getTime('ffffffff-ffff-6fff-bfff-ffffffffffff');
239247
$this->assertSame('103072857660.6846975', $uuid6_time);
240248
$uuid7_time = UUID::getTime('ffffffff-ffff-7fff-bfff-ffffffffffff');
241-
$this->assertSame('281474976710.6552500', $uuid7_time);
249+
$this->assertSame('281474976710.6560000', $uuid7_time);
242250
}
243251
}

0 commit comments

Comments
 (0)