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   206473 use strict;
  6         13  
  6         195  
3 6     6   27 use warnings;
  6         7  
  6         187  
4 6     6   4611 use Getopt::Long qw/GetOptionsFromArray/;
  6         59522  
  6         30  
5 6     6   4134 use IO::Interactive::Tiny;
  6         48  
  6         176  
6 6     6   2646 use POSIX qw/strftime/;
  6         31937  
  6         32  
7 6     6   7556 use Config::CmdRC qw/.from_unixtimerc/;
  6         78043  
  6         36  
8 6     6   757 use Exporter 'import';
  6         8  
  6         5171  
9             our @EXPORT = qw/from_unixtime/;
10              
11             our $VERSION = '0.15';
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 27874 my $self = shift;
29 16         26 my @argv = @_;
30              
31 16         25 my $config = +{};
32 16         33 _get_options($config, \@argv);
33              
34 16         28 _main($config);
35             }
36              
37             sub _main {
38 16     16   19 my $config = shift;
39              
40 16 50       45 if ( ! IO::Interactive::Tiny::is_interactive(*STDIN) ) {
41 16         227 while ( my $line = ) {
42 34         45 chomp $line;
43 34 100       49 if ( my $match = _may_replace($line, $config) ) {
44 13 100       29 if ( ! _may_not_replace($line, $config) ) {
45 12         21 _replace_unixtime($match => \$line, $config);
46             }
47             }
48 34         764 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   55 my ($line, $config) = @_;
61              
62 39 100 100     787 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         71 return $1;
67             }
68             }
69              
70             sub _may_not_replace {
71 13     13   15 my ($line, $config) = @_;
72              
73 13 100       64 return unless $config->{'no-re'};
74              
75 1         2 for my $no_re (@{$config->{'no-re'}}) {
  1         3  
76 1 50       13 return 1 if $line =~ m!$no_re!;
77             }
78             }
79              
80             sub _replace_unixtime {
81 17     17   23 my ($maybe_unixtime, $line_ref, $config) = @_;
82              
83 17 100       46 if ($maybe_unixtime > 2**31-1) {
84 1         2 return;
85             }
86              
87 16 100 66     75 if ($config->{'min-time'} && $maybe_unixtime < $config->{'min-time'}) {
88 1         2 return;
89             }
90              
91 15         829 my $date = strftime($config->{format}, localtime($maybe_unixtime));
92 15 100       89 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         123 $$line_ref =~ s/$maybe_unixtime/$replaced_unixtime/;
101             }
102              
103             sub from_unixtime {
104 4     4 1 895 my ($lines, @argv) = @_;
105              
106 4         7 my $config = +{};
107 4         8 _get_options($config, \@argv);
108              
109 4         5 my @replaced_lines;
110 4         9 for my $line ( split /\n/, $lines ) {
111 5 50       9 if ( my $match = _may_replace($line, $config) ) {
112 5         12 _replace_unixtime($match => \$line, $config);
113             }
114 5         15 push @replaced_lines, $line;
115             }
116 4         34 return join("\n", @replaced_lines);
117             }
118              
119             sub _get_options {
120 20     20   21 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       179 ) or _show_usage(2);
139              
140 20         9897 _validate_options($config, $argv);
141             }
142              
143             sub _validate_options {
144 20     20   28 my ($config, $argv) = @_;
145              
146 20   33     75 $config->{format} ||= RC->{format} || $DEFAULT_DATE_FORMAT;
      66        
147 20   50     156 $config->{'start-bracket'} ||= RC->{'start-bracket'} || '(';
      66        
148 20   50     138 $config->{'end-bracket'} ||= RC->{'end-bracket'} || ')';
      66        
149 20 100       106 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         2  
154             }
155 20 100       140 if ($config->{re}) {
156 3         4 $config->{_re} = join '|', map { quotemeta $_; } @{$config->{re}};
  4         14  
  3         4  
157             }
158 20         22 push @{$config->{unixtime}}, @{$argv};
  20         43  
  20         35  
159             }
160              
161             sub _show_usage {
162 1     1   8 my $exitval = shift;
163              
164 1         603 require Pod::Usage;
165 1         44665 Pod::Usage::pod2usage(-exitval => $exitval);
166             }
167              
168             1;
169              
170             __END__