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
4 changes: 3 additions & 1 deletion lib/Date/Format/Generic.pm
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ sub format_Z {
sub format_z {
my $t = $_[0]->[9];
my $o = defined $tzname ? tz_offset($tzname, $t) : tz_offset(undef,$t);
sprintf("%+03d%02d", int($o / 3600), int(abs($o) % 3600) / 60);
my $sign = $o < 0 ? '-' : '+';
my $abs = abs($o);
sprintf("%s%02d%02d", $sign, int($abs / 3600), int($abs % 3600) / 60);
}

sub format_c { &format_x . " " . &format_X }
Expand Down
22 changes: 22 additions & 0 deletions t/format-z-sign.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use strict;
use warnings;
use Test::More tests => 6;
use Date::Format qw(time2str);

# Regression: format_z lost the sign for negative sub-hour timezone offsets
# because int(-1800/3600) truncates to 0, and sprintf("%+03d",...) renders
# 0 as "+00" instead of "-00".

my $epoch = 946684800; # 2000-01-01 00:00:00 UTC

# Negative sub-hour offsets (the bug)
is(time2str("%z", $epoch, "-0030"), "-0030", "%z preserves sign for -0030");
is(time2str("%z", $epoch, "-0015"), "-0015", "%z preserves sign for -0015");
is(time2str("%z", $epoch, "-0045"), "-0045", "%z preserves sign for -0045");

# Positive sub-hour offsets (control)
is(time2str("%z", $epoch, "+0030"), "+0030", "%z correct for +0030");
is(time2str("%z", $epoch, "+0545"), "+0545", "%z correct for +0545 (Nepal)");

# Zero offset
is(time2str("%z", $epoch, "+0000"), "+0000", "%z correct for UTC");
Loading