File Coverage

blib/lib/App/perlmv/scriptlet/add_prefix_datestamp.pm
Criterion Covered Total %
statement 11 16 68.7
branch 0 2 0.0
condition 0 6 0.0
subroutine 4 5 80.0
pod n/a
total 15 29 51.7


line stmt bran cond sub pod time code
1             package App::perlmv::scriptlet::add_prefix_datestamp;
2              
3 1     1   291737 use 5.010001;
  1         5  
4 1     1   8 use strict;
  1         2  
  1         33  
5 1     1   7 use warnings;
  1         2  
  1         518  
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2023-08-25'; # DATE
9             our $DIST = 'App-perlmv-scriptlet-add_prefix_datestamp'; # DIST
10             our $VERSION = '0.003'; # VERSION
11              
12             sub main::_parse_date {
13 0     0     my $date = shift;
14 0 0         if ($date =~ /^\A(\d{4})-?(\d{2})-?(\d{2})(?:[T ]?(\d{2}):?(\d{2}):?(\d{2}))?\z/) {
15 0           require Time::Local;
16 0   0       return Time::Local::timelocal_posix($6 // 0, $5 // 0, $4 // 0, $3, $2-1, $1 - 1900);
      0        
      0        
17             } else {
18 0           die "Can't parse date '$date'";
19             }
20             }
21              
22             our $SCRIPTLET = {
23             summary => 'Add datestamp prefix (YYYYMMDD-) to filenames, using files\' modification time as date',
24             args => {
25             date => {
26             summary => "Use this date instead of file's modification time",
27             schema => 'date*',
28             },
29             avoid_duplicate_prefix => {
30             summary => 'Avoid adding prefix when filename already has prefix that looks like datestamp (1xxxxxxx- to 2xxxxxxx)',
31             schema => 'bool*',
32             },
33             prefix_regex => {
34             summary => 'Specify how existing datestamp prefix should be recognized',
35             schema => 're_from_str',
36             description => <<'_',
37              
38             This regex is used to check for the existence of datestamp (if you use the
39             `avoid_duplicate_prefix` option. The default is `qr/^\d{8}(?:T\d{6})?-/` but if
40             your existing datestamps are in different syntax you can accommodate them here.
41              
42             _
43             },
44             prefix_format => {
45             summary => 'Specify datestamp format, in the form of strftime() template',
46             schema => 'str*',
47             description => <<'_',
48              
49             The default format is `"%Y%m%d-"` or `"%Y%m%dT%H%M%S-"` if you enable the
50             `with_time` option. But you can customize it here.
51              
52             _
53             },
54             with_time => {
55             summary => 'Whether to add time (YYYYMMDD"T"hhmmss instead of just date (YYYYMMDD)',
56             schema => 'bool*',
57             },
58             },
59             code => sub {
60             package
61             App::perlmv::code;
62              
63             require POSIX;
64              
65 1     1   11 use vars qw($ARGS);
  1         2  
  1         344  
66              
67             my $re = $ARGS->{prefix_regex} // qr/\A[12][0-9]{3}(0[1-9]|10|11|12)([0-2][0-9]|30|31)-/;
68              
69             if ($ARGS->{avoid_duplicate_prefix} && $_ =~ $re) {
70             return $_;
71             }
72             my @stat = stat($_);
73             my $time = defined $ARGS->{date} ? main::_parse_date($ARGS->{date}) : $stat[9];
74             my $format = $ARGS->{prefix_format} // ($ARGS->{with_time} ? '%Y%m%dT%H%M%S-' : '%Y%m%d-');
75             my $prefix = POSIX::strftime($format, localtime($time));
76              
77             "$prefix$_";
78             },
79             };
80              
81             1;
82              
83             # ABSTRACT: Add datestamp prefix (YYYYMMDD-) to filenames, using files' modification time as date
84              
85             __END__