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: 37509
Status: resolved
Priority: 0/
Queue: DateTime

People
Owner: Nobody in particular
Requestors: steve.bruce [...] optusnet.com.au
Cc:
AdminCc:

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



Subject: UTC subtract_datetime returns 0 days for 30th July 08 - 30th June 08
Date: Tue, 08 Jul 2008 15:43:23 +1000
To: bug-datetime [...] rt.cpan.org
From: Steven Bruce <steve.bruce [...] optusnet.com.au>
Hi, I seem to have found a problem in using from_epochand subtract_datetime. I have not specified a timezone so presumably it's using UTC by default. The following code prints: "30-07-2008 - 30-06-2008 = 0" my $startDate = DateTime->from_epoch(epoch => "1214834400"); my $endDate = DateTime->from_epoch(epoch => "1217426400"); print $endDate->dmy; print(" - "); print $startDate->dmy; print(" = "); my $daysElapsed = $endDate->subtract_datetime($startDate); print $daysElapsed->in_units('days') . "\n"; Yet if I just set the 'day' to anything other than "30" it works, eg. "29-07-2008 - 30-06-2008 = 29" my $startDate = DateTime->from_epoch(epoch => "1214834400"); my $endDate = DateTime->from_epoch(epoch => "1217426400"); print $endDate->dmy; print(" - "); print $startDate->dmy; print(" = "); $endDate->set( day => '29'); my $daysElapsed = $endDate->subtract_datetime($startDate); print $daysElapsed->in_units('days') . "\n"; It also works if I set the timezone before doing the subtraction, which goes against the advice on CPAN "30-07-2008 - 1-07-2008 = 30" my $startDate = DateTime->from_epoch(epoch => "1214834400"); my $endDate = DateTime->from_epoch(epoch => "1217426400"); $startDate->set_time_zone("Australia/Sydney"); $endDate->set_time_zone("Australia/Sydney"); print $endDate->dmy; print(" - "); print $startDate->dmy; print(" = "); my $daysElapsed = $endDate->subtract_datetime($startDate); print $daysElapsed->in_units('days') . "\n"; cheers Steve +61 2 8082 7946
Subject: Re: [rt.cpan.org #37509] UTC subtract_datetime returns 0 days for 30th July 08 - 30th June 08
Date: Wed, 9 Jul 2008 08:37:17 +0200
To: bug-DateTime [...] rt.cpan.org
From: "Flavio S. Glock" <fglock [...] gmail.com>
If you are only interested in days (no months), then you need the delta_days method: use DateTime; my $startDate = DateTime->from_epoch(epoch => "1214834400"); my $endDate = DateTime->from_epoch(epoch => "1217426400"); print $endDate->dmy; print(" - "); print $startDate->dmy; print(" = "); my $daysElapsed = $endDate->subtract_datetime($startDate); print $daysElapsed->in_units('days') . " days\n"; print $daysElapsed->in_units('months') . " months\n"; $daysElapsed = $endDate->delta_days($startDate); print $daysElapsed->in_units('days') . " days\n"; print $daysElapsed->in_units('months') . " months\n";