| 1 | package UUID::V7; | ||||||||||
| 2 | 2 | $UUID::V7::VERSION = '0.003'; | |||||||||
| 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 | my $clock = POSIX::RT::Clock->new('realtime'); | |||||||||
| 15 | 2 | my $nano_to_milli = 1_000_000; | |||||||||
| 16 | 2 | my $format = 'H8H4H4H4H12'; | |||||||||
| 17 | 2 | my $to_12_bit = 245; # $nano_to_mill / 245 == 4081 | |||||||||
| 18 | 2 | my $uuid_v7_marker = 0x7000; | |||||||||
| 19 | |||||||||||
| 20 | 1000001 | ▶ | sub generate_uuid_v7 { | ||||||||
|
Subroutine
generate_uuid_v7: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | 2 | ▶ | use integer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1000001 | my $time = $clock->get_time; | |||||||||
| 23 | 1000001 | my $nanoseconds = $time->nsec; | |||||||||
| 24 | 1000001 | my $milliseconds = $time->sec * 1000 + $nanoseconds / $nano_to_milli; | |||||||||
| 25 | 1000001 | my $sub_milliseconds = $nanoseconds % $nano_to_milli / $to_12_bit; | |||||||||
| 26 | |||||||||||
| 27 | 1000001 | my $high = ($milliseconds << 16) + $sub_milliseconds | $uuid_v7_marker; | |||||||||
| 28 | 1000001 | my $result = pack 'Q>A8', $high, random_bytes(8); | |||||||||
| 29 | 1000001 | vec($result, 35, 2) = 2; # RFC 9562 variant | |||||||||
| 30 | |||||||||||
| 31 | 1000001 | return join '-', unpack $format, $result; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 1 | ▶ | sub extract_uuid_v7_time { | ||||||||
|
Subroutine
extract_uuid_v7_time: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 35 | 1 | my ($uuid) = @_; | |||||||||
| 36 | 1 | my $binary = uuid_string_to_binary($uuid); | |||||||||
| 37 | 1 | ▶ | croak 'UUID is not version 7' if vec($binary, 13, 4) != 7; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | |||||||||||
| 39 | 1 | my $first = unpack 'Q>', $binary; | |||||||||
| 40 | 1 | my $time = $first >> 16; | |||||||||
| 41 | 1 | $time += ($first & 0x0FFF) * $to_12_bit / $nano_to_milli; | |||||||||
| 42 | 1 | return $time / 1000; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 3 | ▶ | sub uuid_string_to_binary { | ||||||||
|
Subroutine
uuid_string_to_binary: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 46 | 3 | my ($uuid) = @_; | |||||||||
| 47 | 3 | ▶ | croak 'UUID has incorrect length' if length $uuid != 36; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 3 | return pack $format, split /-/, $uuid; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 1 | ▶ | sub uuid_binary_to_string { | ||||||||
|
Subroutine
uuid_binary_to_string: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 52 | 1 | my ($input) = @_; | |||||||||
| 53 | 1 | ▶ | croak 'UUID binary has incorrect length' if length $input != 16; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 1 | return join '-', unpack $format, $input; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 1 | ▶ | sub is_uuid_v7 { | ||||||||
|
Subroutine
is_uuid_v7: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 58 | 1 | my ($uuid) = @_; | |||||||||
| 59 | 1 | my $binary = uuid_string_to_binary($uuid); | |||||||||
| 60 | 1 | return vec($binary, 13, 4) == 7; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 2 | 1; | |||||||||
| 64 | |||||||||||
| 65 | # ABSTRACT: A precise UUIDv7 implementation | ||||||||||
| 66 | |||||||||||
| 67 | __END__ | ||||||||||