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;
    }
}

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

    return unless $config->{'no-re'};

    for my $no_re (@{$config->{'no-re'}}) {
        return 1 if $line =~ m!$no_re!;
    }
}

sub _replace_unixtime {
    my ($maybe_unixtime, $line_ref, $config) = @_;

    if ($maybe_unixtime > 2**31-1) {
        return;
    }

    if ($config->{'min-time'} && $maybe_unixtime < $config->{'min-time'}) {
        return;
    }

    my $date = strftime($config->{format}, localtime($maybe_unixtime));
    my $replaced_unixtime = sprintf(
        "%s%s%s%s",
        $config->{'replace'} ? '' : $maybe_unixtime,
        $config->{'start-bracket'},
        $date,
        $config->{'end-bracket'},
    );

    $$line_ref =~ s/$maybe_unixtime/$replaced_unixtime/;
}

sub from_unixtime {
    my ($lines, @argv) = @_;

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

    my @replaced_lines;
    for my $line ( split /\n/, $lines ) {
        if ( my $match = _may_replace($line, $config) ) {
            _replace_unixtime($match => \$line, $config);
        }
        push @replaced_lines, $line;
    }
    return join("\n", @replaced_lines);
}

sub _get_options {
    my ($config, $argv) = @_;

    GetOptionsFromArray(
        $argv,
        'f|format=s'      => \$config->{format},
        'start-bracket=s' => \$config->{'start-bracket'},
        'end-bracket=s'   => \$config->{'end-bracket'},
        're=s@'           => \$config->{re},
        'no-re=s@'        => \$config->{'no-re'},
        'min-time=i'      => \$config->{'min-time'},
        'replace'         => \$config->{'replace'},



( run in 1.473 second using v1.01-cache-2.11-cpan-9581c071862 )