Subject: | Weird behavior in utc_year value in a DateTime around New Year’s Eve |
Date: | Tue, 4 Nov 2014 14:02:49 +0000 |
To: | "bug-DateTime [...] rt.cpan.org" <bug-DateTime [...] rt.cpan.org> |
From: | Matthew Gay <mgay [...] athenahealth.com> |
While working with a unit test, I ran across some strange behavior with the utc_year value of a date time. Given that utc_year is undocumented, this may not be worth fixing, but I thought it was strange enough to be worth reporting.
The following code snippet creates a DateTime late on New Year’s Eve in New York time, and then subtracts a minute from the DateTime. After the subtraction, the utc_year value jumps forward.
Code
use DateTime;
my $dt = DateTime->new(
year => 1950,
month => 12,
day => 31,
hour => 21,
minute => 0,
second => 0,
time_zone => 'America/New_York',
);
print "$dt\n";
print "UTC year: " . $dt->utc_year . "\n";
print "\nSubtracting a minute...\n\n";
$dt->subtract(minutes => 1);
print "$dt\n";
print "UTC year: " . $dt->utc_year . "\n";
Output
1950-12-31T21:00:00
UTC year: 1951
Subtracting a minute...
1950-12-31T20:59:00
UTC year: 1952
Note that the UTC year starts off as 1951 (which makes sense because it’s “next year” in UTC). But then the subtraction of a minute results in the year jumping forward another year, which is clearly wrong. As far as I can tell this doesn’t have an effect on the rest of the DateTime object though, so it should only affect people who are actually using utc_year.
Matt