Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.5.0a4

Bug #335 Date::addSeconds() adds hours instead of seconds
Submitted: 2003-12-02 02:31 UTC
From: aashley at optimiser dot com Assigned: pajoye
Status: Closed Package: Date
PHP Version: 4.3.3 OS: Gentoo Linux
Roadmaps: (Not assigned)    
Subscription  
Comments Add Comment Add patch


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 50 + 2 = ?

 
 [2003-12-02 02:31 UTC] aashley at optimiser dot com
Description: ------------ Date::addSeconds() incorrectly calls the creation of the of the Date_Span object so that the Span assumes that the number specified is in hours not seconds. Patch to fix code: --- /home/aashley/pear/Date/Date.php 2003-11-18 10:21:44.000000000 +0800 +++ /usr/share/pear/Date.php 2003-12-02 10:25:12.097214200 +0800 @@ -576,7 +576,7 @@ */ function addSeconds($sec) { - $this->addSpan(new Date_Span($sec)); + $this->addSpan(new Date_Span('00:00:'.$sec)); } /** Reproduce code: --------------- <?php $date = new Date('2003-01-01 00:00:00'); $date->addSeconds(10); print $date->getDate(); ?> Expected result: ---------------- 2003-01-01 00:00:10 Actual result: -------------- 2003-01-01 10:00:00

Comments

 [2003-12-03 21:32 UTC] baba at php dot net
Thank you for taking the time to write to us, but this is not a bug. reproduce code produces expected result for me. Date_Span code treates integer only paramater as seconds. please check against 1.4rc1 package and comment if you can still produce this error.
 [2003-12-04 02:11 UTC] aashley at optimiser dot com
Okay further investigation and I narrowed down the problem. The seconds value I was adding was being retireved from a database so was being passed to addSeconds as a string, which cases setFromString to be called which interperates the value as hours. The following code demonstrates this: <?php include_once 'Date.php'; $date = new Date('2003-12-01 03:09:16'); $date->addSeconds('10'); print 'Add 10 seconds as a string: '.$date->getDate()."\n"; $date = new Date('2003-12-01 03:09:16'); $date->addSeconds(10); print 'Add 10 seconds as an integer: '.$date->getDate()."\n"; ?> Patch to fix: --- /home/aashley/pear/Date/Date.php 2003-12-04 10:11:51.978938200 +0800 +++ /usr/share/pear/Date.php 2003-12-04 10:10:34.500899754 +0800 @@ -582,7 +582,7 @@ */ function addSeconds($sec) { - $this->addSpan(new Date_Span($sec)); + $this->addSpan(new Date_Span((integer)$sec)); } /**
 [2003-12-15 08:04 UTC] pajoye
Thank you for taking the time to write to us, but this is not a bug. Hello, This is not a bug. You have to cast the value yourself. We cannot force the value to be an integer. For instance the current implementation allows: $date->addSeconds(10); $date->addSeconds('00:00:10'); $date->addSeconds(new Date_Span('10','%g')); I like to keep it as flexible as it is now. Thanks for your feedback, pierre
 [2003-12-15 08:22 UTC] aashley at optimiser dot com
if thats your intended behaviour for the function why have it at all? its just works as an alias for addSpan(). the fix I did was to make it behave as the name of the function implies its doing, adding this value as seconds.
 [2003-12-15 08:32 UTC] pajoye
This bug has been fixed in CVS. In case this was a documentation problem, the fix will show up at the end of next Sunday (CET) on pear.php.net. In case this was a pear.php.net website problem, the change will show up on the website in short time. Thank you for the report, and for helping us make PEAR better. > if thats your intended behaviour for the function > why have it at all? > its just works as an alias for addSpan(). Reading back the documentation I realized I was wrong. I'm even happy to be wrong :): * @param int $sec the number of seconds to add thanks again :)