App-FromUnixtime

 view release on metacpan or  search on metacpan

lib/App/FromUnixtime.pm  view on Meta::CPAN

package App::FromUnixtime;
use strict;
use warnings;
use Getopt::Long qw/GetOptionsFromArray/;
use IO::Interactive::Tiny;
use POSIX qw/strftime/;
use Config::CmdRC qw/.from_unixtimerc/;
use Exporter 'import';
our @EXPORT = qw/from_unixtime/;

our $VERSION = '0.17';

our $MAYBE_UNIXTIME = join '|', (
    'created_(?:at|on)',
    'updated_(?:at|on)',
    'released_(?:at|on)',
    'closed_(?:at|on)',
    'published_(?:at|on)',
    'expired_(?:at|on)',
    'date',
    'unixtime',
    '_time',
);

our $DEFAULT_DATE_FORMAT = '%a, %d %b %Y %H:%M:%S %z';

sub run {
    my $self = shift;
    my @argv = @_;

    my $config = +{};
    _get_options($config, \@argv);

    _main($config);
}

sub _main {
    my $config = shift;

    if ( ! IO::Interactive::Tiny::is_interactive(*STDIN) ) {
        while ( my $line = <STDIN> ) {
            chomp $line;
            if ( my $match = _may_replace($line, $config) ) {
                if ( ! _may_not_replace($line, $config) ) {
                    _replace_unixtime($match => \$line, $config);
                }
            }
            print "$line\n";
        }
    }
    else {
        for my $unixtime (@{$config->{unixtime}}) {
            _replace_unixtime($unixtime => \$unixtime, $config);
            print "$unixtime\n";
        }
    }
}

sub _may_replace {
    my ($line, $config) = @_;

    if ($line =~ m!(?:$MAYBE_UNIXTIME).*[^\d](\d+)!
                || ($config->{_re} && $line =~ m!(?:$config->{_re}).*[^\d](\d+)!)
                || $line =~ m!^[\s\t\r\n]*(\d+)[\s\t\r\n]*$!
    ) {
        return $1;
    }



( run in 2.416 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )