File Coverage

blib/lib/App/FromUnixtime.pm
Criterion Covered Total %
statement 82 89 92.1
branch 24 28 85.7
condition 19 28 67.8
subroutine 16 18 88.8
pod 2 2 100.0
total 143 165 86.6


line stmt bran cond sub pod time code
1             package App::FromUnixtime;
2 6     6   191947 use strict;
  6         13  
  6         185  
3 6     6   23 use warnings;
  6         7  
  6         169  
4 6     6   4393 use Getopt::Long qw/GetOptionsFromArray/;
  6         58484  
  6         33  
5 6     6   4403 use IO::Interactive::Tiny;
  6         50  
  6         180  
6 6     6   2710 use POSIX qw/strftime/;
  6         32095  
  6         40  
7 6     6   7615 use Config::CmdRC qw/.from_unixtimerc/;
  6         78137  
  6         37  
8 6     6   767 use Exporter 'import';
  6         10  
  6         5094  
9             our @EXPORT = qw/from_unixtime/;
10              
11             our $VERSION = '0.16';
12              
13             our $MAYBE_UNIXTIME = join '|', (
14             'created_(?:at|on)',
15             'updated_(?:at|on)',
16             'released_(?:at|on)',
17             'closed_(?:at|on)',
18             'published_(?:at|on)',
19             'expired_(?:at|on)',
20             'date',
21             'unixtime',
22             '_time',
23             );
24              
25             our $DEFAULT_DATE_FORMAT = '%a, %d %b %Y %H:%M:%S %z';
26              
27             sub run {
28 16     16 1 28103 my $self = shift;
29 16         27 my @argv = @_;
30              
31 16         23 my $config = +{};
32 16         38 _get_options($config, \@argv);
33              
34 16         29 _main($config);
35             }
36              
37             sub _main {
38 16     16   17 my $config = shift;
39              
40 16 50       45 if ( ! IO::Interactive::Tiny::is_interactive(*STDIN) ) {
41 16         203 while ( my $line = ) {
42 34         45 chomp $line;
43 34 100       55 if ( my $match = _may_replace($line, $config) ) {
44 13 100       35 if ( ! _may_not_replace($line, $config) ) {
45 12         25 _replace_unixtime($match => \$line, $config);
46             }
47             }
48 34         322784 print "$line\n";
49             }
50             }
51             else {
52 0         0 for my $unixtime (@{$config->{unixtime}}) {
  0         0  
53 0         0 _replace_unixtime($unixtime => \$unixtime, $config);
54 0         0 print "$unixtime\n";
55             }
56             }
57             }
58              
59             sub _may_replace {
60 39     39   60 my ($line, $config) = @_;
61              
62 39 100 100     866 if ($line =~ m!(?:$MAYBE_UNIXTIME).*[^\d](\d+)!
      66        
      100        
63             || ($config->{_re} && $line =~ m!(?:$config->{_re}).*[^\d](\d+)!)
64             || $line =~ m!^[\s\t\r\n]*(\d+)[\s\t\r\n]*$!
65             ) {
66 18         82 return $1;
67             }
68             }
69              
70             sub _may_not_replace {
71 13     13   17 my ($line, $config) = @_;
72              
73 13 100       44 return unless $config->{'no-re'};
74              
75 1         1 for my $no_re (@{$config->{'no-re'}}) {
  1         3  
76 1 50       12 return 1 if $line =~ m!$no_re!;
77             }
78             }
79              
80             sub _replace_unixtime {
81 17     17   20 my ($maybe_unixtime, $line_ref, $config) = @_;
82              
83 17 100       50 if ($maybe_unixtime > 2**31-1) {
84 1         2 return;
85             }
86              
87 16 100 66     86 if ($config->{'min-time'} && $maybe_unixtime < $config->{'min-time'}) {
88 1         2 return;
89             }
90              
91 15         1205 my $date = strftime($config->{format}, localtime($maybe_unixtime));
92 15 100       96 my $replaced_unixtime = sprintf(
93             "%s%s%s%s",
94             $config->{'replace'} ? '' : $maybe_unixtime,
95             $config->{'start-bracket'},
96             $date,
97             $config->{'end-bracket'},
98             );
99              
100 15         147 $$line_ref =~ s/$maybe_unixtime/$replaced_unixtime/;
101             }
102              
103             sub from_unixtime {
104 4     4 1 1231 my ($lines, @argv) = @_;
105              
106 4         6 my $config = +{};
107 4         9 _get_options($config, \@argv);
108              
109 4         3 my @replaced_lines;
110 4         12 for my $line ( split /\n/, $lines ) {
111 5 50       9 if ( my $match = _may_replace($line, $config) ) {
112 5         10 _replace_unixtime($match => \$line, $config);
113             }
114 5         15 push @replaced_lines, $line;
115             }
116 4         33 return join("\n", @replaced_lines);
117             }
118              
119             sub _get_options {
120 20     20   23 my ($config, $argv) = @_;
121              
122             GetOptionsFromArray(
123             $argv,
124             'f|format=s' => \$config->{format},
125             'start-bracket=s' => \$config->{'start-bracket'},
126             'end-bracket=s' => \$config->{'end-bracket'},
127             're=s@' => \$config->{re},
128             'no-re=s@' => \$config->{'no-re'},
129             'min-time=i' => \$config->{'min-time'},
130             'replace' => \$config->{'replace'},
131             'h|help' => sub {
132 0     0   0 _show_usage(1);
133             },
134             'v|version' => sub {
135 0     0   0 print "$0 $VERSION\n";
136 0         0 exit 1;
137             },
138 20 50       202 ) or _show_usage(2);
139              
140 20         9468 _validate_options($config, $argv);
141             }
142              
143             sub _validate_options {
144 20     20   30 my ($config, $argv) = @_;
145              
146 20   33     68 $config->{format} ||= RC->{format} || $DEFAULT_DATE_FORMAT;
      66        
147 20   50     151 $config->{'start-bracket'} ||= RC->{'start-bracket'} || '(';
      66        
148 20   50     124 $config->{'end-bracket'} ||= RC->{'end-bracket'} || ')';
      66        
149 20 100       112 if (ref RC->{re} eq 'ARRAY') {
    100          
150 1         4 push @{$config->{re}}, @{RC->{re}};
  1         2  
  1         2  
151             }
152             elsif (RC->{re}) {
153 1         6 push @{$config->{re}}, RC->{re};
  1         3  
154             }
155 20 100       129 if ($config->{re}) {
156 3         4 $config->{_re} = join '|', map { quotemeta $_; } @{$config->{re}};
  4         13  
  3         4  
157             }
158 20         20 push @{$config->{unixtime}}, @{$argv};
  20         59  
  20         37  
159             }
160              
161             sub _show_usage {
162 1     1   8 my $exitval = shift;
163              
164 1         544 require Pod::Usage;
165 1         36946 Pod::Usage::pod2usage(-exitval => $exitval);
166             }
167              
168             1;
169              
170             __END__