Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the DateTime CPAN distribution.

Report information
The Basics
Id: 113633
Status: resolved
Priority: 0/
Queue: DateTime

People
Owner: Nobody in particular
Requestors: earl.oliver [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: subtract_datetime_absolute issue
Date: Sat, 9 Apr 2016 15:44:14 -0700
To: bug-DateTime [...] rt.cpan.org
From: Earl Oliver <earl.oliver [...] gmail.com>
DateTime-> subtract_datetime_absolute does not yield correct differences between two DateTime objects. I am using DateTime as packaged with: Perl 5, version 18, subversion 2 (v5.18.2) built for darwin-thread-multi-2level The following sample code demonstrates the issue consistently: #!/usr/bin/perl use DateTime; my $result_date = DateTime->new( year => 2016, month => 04, day => 9, hour => 19, minute => 35, second => 24); my $dtx = DateTime->from_epoch(epoch => 1460229600); print "result_date epoch = ".$result_date->epoch()."\n"; print "dtx epoch = ".$dtx->epoch()."\n"; print "epoch diff: ".($result_date->epoch() - $dtx->epoch())."\n"; # 924, correct my $seconds = $result_date->subtract_datetime_absolute($dtx)->seconds; print 'subtract_datetime_absolute diff: '.$seconds."\n"; # 899, incorrect, off by 25 seconds.
Download signature.asc
application/pgp-signature 842b

Message body not shown because it is not plain text.

On Sat Apr 09 18:44:25 2016, earl.oliver@gmail.com wrote: Show quoted text
> DateTime-> subtract_datetime_absolute does not yield correct > differences between two DateTime objects. > > I am using DateTime as packaged with: Perl 5, version 18, subversion 2 > (v5.18.2) built for darwin-thread-multi-2level > > The following sample code demonstrates the issue consistently: > > #!/usr/bin/perl > > use DateTime; > > my $result_date = > DateTime->new( > year => 2016, > month => 04, > day => 9, > hour => 19, > minute => 35, > second => 24); > > my $dtx = DateTime->from_epoch(epoch => 1460229600); > > print "result_date epoch = ".$result_date->epoch()."\n"; > print "dtx epoch = ".$dtx->epoch()."\n"; > > print "epoch diff: ".($result_date->epoch() - $dtx->epoch())."\n"; > # 924, correct > > my $seconds = $result_date->subtract_datetime_absolute($dtx)->seconds; > print 'subtract_datetime_absolute diff: '.$seconds."\n"; > # 899, incorrect, off by 25 seconds.
This is a quirk of how DateTime.pm deals with leap seconds. The first date you created has no time zone at all, so it's in the floating time zone. The second is in the UTC tz, since it's created from an epoch. When you subtract one from the other, DateTime tries to account for leap second differences between the two, but this doesn't really work sanely when one object is in the floating zone and the other isn't. I added a note to the docs about this.