NAME

Twitter::ID - Parse the date from a Twitter Snowflake ID

VERSION

version 1.00

SYNOPSIS

# https://twitter.com/Twitter/status/1445078208190291973
my $tid = Twitter::ID->new( 1445078208190291973 );

use Time::Piece;
say Time::Piece->new( $tid->epoch );
# Mon Oct  4 19:27:47 2021

DESCRIPTION

This Perl module allows calculating the date from a "Snowflake" Twitter ID. Does not use the Twitter API.

METHODS

epoch

$posix_seconds = $tid->epoch;

Convenience method to retrieve the seconds since the POSIX epoch. Suitable to be directly passed on to e. g. Time::Piece or DateTime for further processing.

For Twitter IDs from before the introduction of "Snowflake" IDs, this method currently returns an undefined value.

new

$tid = Twitter::ID->new( 474971393852182528 );
$tid = Twitter::ID->new({
  sequence => 0,
  timestamp => 1402076979493,
  worker => 129,
});

Creates a new Twitter ID object. Accepts either the scalar Twitter ID or a hash reference with the ID components (millisecond POSIX timestamp, worker ID, sequence number).

sequence

$sequence = $tid->sequence; $tid->sequence( $sequence );

Reads or writes the ID's sequence number component. The sequence number is a positive integer ranging from 0 to 4095.

For Twitter IDs from before the introduction of "Snowflake" IDs, this method returns an undefined value.

timestamp

$posix_milliseconds = $tid->timestamp;
$tid->timestamp( $posix_milliseconds );

Reads or writes the ID's timestamp component. The timestamp is in milliseconds since the POSIX epoch. Timestamps from before the introduction of "Snowflake" IDs in late 2010 are unsupported.

For Twitter IDs from before the introduction of "Snowflake" IDs, this method returns an undefined value.

worker

$worker = $tid->worker; $tid->worker( $worker );

Reads or writes the ID's worker ident component. The worker ident is a positive integer ranging from 0 to 1023. Its five most significant bits represent the encoded Twitter datacenter number.

For Twitter IDs from before the introduction of "Snowflake" IDs, this method returns an undefined value.

BUGS AND LIMITATIONS

This module currently requires and expects a Perl that is compiled with 64-bit integer support.

Twitter IDs from before the introduction of "Snowflake" IDs in late 2010 are not currently supported by this software. The creation time of such IDs may be estimated with the (unrelated) web service TweetedAt. I myself have no need to parse Twitter IDs from 2010 or earlier and likely won't be able to justify spending time on adding such a feature to this module, but I'd be happy to accept patches or grant co-maintainer status.

SEE ALSO

https://developer.twitter.com/en/docs/twitter-ids

https://github.com/twitter-archive/snowflake/blob/b3f6a3c6ca/src/main/scala/com/twitter/service/snowflake/IdWorker.scala

https://ws-dl.blogspot.com/2019/08/2019-08-03-tweetedat-finding-tweet.html

AUTHOR

Arne Johannessen <ajnn@cpan.org>

If you contact me by email, please make sure you include the word "Perl" in your subject header to help beat the spam filters.

COPYRIGHT AND LICENSE

This software is Copyright (c) 2022 by Arne Johannessen.

This is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0 or (at your option) the same terms as the Perl 5 programming language system itself.