| 1 | 4 | ▶ | use v5.26; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Twitter::ID; | ||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Parse the date from a Twitter Snowflake ID | ||||||||||||||||||||||||||||
| 6 | 4 | $Twitter::ID::VERSION = '1.00'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | my $TW_EPOCH = 1288834974657; | |||||||||||||||||||||||||||
| 12 | 4 | my $WORKER_BITS = 10; | |||||||||||||||||||||||||||
| 13 | 4 | my $SEQUENCE_BITS = 12; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | my $TIMESTAMP_SHIFT = $WORKER_BITS + $SEQUENCE_BITS; | |||||||||||||||||||||||||||
| 16 | 4 | my $WORKER_MASK = (-1 ^ (-1 << $WORKER_BITS)) << $SEQUENCE_BITS; | |||||||||||||||||||||||||||
| 17 | 4 | my $SEQUENCE_MASK = -1 ^ (-1 << $SEQUENCE_BITS); | |||||||||||||||||||||||||||
| 18 | 4 | my $MAX_WORKER = 1 << $WORKER_BITS; | |||||||||||||||||||||||||||
| 19 | 4 | my $MAX_SEQUENCE = 1 << $SEQUENCE_BITS; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | my $LAST_PRE_SNOWFLAKE_ID = 29700859247; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 18 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 18 | my ($class, $id) = @_; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 18 | ▶ | if (ref $id eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 8 | my $self = bless \(my $o = 0), $class; | |||||||||||||||||||||||||||
| 29 | 8 | $self->_set( $id->{timestamp}, $id->{worker}, $id->{sequence} ); | |||||||||||||||||||||||||||
| 30 | 2 | return $self; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 10 | ▶ | if (! $id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 2 | return bless \(my $o = 0), $class; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 8 | bless \$id, $class; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 11 | ▶ | sub _set { | ||||||||||||||||||||||||||
|
Subroutine
_set: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 42 | 11 | my ($self, $timestamp, $worker, $sequence) = @_; | |||||||||||||||||||||||||||
| 43 | 11 | ▶ | $timestamp //= $TW_EPOCH; | ||||||||||||||||||||||||||
|
Condition66%2/3
$timestamp //= $TW_EPOCH
MC/DC0%0/2
$timestamp //= $TW_EPOCH
$timestamp
$TW_EPOCH
Truth table66%2/3
A //= B
A= $timestampB= $TW_EPOCH
| |||||||||||||||||||||||||||||
| 44 | 11 | ▶ | $worker //= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$worker //= 0
MC/DC100%1/1
$worker //= 0
$worker
Truth table100%2/2
A
A= $worker
| |||||||||||||||||||||||||||||
| 45 | 11 | ▶ | $sequence //= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$sequence //= 0
MC/DC100%1/1
$sequence //= 0
$sequence
Truth table100%2/2
A
A= $sequence
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 11 | ▶ | croak "Twitter timestamps before $TW_EPOCH unsupported" if $timestamp < $TW_EPOCH; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 9 | ▶ | croak "Twitter ID components must be positive" if $worker < 0 || $sequence < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$worker < 0 or $sequence < 0
MC/DC100%2/2
$worker < 0 or $sequence < 0
$worker < 0
$sequence < 0
Truth table100%3/3
A or B
A= $worker < 0B= $sequence < 0
| |||||||||||||||||||||||||||||
| 49 | 7 | ▶ | croak "Worker ID $worker too large (max $MAX_WORKER)" if $worker >= $MAX_WORKER; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 6 | ▶ | croak "Sequence number $sequence too large (max $MAX_SEQUENCE)" if $sequence >= $MAX_SEQUENCE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 5 | $$self = ($timestamp - $TW_EPOCH) << $TIMESTAMP_SHIFT | |||||||||||||||||||||||||||
| 53 | | $worker << $SEQUENCE_BITS | ||||||||||||||||||||||||||||
| 54 | | $sequence; | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 11 | ▶ | sub timestamp { | ||||||||||||||||||||||||||
|
Subroutine
timestamp: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 11 | my ($self, $timestamp) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 11 | ▶ | if (defined $timestamp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | $self->_set( $timestamp, $self->worker, $self->sequence ); | |||||||||||||||||||||||||||
| 63 | 1 | return; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 10 | ▶ | return if $$self <= $LAST_PRE_SNOWFLAKE_ID; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 6 | return ($$self >> $TIMESTAMP_SHIFT) + $TW_EPOCH; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 8 | ▶ | sub worker { | ||||||||||||||||||||||||||
|
Subroutine
worker: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 8 | my ($self, $worker) = @_; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 8 | ▶ | if (defined $worker) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | $self->_set( $self->timestamp, $worker, $self->sequence ); | |||||||||||||||||||||||||||
| 77 | 1 | return; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 7 | ▶ | return if $$self <= $LAST_PRE_SNOWFLAKE_ID; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | return ($$self & $WORKER_MASK) >> $SEQUENCE_BITS; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 8 | ▶ | sub sequence { | ||||||||||||||||||||||||||
|
Subroutine
sequence: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 8 | my ($self, $sequence) = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 8 | ▶ | if (defined $sequence) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 1 | $self->_set( $self->timestamp, $self->worker, $sequence ); | |||||||||||||||||||||||||||
| 91 | 1 | return; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 7 | ▶ | return if $$self <= $LAST_PRE_SNOWFLAKE_ID; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 3 | return $$self & $SEQUENCE_MASK; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 4 | ▶ | sub epoch { | ||||||||||||||||||||||||||
|
Subroutine
epoch: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 4 | my ($self, $epoch) = @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 4 | ▶ | croak "epoch() is read-only" if defined $epoch; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | my $timestamp = $self->timestamp; | |||||||||||||||||||||||||||
| 106 | 3 | ▶ | return unless defined $timestamp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 2 | return $timestamp / 1000; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 4 | 1; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | __END__ | ||||||||||||||||||||||||||||