User:AnomieBOT/source/d/Timestamp.pm

From Wikipedia, the free encyclopedia
< User:AnomieBOT‎ | source‎ | d
package d::Timestamp;

use utf8;
use strict;
use Time::Local;

=pod

=head1 NAME

d::Timestamp - AnomieBOT timestamp handling decorator

=head1 SYNOPSIS

 use AnomieBOT::API;

 $api = new AnomieBOT::API('conf.ini', 1);
 $api->decorators(qw/d::Timestamp/);

=head1 DESCRIPTION

C<d::Timestamp> contains timestamp-related utility functions for use by an
AnomieBOT task. When "d::Timestamp" is used as a decorator on the API object,
the following methods are available.

=head1 METHODS PROVIDED

=over

=item $api->ISO2timestamp( $ts )

Convert an ISO-format timestamp into a standard epoch value.

=item $api->timestamp2ISO( $ts )

Convert an epoch value to an ISO-format timestamp.

=cut

sub ISO2timestamp {
    my $api=shift;
    my $t=shift;
    return undef unless defined($t);
    return timegm($6,$5,$4,$3,$2-1,$1-1900)
        if $t=~/^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z$/;
    return undef;
}

sub timestamp2ISO {
    my $api=shift;
    my @t=gmtime(shift);
    return undef unless @t;
    return sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ',
                   $t[5]+1900, $t[4]+1, $t[3], $t[2], $t[1], $t[0]);
}

1;

=pod

=back

=head1 COPYRIGHT

Copyright 20082013 Anomie

This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.