Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Date/Format.pm
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ category of the program's locale.
%e like %d, but a leading zero is replaced by a space (eg 1..31)
%D MM/DD/YY
%F ISO 8601 date format: YYYY-MM-DD
%g ISO 8601 week-based year (2 digits)
%G GPS week number (weeks since January 6, 1980)
%h month abbr
%H hour, 24 hour clock, leading 0's)
Expand Down
29 changes: 21 additions & 8 deletions lib/Date/Format/Generic.pm
Original file line number Diff line number Diff line change
Expand Up @@ -244,21 +244,34 @@ sub format_OY { roman(format_Y(@_)) }

sub format_G { int(($_[0]->[9] - 315964800) / 604800) }

sub format_V {
sub _iso_week_year {
my $yday = $_[0]->[7];
my $year = $_[0]->[5] + 1900;
# Convert Perl wday (Sun=0) to Monday-based (Mon=0..Sun=6)
my $mwday = ($_[0]->[6] + 6) % 7;
# Day-of-year of the Thursday in this ISO week
my $thu = $yday - $mwday + 3;
if ($thu < 0) {
# Thursday falls in the previous year
my $py = $year - 1;
$thu += (($py % 4 == 0 && $py % 100 != 0) || $py % 400 == 0) ? 366 : 365;
return ($year - 1, undef);
}
my $ylen = (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0) ? 366 : 365;
return sprintf("%02d", 1) if $thu >= $ylen;
sprintf("%02d", int($thu / 7) + 1);
if ($thu >= $ylen) {
return ($year + 1, 1);
}
return ($year, int($thu / 7) + 1);
}

sub format_V {
my ($iso_year, $wk) = _iso_week_year($_[0]);
unless (defined $wk) {
my $py = $iso_year;
my $yday = $_[0]->[7];
my $mwday = ($_[0]->[6] + 6) % 7;
my $thu = $yday - $mwday + 3;
$thu += (($py % 4 == 0 && $py % 100 != 0) || $py % 400 == 0) ? 366 : 365;
$wk = int($thu / 7) + 1;
}
sprintf("%02d", $wk);
}

sub format_g { sprintf("%02d", (_iso_week_year($_[0]))[0] % 100) }

1;
15 changes: 14 additions & 1 deletion t/format.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Test::More tests => 314;
use Test::More tests => 324;
use Date::Format qw(ctime time2str);
use Date::Language;
use utf8;
Expand Down Expand Up @@ -35,6 +35,7 @@ __DATA__
%d 07
%e 7
%D 09/07/99
%g 99
%G 1026
%h Sep
%H 13
Expand Down Expand Up @@ -351,3 +352,15 @@ Dutch
1283926923 # Wed Sep 8 06:22:03 2010 GMT (AM)
%p VM
%P vm
1767182400 # Wed Dec 31 12:00:00 2025 GMT — ISO week 1 of 2026
%g 26
%V 01
%y 25
1451649600 # Fri Jan 1 12:00:00 2016 GMT — ISO week 53 of 2015
%g 15
%V 53
%y 16
1419854400 # Mon Dec 29 12:00:00 2014 GMT — ISO week 1 of 2015
%g 15
%V 01
%y 14
Loading