File Coverage

blib/lib/DateTimeX/strftimeq.pm
Criterion Covered Total %
statement 77 77 100.0
branch 5 8 62.5
condition 7 11 63.6
subroutine 20 20 100.0
pod 1 1 100.0
total 110 117 94.0


line stmt bran cond sub pod time code
1             ## no critic: Modules::ProhibitAutomaticExportation
2              
3             package DateTimeX::strftimeq;
4              
5             our $DATE = '2019-11-20'; # DATE
6             our $DIST = 'DateTimeX-strftimeq'; # DIST
7             our $VERSION = '0.004'; # VERSION
8              
9 1     1   588040 use 5.010001;
  1         12  
10 1     1   5 use strict;
  1         3  
  1         24  
11 1     1   5 use warnings;
  1         2  
  1         32  
12              
13 1     1   473 use Date::strftimeq ();
  1         1603  
  1         23  
14 1     1   6 use POSIX ();
  1         3  
  1         17  
15 1     1   5 use Scalar::Util 'blessed';
  1         2  
  1         46  
16              
17 1     1   6 use Exporter 'import';
  1         2  
  1         372  
18             our @EXPORT = qw(strftimeq);
19              
20             sub strftimeq {
21 12     12 1 3541 my ($format, @time) = @_;
22              
23 12         36 my ($caller_pkg) = caller();
24 12         22 my ($dt, %compiled_code);
25              
26 12 50 66     85 if (@time == 1 && blessed $time[0] && $time[0]->isa('DateTime')) {
      66        
27 6         12 $dt = $time[0];
28 6         27 @time = (
29             $dt->second,
30             $dt->minute,
31             $dt->hour,
32             $dt->day,
33             $dt->month-1,
34             $dt->year-1900,
35             );
36             }
37              
38 12         238 $format =~ s{$Date::strftimeq::regex}{
39             # for faster acccess
40 38         407 my %m = %+;
41              
42             #use DD; dd \%m; # DEBUG
43              
44 38 100       136 if (exists $m{code}) {
45 6         36 require DateTime;
46 6   66     35 $dt //= DateTime->new(
47             second => $time[0],
48             minute => $time[1],
49             hour => $time[2],
50             day => $time[3],
51             month => $time[4]+1,
52             year => $time[5]+1900,
53             );
54 6 50       901 unless (defined $compiled_code{$m{code}}) {
55             #say "D: compiling $m{code}"; # DEBUG
56 1     1   7 $compiled_code{$m{code}} = eval "package $caller_pkg; no strict; no warnings; sub { $m{code} }";
  1     1   3  
  1     1   27  
  1     1   4  
  1     1   3  
  1     1   54  
  1     1   6  
  1     1   3  
  1     1   23  
  1     1   5  
  1     1   2  
  1     1   35  
  1         7  
  1         3  
  1         22  
  1         5  
  1         1  
  1         49  
  1         6  
  1         2  
  1         22  
  1         5  
  1         2  
  1         46  
  1         6  
  1         2  
  1         23  
  1         4  
  1         2  
  1         46  
  1         6  
  1         2  
  1         21  
  1         5  
  1         4  
  1         47  
  6         403  
57 6 50       24 die "Can't compile code in $m{all}: $@" if $@;
58             }
59 6         10 local $_ = $dt;
60 6         158 my $code_res = $compiled_code{$m{code}}->(
61             time => \@time,
62             dt => $dt,
63             );
64 6   50     123 $code_res //= "";
65 6         13 $code_res =~ s/%/%%/g;
66 6         26 $code_res;
67             } else {
68 32         175 $m{all};
69             }
70             }xego;
71              
72 12         513 POSIX::strftime($format, @time);
73             }
74              
75             1;
76             # ABSTRACT: POSIX::strftime() with support for embedded perl code in %(...)q
77              
78             __END__
79              
80             =pod
81              
82             =encoding UTF-8
83              
84             =head1 NAME
85              
86             DateTimeX::strftimeq - POSIX::strftime() with support for embedded perl code in %(...)q
87              
88             =head1 VERSION
89              
90             This document describes version 0.004 of DateTimeX::strftimeq (from Perl distribution DateTimeX-strftimeq), released on 2019-11-20.
91              
92             =head1 SYNOPSIS
93              
94             use DateTimeX::strftimeq; # by default exports strftimeq()
95              
96             my @time = localtime();
97             print strftimeq '<%6Y-%m-%d>', @time; # <002019-11-19>
98             print strftimeq '<%6Y-%m-%d%( $_->day_of_week eq 7 ? "sun" : "" )q>', @time; # <002019-11-19>
99             print strftimeq '<%6Y-%m-%d%( $_->day_of_week eq 2 ? "tue" : "" )q>', @time; # <002019-11-19tue>
100              
101             You can also pass DateTime object instead of ($second, $minute, $hour, $day,
102             $month, $year):
103              
104             print strftimeq '<%6Y-%m-%d>', $dt; # <002019-11-19>
105              
106             =head1 DESCRIPTION
107              
108             This module provides C<strftimeq()> which extends L<POSIX>'s C<strftime()> with
109             a conversion: C<%(...)q>. Inside the parenthesis, you can specify Perl code.
110              
111             The Perl code will receive a hash argument (C<%args>) with the following keys:
112             C<time> (arrayref, the arguments passed to strftimeq() except for the first),
113             C<dt> (L<DateTime> object). For convenience, C<$_> will also be locally set to
114             the DateTime object. The Perl code will be eval-ed in the caller's package,
115             without L<strict> and without L<warnings>.
116              
117             =head1 FUNCTIONS
118              
119             =head2 strftimeq
120              
121             Usage:
122              
123             $str = strftimeq $fmt, $sec, $min, $hour, $mday, $mon, $year;
124             $str = strftimeq $fmt, $dt;
125              
126             =head1 HOMEPAGE
127              
128             Please visit the project's homepage at L<https://metacpan.org/release/DateTimeX-strftimeq>.
129              
130             =head1 SOURCE
131              
132             Source repository is at L<https://github.com/perlancar/perl-DateTimeX-strftimeq>.
133              
134             =head1 BUGS
135              
136             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=DateTimeX-strftimeq>
137              
138             When submitting a bug or request, please include a test-file or a
139             patch to an existing test-file that illustrates the bug or desired
140             feature.
141              
142             =head1 SEE ALSO
143              
144             L<POSIX>'s C<strftime()>
145              
146             L<DateTime>
147              
148             =head1 AUTHOR
149              
150             perlancar <perlancar@cpan.org>
151              
152             =head1 COPYRIGHT AND LICENSE
153              
154             This software is copyright (c) 2019 by perlancar@cpan.org.
155              
156             This is free software; you can redistribute it and/or modify it under
157             the same terms as the Perl 5 programming language system itself.
158              
159             =cut