Reputation: 3590
I have the code:
use POSIX qw( strftime );
print POSIX::strftime("%a, %d %b %G %T-0000",localtime(1325427034));
which should output
Sun, 01 Jan 2012 09:10:34-0000
but instead it outputs
Sun, 01 Jan 2011 09:10:34-0000
In a few tests I did, it seems to happen whenever the time is in the first few days of 2010, 2011, or 2012. It works correctly in every other case including the first few days of 2013 and 2014. Is there something I should be doing differently?
Upvotes: 2
Views: 199
Reputation: 98398
You want %Y, not %G. %G is intended to be used with %V to show ISO 8601 week numbers, which may start before the beginning of the year or end after it ends.
Example:
use POSIX 'strftime';
print strftime("%Y-%m-%d week %G-%V\n",0,0,0,$_,11,111) for 25..31;
print strftime("%Y-%m-%d week %G-%V\n",0,0,0,$_,0,112) for 1..9;
output:
2011-12-25 week 2011-51
2011-12-26 week 2011-52
2011-12-27 week 2011-52
2011-12-28 week 2011-52
2011-12-29 week 2011-52
2011-12-30 week 2011-52
2011-12-31 week 2011-52
2012-01-01 week 2011-52
2012-01-02 week 2012-01
2012-01-03 week 2012-01
2012-01-04 week 2012-01
2012-01-05 week 2012-01
2012-01-06 week 2012-01
2012-01-07 week 2012-01
2012-01-08 week 2012-01
2012-01-09 week 2012-02
Upvotes: 8
Reputation: 50667
%G
is subject to ISO_8601
standard,
From perldoc
Consult your system's strftime() manpage for details about these and the other arguments.
%G The ISO 8601 week-based year (see NOTES) with century as a decimal number. The 4-digit year corresponding to the ISO week number (see %V). This has the same format and value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead. (TZ)
print POSIX::strftime("%a, %d %b %Y %T-0000",localtime(1325427034));
^__ gives 2012
Upvotes: 10