bin/date

filter: Statementstmt 63.6% 63 / 99 Branchbran 26.1% 11 / 42 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 73.3% 11 / 15 Podpod n/a totaltotal 50.8% 89 / 175 CC 24 SCAR 25.2
CC
24
Cov
51%
windows_time_zones
30.0
munge_tz
20.7
usage
20.0
CRAP 12.4
« bin/cat ↑ Summary bin/cut »
1 #!/usr/bin/perl
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use subs qw(core_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =begin metadata
6  
7 Name: date
8 Description: display or set date and time
9 Author: brian d foy, brian.d.foy@gmail.com
10 Author: Joshua Gross
11 License: artistic2
12  
13 =end metadata
14  
15 =cut
16  
17 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 my $VERSION = '1.0.5';
20  
21 # set this if we do anything to select a timezone abbreviation, then
22 # prefer this if it is defined.
23 1 my $TZ;
24  
25 # run if called directly, indirectly, directly par-packed, undirectly par-packed
26 1 ▶run(\@ARGV) if !caller() || caller(0) =~ /^(PerlPowerTools::Packed|PAR)$/ || caller(1) eq 'PAR';
Branch: 1 branch
BranchTrueFalseTotal
if not caller() or caller(0) =~ /^(PerlPowerTools::Packed|PAR)$/ or caller 1 eq "PAR" 1 0 1
Condition33%2/6
not caller() or caller(0) =~ /^(PerlPowerTools::Packed|PAR)$/ or caller 1 eq "PAR"
l!l&&r!l&&!r
100
not caller() or caller(0) =~ /^(PerlPowerTools::Packed|PAR)$/
l!l&&r!l&&!r
100
MC/DC0%0/3
not caller() or caller(0) =~ /^(PerlPowerTools::Packed|PAR)$/ or caller 1 eq "PAR"
not caller() caller(0) =~ /^(PerlPowerTools::Packed|PAR)$/ caller 1 eq "PAR"
Truth table25%1/4
not(A) or B or C
A= not caller()B= caller(0) =~ /^(PerlPowerTools::Packed|PAR)$/C= caller 1 eq "PAR"
ABCresult
1XX1
01X1
0011
0000
27  
28 2 ▶sub get_formats {
Subroutine get_formats: called CC=1 SCAR=0.0
29 {
30 2 "--rfc-3339" => '%Y-%m-%d %H:%M:%S%z', # iso-8601 without T
31 "--rfc-5322" => '%a, %d %b %Y %H:%M:%S %z',
32 "-I" => '%Y-%m-%dT%H:%M:%S%:z', # iso-8601
33 "-R" => '%a, %d %b %Y %T %z', # rfc-2822
34 "default" => '%a %b %e %T %Z %Y',
35 };
36 }
37  
38 1 ▶sub munge_tz {
Subroutine munge_tz: called CC=5 SCAR=30.3
39 1 ▶ return $TZ if defined $TZ; # something else we did set this, so use it
Branch: 1 branch
BranchTrueFalseTotal
if defined $TZ 1 0 1
40 0 my $from_posix = posix_tz();
41 0 ▶ return $from_posix if $from_posix =~ m/\A[A-Z]{3,4}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $from_posix =~ /\A[A-Z]{3,4}\z/ 0 0 0
42 0 my $from_windows = windows_time_zones();
43 0 ▶ my $i_dst = (core_time())[-1] ? -1 : 0; # some only have one entry, so two would be the same
Branch: 1 branch
BranchTrueFalseTotal
(core_time())[-1] ? : 0 0 0
44  
45 0 ▶ return $from_windows->{$from_posix}[$i_dst] if exists $from_windows->{$from_posix};
Branch: 1 branch
BranchTrueFalseTotal
if exists $from_windows->{$from_posix} 0 0 0
46 }
47  
48 0 ▶sub posix_tz { POSIX::strftime( '%Z', core_time() ) }
Subroutine posix_tz: not called CC=1 SCAR=6.9
49  
50 1 ▶sub quarter { int((core_time())[4] / 3) + 1 }
Subroutine quarter: called CC=1 SCAR=0.0
51  
52 1 ▶sub run {
Subroutine run: called CC=9 SCAR=24.7
53 1 my $args = shift;
54 1 *core_time = do {
55 1 my @times = CORE::localtime;
56 0 ▶ sub { @times }
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 1 };
58  
59 0 ▶ if( grep { $_ eq '-h' } @$args ) { usage(0) }
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$_ eq '-h';} @$args) { } 0 1 1
elsif (grep {$_ eq '-v';} @$args) { } 0 1 1
60 0 elsif( grep { $_ eq '-v' } @$args ) { print "$0 $VERSION\n"; exit 0; }
61  
62 1 my $formats = eval { get_formats() };
63 1 my $at = $@;
64 1 ▶ if( length $at ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $at) 0 1 1
65 0 print $at;
66 0 exit 1;
67 }
68  
69 1 my %allowed = map { ("-$_" => 1) } split( //, 'u' );
70 1 @allowed{ keys %$formats } = (1) x (keys %$formats);
71  
72 1 foreach (@$args) {
73 1 ▶ if( /^-/ and ! exists $allowed{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^-/ and not exists $allowed{$_}) 0 1 1
Condition33%1/3
/^-/ and not exists $allowed{$_}
!ll&&!rl&&r
010
MC/DC0%0/2
/^-/ and not exists $allowed{$_}
/^-/ not exists $allowed{$_}
Truth table33%1/3
A and not(B)
A= /^-/B= not exists $allowed{$_}
ABresult
0X0
100
111
74 0 print STDERR "Unrecognized option $_\n";
75 0 usage(2);
76 }
77  
78 1 ▶ if( /^\-u/ ) { $TZ = $ENV{'TZ'} = 'UTC'; my @times = CORE::gmtime; *core_time = sub { @times } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (/^\-u/) 1 0 1
79 }
80  
81 1 my $format = select_format(@$args);
82 1 my $specifiers = setup_specifiers();
83  
84 1 ▶ $format =~ s/%(:?.)/ exists $specifiers->{$1} ? $specifiers->{$1} : "%$1" /eg;
Branch: 1 branch
BranchTrueFalseTotal
exists $specifiers->{$1} ? : 6 0 6
85 1 print "$format\n";
86 }
87  
88 1 ▶sub select_format {
Subroutine select_format: called CC=4 SCAR=14.6
89 1 my @format_args = grep { /\A\+/ } @_;
90 1 ▶ die "Extra operands: " . join( ' ', @format_args[1..$#format_args] ) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if @format_args > 1 0 1 1
91 if @format_args > 1;
92  
93 1 ▶ return $1 if $format_args[0] =~ /\A\+(.+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $format_args[0] =~ /\A\+(.+)/ 0 1 1
94  
95 1 my $formats = get_formats();
96  
97 (
98 1 ▶ map { $formats->{$_} || () }
Condition33%1/3
$formats->{$_} || ()
l!l&&r!l&&!r
100
MC/DC0%0/2
$formats->{$_} || ()
$formats->{$_} ()
Truth table33%1/3
A || B
A= $formats->{$_}B= ()
ABresult
1X1
011
000
99 1 grep { exists $formats->{$_} }
100 ( @ARGV, 'default' )
101 )[0]
102 }
103  
104 1 ▶sub setup_specifiers {
Subroutine setup_specifiers: called CC=1 SCAR=0.0
105 my %specifiers = (
106 'e' => sprintf( '%2d', (core_time)[3] ),
107 'P' => lc(POSIX::strftime('%p', core_time())),
108 'q' => quarter(),
109 'T' => sprintf( '%02d:%02d:%02d', (core_time)[2,1,0] ),
110 'z' => tz_offset(),
111 1 ':z' => do { ( my $z = tz_offset() ) =~ s/(\d\d)/$1:/; $z },
112 'Z' => munge_tz(),
113 );
114  
115 # We cheat by letting POSIX figure these out because it can handle the
116 # locale. If we later find out that some system doesn't handle one of these,
117 # we'll define our own. But, only add a POSIX cheat if we haven't already
118 # defined that format.
119 1 my @POSIX = qw(
120 a A b B c C d D F g G h H I j k l m M
121 n p r R s S t u U V w W x X y Y
122 );
123 @specifiers{ @POSIX } =
124 35 map { POSIX::strftime( "%$_", core_time() ) }
125 1 grep { ! exists $specifiers{$_} }
126 @POSIX;
127  
128 1 \%specifiers;
129 }
130  
131 2 ▶sub tz_offset {
Subroutine tz_offset: called CC=2 SCAR=7.0
132 # https://stackoverflow.com/a/6428732/2766176
133 2 my @l = CORE::localtime;
134 2 my @g = CORE::gmtime;
135  
136 2 my $minutes = (
137 $l[2] - $g[2] +
138 ( # is this the same year or day? No? Add or subtract 24 hours
139 (($l[5]<<9)|$l[7]) <=> (($g[5]<<9)|$g[7])) * 24
140 ) * 60
141 + $l[1] - $g[1];
142  
143 2 ▶ my $sign = $minutes < 0 ? '-' : "+";
Branch: 1 branch
BranchTrueFalseTotal
$minutes < 0 ? : 0 2 2
144 2 $minutes = abs($minutes);
145  
146 2 sprintf "%s%02d%02d", $sign, int($minutes / 60), $minutes % 60;
147 }
148  
149 0 ▶sub usage {
Subroutine usage: not called CC=4 SCAR=30.0
150 0 ▶ my $exit_code = defined $_[0] ? $_[0] : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
151  
152 0 my $output = <<"HEADER";
153 usage: $0 [-hIRuv] [+format]
154  
155 Formats:
156 HEADER
157  
158 0 open my $fh, '<:encoding(UTF-8)', __FILE__;
159 0 ▶ while( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
160 0 ▶ next unless s/\A=item \s+ \* \s+(?=%)//x;
Branch: 1 branch
BranchTrueFalseTotal
unless s/\A=item \s+ \* \s+(?=%)//x 0 0 0
161 0 $output .= "\t$_";
162 }
163  
164 0 print "$output\n";
165 0 exit( $exit_code );
166 }
167  
168 0 ▶sub windows_time_zones {
Subroutine windows_time_zones: not called CC=5 SCAR=34.0
169 0 my %hash;
170  
171 0 open my $fh, '<:encoding(UTF-8)', __FILE__;
172 0 ▶ while( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
173 0 ▶ next unless /\A__(?:END|DATA)__/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\A__(?:END|DATA)__/ 0 0 0
174 0 last;
175 }
176  
177 0 ▶ while( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
178 0 chomp;
179 0 s/\s*#.*//;
180 0 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 0 0 0
181 0 my( $windows_name, $tz, @names ) = split /\s*,\s*/;
182 0 @names = sort @names; # capital letters sort before + - digits!
183 0 $hash{$windows_name} = \@names;
184 }
185  
186 0 close $fh;
187  
188 0 return \%hash;
189 }
190  
191 =encoding utf8
192  
193 =head1 NAME
194  
195 date - display date and time
196  
197 =head1 SYNOPSIS
198  
199 # show the local date in the default format
200 % date
201  
202 # show the UTC date in the default format
203 % date -u
204  
205 # display version, help
206 % date -v
207 % date -h
208  
209 # show the local date in the specified format
210 % date +FORMAT
211  
212 # show the GMT date in the specified format
213 % date -u +FORMAT
214  
215 # show local date in ISO 8601 format
216 % date -I
217  
218 # show local date in RFC 2822 format
219 % date -R
220  
221  
222 =head1 DESCRIPTION
223  
224 =head2 Options
225  
226 =over 4
227  
228 =item * +FORMAT
229  
230 Specify the date format
231  
232 =item * -h
233  
234 Show the help message and exit
235  
236 =item * -I
237  
238 Use the ISO 8601 date format: C<%Y-%m-%dT%H:%M:%S%:z>
239  
240 =item * -R
241  
242 Use RFC 2822 format: C<%a, %d %b %Y %T %z>
243  
244 =item * -u
245  
246 Use UTC time instead of local time
247  
248 =item * -v
249  
250 Show the version and exit
251  
252 =back
253  
254 =head2 Formats
255  
256 =over 4
257  
258 =item * %% - The character %
259  
260 =item * %a - Three-letter weekday name
261  
262 =item * %A - Full weekday name
263  
264 =item * %b - Three-letter month name
265  
266 =item * %B - Full month name
267  
268 =item * %c - locale version of the date-time string
269  
270 =item * %C - Century (00-99)
271  
272 =item * %d - Day of month (padded w/ zero)
273  
274 =item * %D - Date in MM/DD/YY format
275  
276 =item * %e - Day of month (padded w/ space)
277  
278 =item * %F - %Y-%m-%d
279  
280 =item * %g - ISO 8601 year
281  
282 =item * %G - ISO 8601 year
283  
284 =item * %h - Three-letter month name
285  
286 =item * %H - Hour HH
287  
288 =item * %I - Hour HH (12 hour)
289  
290 =item * %j - Three-digit Julian day
291  
292 =item * %k - Hour - space padded
293  
294 =item * %l - Hour - space padded (12 hour)
295  
296 =item * %m - Month number 01-12
297  
298 =item * %M - Minute MM
299  
300 =item * %n - Newline
301  
302 =item * %p - AM or PM
303  
304 =item * %P - like %p, but lowercase
305  
306 =item * %q - quarter of the year (1-4)
307  
308 =item * %r - Time in HH(12 hour):MM:SS (AM|PM) format
309  
310 =item * %R - Time in HH:MM format
311  
312 =item * %s - Absolute seconds (since epoch)
313  
314 =item * %S - Seconds SS
315  
316 =item * %t - Tab
317  
318 =item * %T - Time in HH:MM:SS format.
319  
320 =item * %u - Day of week, 1=Monday, 7=Sunday.
321  
322 =item * %U - Two digit week number, starting on Sunday.
323  
324 =item * %V - ISO week number, with Monday as the first day of week
325  
326 =item * %w - Day of week, 0=Sunday, 6=Saturday.
327  
328 =item * %W - Two digit week number, start Monday.
329  
330 =item * %x - locale's date representation
331  
332 =item * %X - locale's time representation
333  
334 =item * %y - Two-digit year.
335  
336 =item * %Y - Four-digit year.
337  
338 =item * %z - Time zone offset in [+-]HHMM.
339  
340 =item * %:z - Time zone offset in [+-]HH:MM.
341  
342 =item * %Z - Time zone abbrevation, such as UTC or EST.
343  
344 Note: Windows does not use the POSIX time stuff, so we try to fake it.
345 If you don't get what you expect for the time zone abbreviation
346 please open an issue: L<https://github.com/briandfoy/PerlPowerTools/issues>.
347 If you look in the C<date> file, you'll see a simple text list at the end
348 that we use for Windows.
349  
350 =back
351  
352 =cut
353  
354 # If any of these are wrong, just send the patch or raise an issue
355  
356 =begin comment
357  
358 Windows is a bit of a pain here. We want to support %Z, but Windows
359 doesn't track the time zone abbreviations. You get the full name,
360 such as "Alaskan Standard Time". So, I want a way to convert that.
361  
362 These data are ad hoc, and guessing sometimes.
363  
364 First, I got a list of all the Windows timezone names with `tzutil /l`.
365 However, those are just the names you can use with `/s` rather than
366 all of the names windows will report. For example, you can set
367 "Alaskan Standard Time" but not "Alaskan Daylight Time", although
368 Windows, through the POSIX module, will report "Alaskan Daylight Time"
369 when appropriate.
370  
371 I get all those names by setting every Windows time zone, then checking
372 what POSIX's %Z returns. Note that you need to do this in a separate
373 process because most of the time tools only respect the first setting
374 of the time zone.
375  
376 Second, I want to get time zone abbreviations. This is a bit tricky because
377 the same offset can have many different abbreviations. By hand, I
378 associated the Olson name, such as America/Anchorage, with the Windows
379 name. Once I have all of those, I can use DateTime::TimeZone to get the
380 Olson abbreviations. In some cases, I added additional time zone names.
381  
382 Note that some of the abbreviations may be unexpected or unwanted. For
383 example, in the Russian Time Zones, they have special names, but you
384 may want MSK+n. You can edit this file to put whatever you like.
385  
386 =end comment
387  
388 __DATA__
« bin/cat ↑ Summary bin/cut »