1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
package Twitter::API::Util;
# ABSTRACT: Utilities for working with the Twitter API
$Twitter::API::Util::VERSION = '1.0006';
use 5.14.1;
use warnings;
use Carp qw/croak/;
use Scalar::Util qw/blessed/;
use Time::Local qw/timegm/;
use Try::Tiny;
use namespace::clean;
use Sub::Exporter::Progressive -setup => {
exports => [ qw/
is_twitter_api_error
timestamp_to_gmtime
timestamp_to_localtime
timestamp_to_time
/],
};
sub is_twitter_api_error {
blessed($_[0]) && $_[0]->isa('Twitter::API::Error');
}
my %month;
@month{qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/} = 0..11;
sub _parse_ts {
local $_ = shift() // return;
# "Wed Jun 06 20:07:10 +0000 2012"
my ( $M, $d, $h, $m, $s, $y ) = /
^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)
\ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)
\ (\d\d)\ (\d\d):(\d\d):(\d\d)
\ \+0000\ (\d{4})$
/x or return;
return ( $s, $m, $h, $d, $month{$M}, $y - 1900 );
};
sub timestamp_to_gmtime { gmtime timestamp_to_time($_[0]) }
sub timestamp_to_localtime { localtime timestamp_to_time($_[0]) }
sub timestamp_to_time {
my $ts = shift // return undef;
my @t = _parse_ts($ts) or croak "invalid timestamp: $ts";
timegm @t;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Twitter::API::Util - Utilities for working with the Twitter API
=head1 VERSION
version 1.0006
=head1 SYNOPSIS
use Twitter::API::Util ':all';
# Given a timestamp in Twitter's text format:
my $ts = $status->{created_at}; # "Wed Jun 06 20:07:10 +0000 2012"
# Convert it UNIX epoch seconds (a Perl "time" value):
my $time = timestamp_to_time($status->{created_at});
# Or a Perl localtime:
my $utc = timestamp_to_timepiece($status->{created_at});
# Or a Perl gmtime:
my $utc = timestamp_to_gmtime($status->{created_at});
# Check to see if an exception is a Twitter::API::Error
if ( is_twitter_api_error($@) ) {
warn "Twitter API error: " . $@->twitter_error_text;
}
=head1 DESCRIPTION
Exports helpful utility functions.
=head1 METHODS
=head2 timestamp_to_gmtime
Returns C<gmtime> from a Twitter timestamp string. See L<perlfunc/gmtime-EXPR>
for details.
=head2 timestamp_to_localtime
Returns C<localtime> for a Twitter timestamp string. See
L<perlfunc/localtime-EXPR> for details.
=head2 timestamp_to_time
Returns a UNIX epoch time for a Twitter timestamp string. See L<perlfunc/time>
for details.
=head2 is_twitter_api_error
Returns true if the scalar passed to it is a L<Twitter::API::Error>. Otherwise,
it returns false.
=head1 AUTHOR
Marc Mims <marc@questright.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2015-2021 by Marc Mims.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|