App-FromUnixtime

 view release on metacpan or  search on metacpan

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

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'},
        'h|help' => sub {
            _show_usage(1);
        },
        'v|version' => sub {
            print "$0 $VERSION\n";
            exit 1;
        },
    ) or _show_usage(2);

    _validate_options($config, $argv);
}

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

    $config->{format} ||= RC->{format} || $DEFAULT_DATE_FORMAT;
    $config->{'start-bracket'} ||= RC->{'start-bracket'} || '(';
    $config->{'end-bracket'}   ||= RC->{'end-bracket'}   || ')';
    if (ref RC->{re} eq 'ARRAY') {
        push @{$config->{re}}, @{RC->{re}};
    }
    elsif (RC->{re}) {
        push @{$config->{re}}, RC->{re};
    }
    if ($config->{re}) {
        $config->{_re} = join '|', map { quotemeta $_;  } @{$config->{re}};
    }
    push @{$config->{unixtime}}, @{$argv};
}

sub _show_usage {
    my $exitval = shift;

    require Pod::Usage;
    Pod::Usage::pod2usage(-exitval => $exitval);
}

1;

__END__

=head1 NAME

App::FromUnixtime - to convert from unixtime to date suitably


=head1 SYNOPSIS

    use App::FromUnixtime;

    App::FromUnixtime->run(@ARGV);


=head1 DESCRIPTION

C<App::FromUnixtime> provides the L<from_unixtime> command and the B<from_unixtime> function.


=head1 METHOD

=head2 run

run to convert process


=head1 EXPORT FUNCTION

=head2 from_unixtime($line, @options)

C<App::FromUnixtime> exports B<from_unixtime> function for converting the string that may be included unixtime.

    use App::FromUnixtime;



( run in 2.287 seconds using v1.01-cache-2.11-cpan-5837b0d9d2c )