Skip to content

Commit 26d947b

Browse files
authored
Correctly calculate the maximum UUIDv7 time value (#38)
1 parent 3eb765c commit 26d947b

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

src/UUID.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ public static function getTime(string $uuid): ?string
269269
$retval .= substr_replace(str_pad(strval($ts), 8, '0', \STR_PAD_LEFT), '.', -7, 0);
270270
} elseif ($version === 7) {
271271
$unixts = hexdec(substr($timehex, 0, 10));
272-
$subsec = str_pad(strval(self::decodeSubsec(hexdec(substr($timehex, 10)))), 7, '0', \STR_PAD_LEFT);
273-
$retval = $unixts . '.' . $subsec;
272+
$subsec = self::decodeSubsec(hexdec(substr($timehex, 10)));
273+
$retval = substr_replace(str_pad(strval($unixts * 10 ** 7 + $subsec), 8, '0', \STR_PAD_LEFT), '.', -7, 0);
274274
}
275275
return $retval;
276276
}

tests/UuidTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,18 @@ public function testGetTimeNegativeNearEpoch()
224224
$uuid6_time = UUID::getTime('1b21dd21-3813-6fff-b678-1556dde9b80e');
225225
$this->assertSame($uuid6_time, '-0.0000001');
226226
}
227+
public function testGetTimeZero()
228+
{
229+
$uuid6_time = UUID::getTime('00000000-0000-6000-8000-000000000000');
230+
$this->assertSame($uuid6_time, '-12219292800.0000000');
231+
$uuid7_time = UUID::getTime('00000000-0000-7000-8000-000000000000');
232+
$this->assertSame($uuid7_time, '0.0000000');
233+
}
234+
public function testGetTimeMax()
235+
{
236+
$uuid6_time = UUID::getTime('ffffffff-ffff-6fff-bfff-ffffffffffff');
237+
$this->assertSame($uuid6_time, '103072857660.6846975');
238+
$uuid7_time = UUID::getTime('ffffffff-ffff-7fff-bfff-ffffffffffff');
239+
$this->assertSame($uuid7_time, '68719476736.0000000');
240+
}
227241
}

0 commit comments

Comments
 (0)