#!/usr/bin/env perl

# This chunk of stuff was generated by App::FatPacker. To find the original
# file's code, look for the end of this BEGIN block or the string 'FATPACK'
BEGIN {
my %fatpacked;

$fatpacked{"App/FromUnixtime.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'APP_FROMUNIXTIME';
  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.14';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",$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'},'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;
APP_FROMUNIXTIME

s/^  //mg for values %fatpacked;

my $class = 'FatPacked::'.(0+\%fatpacked);
no strict 'refs';
*{"${class}::files"} = sub { keys %{$_[0]} };

if ($] < 5.008) {
  *{"${class}::INC"} = sub {
     if (my $fat = $_[0]{$_[1]}) {
       return sub {
         return 0 unless length $fat;
         $fat =~ s/^([^\n]*\n?)//;
         $_ = $1;
         return 1;
       };
     }
     return;
  };
}

else {
  *{"${class}::INC"} = sub {
    if (my $fat = $_[0]{$_[1]}) {
      open my $fh, '<', \$fat
        or die "FatPacker error loading $_[1] (could be a perl installation issue?)";
      return $fh;
    }
    return;
  };
}

unshift @INC, bless \%fatpacked, $class;
  } # END OF FATPACK CODE

use strict;
use warnings;
use App::FromUnixtime;

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

__END__

=head1 NAME

from_unixtime - to convert from unixtime to date suitably

=head1 SYNOPSIS

replace unixtime args

    $ from_unixtime 1419702037
    1419702037(Sun, 28 Dec 2014 02:40:37 +0900)

or filter unixtime suitably

    $ cat sql_result
    id    1
    value         1419702037
    created_at    1419702037
    updated_at    1419702037

    $ cat sql_result | from_unixtime
    id    1
    value         1419702037
    created_at    1419702037(Sun, 28 Dec 2014 02:40:37 +0900)
    updated_at    1419702037(Sun, 28 Dec 2014 02:40:37 +0900)

=head2 OPTIONS

    -f   --format       date format // '%a, %d %b %Y %H:%M:%S %z'

    --start-bracket     change start bracket // '('
    --end-bracket       change end bracket   // ')'

    --re                additional column condition
    --no-re             no replace condition(this option has overriding priority)
    --min-time          no replace if the value is smaller than this option

=head3 EXAMPLES

To edit the date format.

    $ cat sql_result | from_unixtime --format '%Y/%m/%d %H:%M:%S'
    id    1
    value         1419702037
    created_at    1419702037(2014/12/28 02:40:37)
    updated_at    1419702037(2014/12/28 02:40:37)

Also replace C<value> line with '--re' option.

    $ cat sql_result | from_unixtime --re value
    id    1
    value         1419702037(Sun, 28 Dec 2014 02:40:37 +0900)
    created_at    1419702037(Sun, 28 Dec 2014 02:40:37 +0900)
    updated_at    1419702037(Sun, 28 Dec 2014 02:40:37 +0900)

If you do not want to replace then you use '--no-re' option

    $ cat sql_result | from_unixtime --no-re updated
    id    1
    value         1419702037
    created_at    1419702037(Sun, 28 Dec 2014 02:40:37 +0900)
    updated_at    1419702037


=head2 CONFIGURATION

`from_unixtime` command will look for a configuration file before reading its command line parameters. This function depends on L<Config::CmdRC>.

The configuration file is `.from_unixtimerc`. And the location of a configuration file is C</etc> or C<$HOME> but if the C<CMDRC_DIR> environment variable is set, `from_unixtime` will look for configuration file in that directory or C<current directory>.

A sample configuration file might read:

    format: %Y/%m/%d %H:%M:%S
    start-bracket: ' ('


=head1 AUTHOR

Dai Okabayashi E<lt>bayashi@cpan.orgE<gt>

=head1 SEE ALSO

L<App::FromUnixtime>, L<Config::CmdRC>

=head1 LICENSE

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.

=cut
