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