File Coverage

blib/lib/POSIX/strftime/GNU.pm
Criterion Covered Total %
statement 24 24 100.0
branch 1 2 50.0
condition 2 3 66.6
subroutine 8 8 100.0
pod n/a
total 35 37 94.5


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -c
2              
3             package POSIX::strftime::GNU;
4              
5             =head1 NAME
6              
7             POSIX::strftime::GNU - strftime with GNU extensions
8              
9             =head1 SYNOPSIS
10              
11             use POSIX::strftime::GNU;
12             use POSIX 'strftime';
13             print POSIX::strftime('%a, %d %b %Y %T %z', localtime);
14              
15             command line:
16              
17             C:\> set PERL_ANYEVENT_LOG=filter=debug
18             C:\> perl -MPOSIX::strftime::GNU -MAnyEvent -e "AE::cv->send"
19              
20             =head1 DESCRIPTION
21              
22             This is a wrapper for L which implements more
23             character sequences compatible with GNU systems.
24              
25             The module is 100% compatible with format of date(1) command from GNU
26             coreutils package.
27              
28             It can be helpful if you run some software on operating system where these
29             extensions, especially C<%z> sequence, are not supported, i.e. on Microsoft
30             Windows. On such system some software can work incorrectly, i.e. logging for
31             L and L modules might be broken.
32              
33             Even GNU C Library's strftime(3) function does not provide 100% compatibility
34             with date(1) command so this module can be useful also on Linux.
35              
36             The XS module is used if compiler is available and can module can be loaded.
37             The XS is mandatory if C environment variable is
38             true.
39              
40             The PP module is used when XS module can not be loaded or
41             C environment variable is true.
42              
43             None of these modules are loaded if both C and
44             C environment variables are defined and false.
45              
46             =for readme stop
47              
48             =cut
49              
50              
51 8     8   294805 use 5.006;
  8         31  
  8         314  
52 8     8   60 use strict;
  8         27  
  8         335  
53 8     8   46 use warnings;
  8         15  
  8         443  
54              
55             our $VERSION = '0.0304';
56              
57 8     8   39 use Carp ();
  8         15  
  8         128  
58 8     8   7482 use POSIX ();
  8         61115  
  8         790  
59              
60             =head1 FUNCTIONS
61              
62             =head2 strftime
63              
64             $str = strftime($format, @time)
65              
66             This is replacement for L function.
67              
68             The nanoseconds can be given as a fraction of seconds.
69              
70             use POSIX::strftime::GNU;
71             use Time::HiRes qw(gettimeofday);
72             my ($t, $nsec) = gettimeofday;
73             my @t = localtime $t;
74             $t[0] += $nsec / 10e5;
75             print strftime('%N', @t);
76              
77             =cut
78              
79             my ($xs_loaded, $pp_loaded);
80              
81             my $xs_env = $ENV{PERL_POSIX_STRFTIME_GNU_XS};
82             my $pp_env = $ENV{PERL_POSIX_STRFTIME_GNU_PP};
83              
84             if (
85             (not defined $xs_env or defined $xs_env and $xs_env )
86             and
87             (not defined $pp_env or defined $pp_env and not $pp_env )
88             ) {
89             $xs_loaded = eval {
90             require POSIX::strftime::GNU::XS;
91 8     8   103 no warnings 'once';
  8         16  
  8         863  
92             *strftime = *POSIX::strftime::GNU::XS::strftime;
93             1;
94             };
95             die $@ if $@ and $ENV{PERL_POSIX_STRFTIME_GNU_XS};
96             };
97              
98             if (not $xs_loaded and (not defined $pp_env or defined $pp_env and $pp_env)) {
99             require POSIX::strftime::GNU::PP;
100 8     8   44 no warnings 'once';
  8         15  
  8         1334  
101             *strftime = *POSIX::strftime::GNU::PP::strftime;
102             $pp_loaded = 1;
103             };
104              
105             sub import {
106 8     8   105 my ($class) = @_;
107 8 50 66     82 *POSIX::strftime = *strftime if $xs_loaded or $pp_loaded;
108 8         116 return 1;
109             };
110              
111             1;
112              
113              
114             =head1 FORMAT
115              
116             The format argument is composed of zero or more conversion specifications.
117             Each conversion specification is composed of a C<%> (percent) character
118             followed by one or two conversion characters which specify the replacement
119             required.
120              
121             There are some extensions of ANSI C (unmarked): those given in the Single UNIX
122             Specification (marked SU), those given in Olson's timezone package (marked
123             TZ), and those given in glibc (marked GNU).
124              
125             The following conversion specifications are supported:
126              
127             =over
128              
129             =item C<%a>
130              
131             The abbreviated weekday name according to the current locale.
132              
133             =item C<%A>
134              
135             The full weekday name according to the current locale.
136              
137             =item C<%b>
138              
139             The abbreviated month name according to the current locale.
140              
141             =item C<%B>
142              
143             The full month name according to the current locale.
144              
145             =item C<%c>
146              
147             The preferred date and time representation for the current locale.
148              
149             =item C<%C>
150              
151             The century number (year/100) as a 2-digit integer. (SU)
152              
153             =item C<%d>
154              
155             The day of the month as a decimal number (range 01 to 31).
156              
157             =item C<%D>
158              
159             Equivalent to C<%m/%d/%y>. (for Americans only: Americans should note that in
160             other countries C<%d/%m/%y> is rather common. This means that in international
161             context this format is ambiguous and should not be used.) (SU)
162              
163             =item C<%e>
164              
165             Like C<%d>, the day of the month as a decimal number, but a leading zero is
166             replaced by a space. (SU)
167              
168             =item C<%E>
169              
170             Modifier: use alternative format, see below. (SU)
171              
172             =item C<%F>
173              
174             Equivalent to C<%Y-%m-%d> (the ISO 8601 date format). (C99)
175              
176             =item C<%G>
177              
178             The ISO 8601 week-based year (see NOTES) with century as a decimal number. The
179             4-digit year corresponding to the ISO week number (see C<%V>). This has the
180             same format and value as %Y, except that if the ISO week number belongs to the
181             previous or next year, that year is used instead. (TZ)
182              
183             =item C<%g>
184              
185             Like C<%G>, but without century, that is, with a 2-digit year (00-99). (TZ)
186              
187             =item C<%h>
188              
189             Equivalent to C<%b>. (SU)
190              
191             =item C<%H>
192              
193             The hour as a decimal number using a 24-hour clock (range 00 to 23).
194              
195             =item C<%I>
196              
197             The hour as a decimal number using a 12-hour clock (range 01 to 12).
198              
199             =item C<%j>
200              
201             The day of the year as a decimal number (range 001 to 366).
202              
203             =item C<%k>
204              
205             The hour (24-hour clock) as a decimal number (range 0 to 23); single digits
206             are preceded by a blank. (See also C<%H>.) (TZ)
207              
208             =item C<%l>
209              
210             The hour (12-hour clock) as a decimal number (range 1 to 12); single digits
211             are preceded by a blank. (See also C<%I>.) (TZ)
212              
213             =item C<%m>
214              
215             The month as a decimal number (range 01 to 12).
216              
217             =item C<%M>
218              
219             The minute as a decimal number (range 00 to 59).
220              
221             =item C<%n>
222              
223             A newline character. (SU)
224              
225             =item C<%N>
226              
227             Nanoseconds (range 000000000 to 999999999). It is a non-POSIX extension and
228             outputs a nanoseconds if there is floating seconds argument.
229              
230             =item C<%O>
231              
232             Modifier: use alternative format, see below. (SU)
233              
234             =item C<%p>
235              
236             Either "AM" or "PM" according to the given time value, or the corresponding
237             strings for the current locale. Noon is treated as "PM" and midnight as "AM".
238              
239             =item C<%P>
240              
241             Like C<%p> but in lowercase: "am" or "pm" or a corresponding string for the
242             current locale. (GNU)
243              
244             =item C<%r>
245              
246             The time in a.m. or p.m. notation. In the POSIX locale this is equivalent to
247             C<%I:%M:%S %p>. (SU)
248              
249             =item C<%R>
250              
251             The time in 24-hour notation (%H:%M). (SU) For a version including the
252             seconds, see C<%T> below.
253              
254             =item C<%s>
255              
256             The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
257              
258             =item C<%S>
259              
260             The second as a decimal number (range 00 to 60). (The range is up to 60 to
261             allow for occasional leap seconds.)
262              
263             =item C<%t>
264              
265             A tab character. (SU)
266              
267             =item C<%T>
268              
269             The time in 24-hour notation (C<%H:%M:%S>). (SU)
270              
271             =item C<%u>
272              
273             The day of the week as a decimal, range 1 to 7, Monday being 1. See also
274             C<%w>. (SU)
275              
276             =item C<%U>
277              
278             The week number of the current year as a decimal number, range 00 to 53,
279             starting with the first Sunday as the first day of week 01. See also C<%V> and
280             C<%W>.
281              
282             =item C<%V>
283              
284             The ISO 8601 week number (see NOTES) of the current year as a decimal number,
285             range 01 to 53, where week 1 is the first week that has at least 4 days in the
286             new year. See also C<%U> and C<%W>. (SU)
287              
288             =item C<%w>
289              
290             The day of the week as a decimal, range 0 to 6, Sunday being 0. See also
291             C<%u>.
292              
293             =item C<%W>
294              
295             The week number of the current year as a decimal number, range 00 to 53,
296             starting with the first Monday as the first day of week 01.
297              
298             =item C<%x>
299              
300             The preferred date representation for the current locale without the time.
301              
302             =item C<%X>
303              
304             The preferred time representation for the current locale without the date.
305              
306             =item C<%y>
307              
308             The year as a decimal number without a century (range 00 to 99).
309              
310             =item C<%Y>
311              
312             The year as a decimal number including the century.
313              
314             =item C<%z>
315              
316             The C<+hhmm> or C<-hhmm> numeric timezone (that is, the hour and minute offset
317             from UTC). (SU)
318              
319             =item C<%Z>
320              
321             The timezone or name or abbreviation.
322              
323             =item C<%+>
324              
325             The date and time in date(1) format. (TZ) (Not supported in glibc2.)
326              
327             =item C<%%>
328              
329             A literal C<%> character.
330              
331             =back
332              
333             Some conversion specifications can be modified by preceding the conversion
334             specifier character by the C or C modifier to indicate that an
335             alternative format should be used. If the alternative format or specification
336             does not exist for the current locale, the behavior will be as if the
337             unmodified conversion specification were used. (SU) The Single UNIX
338             Specification mentions C<%Ec>, C<%EC>, C<%Ex>, C<%EX>, C<%Ey>, C<%EY>, C<%Od>,
339             C<%Oe>, C<%OH>, C<%OI>, C<%Om>, C<%OM>, C<%OS>, C<%Ou>, C<%OU>, C<%OV>,
340             C<%Ow>, C<%OW>, C<%Oy>, where the effect of the C modifier is to use
341             alternative numeric symbols (say, roman numerals), and that of the C
342             modifier is to use a locale-dependent alternative representation.
343              
344             C<%G>, C<%g>, and C<%V> yield values calculated from the week-based year
345             defined by the ISO 8601 standard. In this system, weeks start on a Monday, and
346             are numbered from 01, for the first week, up to 52 or 53, for the last week.
347             Week 1 is the first week where four or more days fall within the new year (or,
348             synonymously, week 01 is: the first week of the year that contains a Thursday;
349             or, the week that has 4 January in it). When three of fewer days of the first
350             calendar week of the new year fall within that year, then the ISO 8601
351             week-based system counts those days as part of week 53 of the preceding year.
352             For example, 1 January 2010 is a Friday, meaning that just three days of that
353             calendar week fall in 2010. Thus, the ISO 8601 week- based system considers
354             these days to be part of week 53 (C<%V>) of the year 2009 (C<%G>) ; week 01 of
355             ISO 8601 year 2010 starts on Monday, 4 January 2010.
356              
357             Glibc provides some extensions for conversion specifications. (These
358             extensions are not specified in POSIX.1-2001, but a few other systems provide
359             similar features.) Between the C<%> character and the conversion specifier
360             character, an optional flag and field width may be specified. (These precede
361             the C or C modifiers, if present.)
362              
363             The following flag characters are permitted:
364              
365             =over
366              
367             =item C<_>
368              
369             (underscore) Pad a numeric result string with spaces.
370              
371             =item C<->
372              
373             (dash) Do not pad a numeric result string.
374              
375             =item C<0>
376              
377             Pad a numeric result string with zeros even if the conversion specifier
378             character uses space-padding by default.
379              
380             =item C<^>
381              
382             Convert alphabetic characters in result string to upper case.
383              
384             =item C<#>
385              
386             Swap the case of the result string. (This flag only works with certain
387             conversion specifier characters, and of these, it is only really useful with
388             C<%Z>.)
389              
390             =back
391              
392             =for readme continue
393              
394             =head1 INSTALLING
395              
396             =head2 Cygwin
397              
398             This module requires C package.
399              
400             =head1 BUGS
401              
402             Timezone name is guessed with several heuristics so it can differ from
403             timezone name returned by date(1) command.
404              
405             If you find the bug or want to implement new features, please report it at
406             L
407              
408             The code repository is available at
409             L
410              
411             =head1 AUTHOR
412              
413             Piotr Roszatycki
414              
415             =head1 LICENSE
416              
417             Copyright (c) 2012-2014 Piotr Roszatycki .
418              
419             Format specification is based on strftime(3) manual page which is a part of
420             the Linux man-pages project.
421              
422             This is free software; you can redistribute it and/or modify it under
423             the same terms as perl itself.
424              
425             See L