Skip to content

Commit e3e6f37

Browse files
authored
add getTime() (#28)
* add `getTime()`
1 parent 5df68ba commit e3e6f37

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,10 @@ $cmp3 = UUID::cmp(
110110
'{C4A760A8-DBCF-5254-A0D9-6A4474BD1B62}'
111111
);
112112
var_dump($cmp3 === 0); // bool(true)
113+
114+
// Extract Unix time from versions 6 and 7 as a string.
115+
$uuid6_time = UUID::getTime(UUID::uuid6());
116+
var_dump($uuid6_time); // e.g. string(18) "1639860190.2801270"
117+
$uuid7_time = UUID::getTime(UUID::uuid7());
118+
var_dump($uuid7_time); // e.g. string(18) "1639860190.2801320"
113119
```

src/UUID.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,12 @@ public static function uuid6(): string
208208
public static function uuid7(): string
209209
{
210210
[$unixts, $subsec] = self::getUnixTime();
211-
$uhex = str_pad(dechex($unixts), 9, '0', \STR_PAD_LEFT);
211+
$uhex = substr(str_pad(dechex($unixts), 9, '0', \STR_PAD_LEFT), -9);
212212
$shex = str_pad(dechex($subsec), 6, '0', \STR_PAD_LEFT);
213213
$uhex .= sprintf(
214214
'%03s7%03s',
215215
substr($shex, 0, 3),
216-
substr($shex, -3)
216+
substr($shex, 3, 3)
217217
);
218218
$uhex .= bin2hex(random_bytes(8));
219219
return self::uuidFromHex($uhex, 7);
@@ -242,6 +242,28 @@ public static function equals(string $uuid1, string $uuid2): bool
242242
return self::getBytes($uuid1) === self::getBytes($uuid2);
243243
}
244244

245+
/**
246+
* Returns Unix time from a UUID.
247+
*
248+
* @param string $uuid The UUID string
249+
* @return string Unix time
250+
*/
251+
public static function getTime(string $uuid): ?string
252+
{
253+
$uuid = self::stripExtras($uuid);
254+
$version = self::getVersion($uuid);
255+
$timehex = '0' . substr($uuid, 0, 12) . substr($uuid, 13, 3);
256+
$retval = null;
257+
if ($version === 6) {
258+
$retval = substr_replace(strval(hexdec($timehex) - self::TIME_OFFSET_INT), '.', -7, 0);
259+
} elseif ($version === 7) {
260+
$unixts = hexdec(substr($timehex, 0, 10));
261+
$subsec = str_pad(strval(hexdec(substr($timehex, 10))), 7, '0', \STR_PAD_LEFT);
262+
$retval = $unixts . '.' . $subsec;
263+
}
264+
return $retval;
265+
}
266+
245267
/**
246268
* Returns the UUID version.
247269
*

tests/UuidTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,19 @@ public function testCanUseAliases()
180180
UUID::getVersion(UUID::v7())
181181
);
182182
}
183+
184+
public function testGetTimeValid()
185+
{
186+
$now = microtime(true);
187+
$uuid6 = UUID::uuid6();
188+
$uuid7 = UUID::uuid7();
189+
$this->assertEqualsWithDelta($now, UUID::getTime($uuid6), 0.001);
190+
$this->assertEqualsWithDelta($now, UUID::getTime($uuid7), 0.001);
191+
}
192+
193+
public function testGetTimeNull()
194+
{
195+
$uuid4_time = UUID::getTime(UUID::uuid4());
196+
$this->assertNull($uuid4_time);
197+
}
183198
}

0 commit comments

Comments
 (0)