Subject: | Simple scalars are insufficient for DateTime math |
Date: | Tue, 02 Dec 2008 09:09:08 -0500 |
To: | bug-DateTime [...] rt.cpan.org |
From: | Andrew Switala <andy.switala [...] gmail.com> |
Hi,
The following applies to DateTime 0.45 on Perl 5.10.0 (Win32).
This is wrong:
$ perl -mDateTime -e "print ((DateTime->new(year => 1970, \
time_zone => 'UTC')->subtract_datetime_absolute(DateTime->new(year \
=> 1583, time_zone => 'UTC')))->seconds)"
12212553600
Conversely:
$ perl -mDateTime -e "print DateTime->new(year => 1583, \
time_zone => 'UTC')->add(seconds=>12212553600)"
1582-12-31T23:59:59
$ perl -mDateTime -mMath::BigInt -e "print DateTime->new(year \
=> 1583,time_zone=>'UTC')->add(seconds => \
Math::BigInt->new('12212553600'))"
The 'seconds' parameter ("12212553600") to DateTime::Duration::new was a
'hashref object', which is not one of the allowed types: scalar at
C:/strawberry/perl/site/lib/DateTime/Duration.pm line 26
DateTime::Duration::new(undef, 'seconds',
'Math::BigInt=HASH(0x1babaa4)'
) called at C:/strawberry/perl/site/lib/DateTime.pm line 1519
DateTime::add('DateTime=HASH(0x3a224)', 'seconds',
'Math::BigInt=HASH(0x1babaa4)') called at -e line 1
As a work-around, my code has to deal with three separate cases:
1. Prior to 1972 - before leap seconds were introduced, so convert delta
seconds > 2**32 to delta weeks and days < 2**32 (so far).
2. 1972 to 6 months from now - delta seconds fits in a 32 bit integer
3. More than 6 months from now - leap seconds are unknown, so delta
seconds are uncertain, might as well convert to delta weeks and days.
^^^ Not having to deal with this sort of thing is the reason one uses a
date library in the first place. DateTime is otherwise an excellent module.
--
Andy Switala