line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::FromUnixtime; |
2
|
6
|
|
|
6
|
|
195153
|
use strict; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
191
|
|
3
|
6
|
|
|
6
|
|
25
|
use warnings; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
196
|
|
4
|
6
|
|
|
6
|
|
4256
|
use Getopt::Long qw/GetOptionsFromArray/; |
|
6
|
|
|
|
|
58700
|
|
|
6
|
|
|
|
|
31
|
|
5
|
6
|
|
|
6
|
|
4081
|
use IO::Interactive::Tiny; |
|
6
|
|
|
|
|
51
|
|
|
6
|
|
|
|
|
189
|
|
6
|
6
|
|
|
6
|
|
2703
|
use POSIX qw/strftime/; |
|
6
|
|
|
|
|
31243
|
|
|
6
|
|
|
|
|
26
|
|
7
|
6
|
|
|
6
|
|
7610
|
use Config::CmdRC qw/.from_unixtimerc/; |
|
6
|
|
|
|
|
77014
|
|
|
6
|
|
|
|
|
36
|
|
8
|
6
|
|
|
6
|
|
728
|
use Exporter 'import'; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
5086
|
|
9
|
|
|
|
|
|
|
our @EXPORT = qw/from_unixtime/; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.17'; |
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
|
30171
|
my $self = shift; |
29
|
16
|
|
|
|
|
30
|
my @argv = @_; |
30
|
|
|
|
|
|
|
|
31
|
16
|
|
|
|
|
24
|
my $config = +{}; |
32
|
16
|
|
|
|
|
40
|
_get_options($config, \@argv); |
33
|
|
|
|
|
|
|
|
34
|
16
|
|
|
|
|
32
|
_main($config); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub _main { |
38
|
16
|
|
|
16
|
|
17
|
my $config = shift; |
39
|
|
|
|
|
|
|
|
40
|
16
|
50
|
|
|
|
46
|
if ( ! IO::Interactive::Tiny::is_interactive(*STDIN) ) { |
41
|
16
|
|
|
|
|
228
|
while ( my $line = ) { |
42
|
34
|
|
|
|
|
37
|
chomp $line; |
43
|
34
|
100
|
|
|
|
50
|
if ( my $match = _may_replace($line, $config) ) { |
44
|
13
|
100
|
|
|
|
32
|
if ( ! _may_not_replace($line, $config) ) { |
45
|
12
|
|
|
|
|
25
|
_replace_unixtime($match => \$line, $config); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
34
|
|
|
|
|
730
|
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
|
|
54
|
my ($line, $config) = @_; |
61
|
|
|
|
|
|
|
|
62
|
39
|
100
|
100
|
|
|
828
|
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
|
|
|
|
|
73
|
return $1; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub _may_not_replace { |
71
|
13
|
|
|
13
|
|
17
|
my ($line, $config) = @_; |
72
|
|
|
|
|
|
|
|
73
|
13
|
100
|
|
|
|
37
|
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
|
|
|
|
45
|
if ($maybe_unixtime > 2**31-1) { |
84
|
1
|
|
|
|
|
3
|
return; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
16
|
100
|
66
|
|
|
73
|
if ($config->{'min-time'} && $maybe_unixtime < $config->{'min-time'}) { |
88
|
1
|
|
|
|
|
2
|
return; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
15
|
|
|
|
|
809
|
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
|
|
|
|
|
127
|
$$line_ref =~ s/$maybe_unixtime/$replaced_unixtime/; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub from_unixtime { |
104
|
4
|
|
|
4
|
1
|
923
|
my ($lines, @argv) = @_; |
105
|
|
|
|
|
|
|
|
106
|
4
|
|
|
|
|
6
|
my $config = +{}; |
107
|
4
|
|
|
|
|
9
|
_get_options($config, \@argv); |
108
|
|
|
|
|
|
|
|
109
|
4
|
|
|
|
|
4
|
my @replaced_lines; |
110
|
4
|
|
|
|
|
11
|
for my $line ( split /\n/, $lines ) { |
111
|
5
|
50
|
|
|
|
8
|
if ( my $match = _may_replace($line, $config) ) { |
112
|
5
|
|
|
|
|
11
|
_replace_unixtime($match => \$line, $config); |
113
|
|
|
|
|
|
|
} |
114
|
5
|
|
|
|
|
14
|
push @replaced_lines, $line; |
115
|
|
|
|
|
|
|
} |
116
|
4
|
|
|
|
|
31
|
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
|
|
|
|
196
|
) or _show_usage(2); |
139
|
|
|
|
|
|
|
|
140
|
20
|
|
|
|
|
9702
|
_validate_options($config, $argv); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _validate_options { |
144
|
20
|
|
|
20
|
|
27
|
my ($config, $argv) = @_; |
145
|
|
|
|
|
|
|
|
146
|
20
|
|
33
|
|
|
73
|
$config->{format} ||= RC->{format} || $DEFAULT_DATE_FORMAT; |
|
|
|
66
|
|
|
|
|
147
|
20
|
|
50
|
|
|
152
|
$config->{'start-bracket'} ||= RC->{'start-bracket'} || '('; |
|
|
|
66
|
|
|
|
|
148
|
20
|
|
50
|
|
|
121
|
$config->{'end-bracket'} ||= RC->{'end-bracket'} || ')'; |
|
|
|
66
|
|
|
|
|
149
|
20
|
100
|
|
|
|
97
|
if (ref RC->{re} eq 'ARRAY') { |
|
|
100
|
|
|
|
|
|
150
|
1
|
|
|
|
|
4
|
push @{$config->{re}}, @{RC->{re}}; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
3
|
|
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
elsif (RC->{re}) { |
153
|
1
|
|
|
|
|
6
|
push @{$config->{re}}, RC->{re}; |
|
1
|
|
|
|
|
4
|
|
154
|
|
|
|
|
|
|
} |
155
|
20
|
100
|
|
|
|
128
|
if ($config->{re}) { |
156
|
3
|
|
|
|
|
4
|
$config->{_re} = join '|', map { quotemeta $_; } @{$config->{re}}; |
|
4
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
5
|
|
157
|
|
|
|
|
|
|
} |
158
|
20
|
|
|
|
|
22
|
push @{$config->{unixtime}}, @{$argv}; |
|
20
|
|
|
|
|
42
|
|
|
20
|
|
|
|
|
34
|
|
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub _show_usage { |
162
|
1
|
|
|
1
|
|
7
|
my $exitval = shift; |
163
|
|
|
|
|
|
|
|
164
|
1
|
|
|
|
|
608
|
require Pod::Usage; |
165
|
1
|
|
|
|
|
37566
|
Pod::Usage::pod2usage(-exitval => $exitval); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
1; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
__END__ |