| 1 | package UUID::V7; | ||||||||||
| 2 | 2 | $UUID::V7::VERSION = '0.005'; | |||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | our @EXPORT_OK = qw/generate_uuid_v7 extract_uuid_v7_time uuid_string_to_binary uuid_binary_to_string is_uuid_v7/; | |||||||||
| 8 | 2 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use Crypt::SysRandom 'random_bytes'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use POSIX::RT::Clock 0.024; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | use constant RESOLUTION => POSIX::RT::Clock->new('realtime')->get_resolution->nsec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 2 | my $clock = POSIX::RT::Clock->new('realtime'); | |||||||||
| 17 | 2 | my $nano_per_milli = 1_000_000; | |||||||||
| 18 | 2 | my $format = 'H8H4H4H4H12'; | |||||||||
| 19 | 2 | my $submilli_modulus = 1 << 12; | |||||||||
| 20 | 2 | my $uuid_v7_marker = 0x7000; | |||||||||
| 21 | |||||||||||
| 22 | 1000001 | ▶ | sub generate_uuid_v7 { | ||||||||
|
Subroutine
generate_uuid_v7: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 23 | 2 | ▶ | use integer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 1000001 | my $time = $clock->get_time; | |||||||||
| 25 | 1000001 | my $nanoseconds = $time->nsec; | |||||||||
| 26 | 1000001 | my $milliseconds = $time->sec * 1000 + $nanoseconds / $nano_per_milli; | |||||||||
| 27 | 1000001 | my $sub_milliseconds = $nanoseconds % $nano_per_milli * $submilli_modulus / $nano_per_milli; | |||||||||
| 28 | |||||||||||
| 29 | 1000001 | my $high = ($milliseconds << 16) + $sub_milliseconds | $uuid_v7_marker; | |||||||||
| 30 | 1000001 | my $result = pack 'Q>A8', $high, random_bytes(8); | |||||||||
| 31 | 1000001 | vec($result, 28, 2) ^= vec($result, 35, 2) if RESOLUTION >= 1000; # lowest two bits of timestamp set to unused randomness. | |||||||||
| 32 | 1000001 | vec($result, 35, 2) = 2; # RFC 9562 variant | |||||||||
| 33 | |||||||||||
| 34 | 1000001 | return join '-', unpack $format, $result; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 1 | ▶ | sub extract_uuid_v7_time { | ||||||||
|
Subroutine
extract_uuid_v7_time: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 38 | 1 | my ($uuid) = @_; | |||||||||
| 39 | 1 | my $binary = uuid_string_to_binary($uuid); | |||||||||
| 40 | 1 | ▶ | croak 'UUID is not version 7' if vec($binary, 13, 4) != 7; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | 1 | my $high = unpack 'Q>', $binary; | |||||||||
| 43 | 1 | my $time = $high >> 16; | |||||||||
| 44 | 1 | $time += ($high & 0x0FFF) / $submilli_modulus; | |||||||||
| 45 | 1 | return $time / 1000; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 3 | ▶ | sub uuid_string_to_binary { | ||||||||
|
Subroutine
uuid_string_to_binary: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 49 | 3 | my ($uuid) = @_; | |||||||||
| 50 | 3 | ▶ | croak 'UUID has incorrect length' if length $uuid != 36; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 3 | return pack $format, split /-/, $uuid; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 1 | ▶ | sub uuid_binary_to_string { | ||||||||
|
Subroutine
uuid_binary_to_string: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 55 | 1 | my ($input) = @_; | |||||||||
| 56 | 1 | ▶ | croak 'UUID binary has incorrect length' if length $input != 16; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 1 | return join '-', unpack $format, $input; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 1 | ▶ | sub is_uuid_v7 { | ||||||||
|
Subroutine
is_uuid_v7: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 61 | 1 | my ($uuid) = @_; | |||||||||
| 62 | 1 | my $binary = uuid_string_to_binary($uuid); | |||||||||
| 63 | 1 | return vec($binary, 13, 4) == 7; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 2 | 1; | |||||||||
| 67 | |||||||||||
| 68 | # ABSTRACT: A precise UUIDv7 implementation | ||||||||||
| 69 | |||||||||||
| 70 | __END__ | ||||||||||