Bug #16520 [Opn]: Compare returns invalid result for certain dates due to standard time conversion

From: Date: Sat, 26 Sep 2009 17:32:02 +0000
Subject: Bug #16520 [Opn]: Compare returns invalid result for certain dates due to standard time conversion
References: 1  Groups: php.pear.bugs 
Request: Send a blank email to pear-bugs+get-1734@lists.php.net to get a copy of this message
Edit report at http://pear.php.net/bugs/bug.php?id=16520&edit=1 ID: 16520 Updated by: spencer@instantdelay.com Reported By: spencer at instantdelay dot com -Summary: compare sometimes returns invalid result due to standard time conversion +Summary: Compare returns invalid result for certain dates due to standard time conversion Status: Open Type: Bug Package: Date -Operating System: Windows XP Professional +Operating System: Irrelevant Package Version: 1.5.0a1 PHP Version: 5.2.5 Roadmap Versions: New Comment: -Summary: compare sometimes returns invalid result due to standard time conversion +Summary: Compare returns invalid result for certain dates due to standard time conversion -Operating System: Windows XP Professional +Operating System: Irrelevant Previous Comments: ------------------------------------------------------------------------ [2009-08-13 20:06:21] svanhoose Sorry, I set the time zone incorrectly in my test script. Please use these lines instead: ----------- $d1->setTZByID("America/Los_Angeles"); $d2->setTZByID("America/Los_Angeles"); ------------------------------------------------------------------------ [2009-08-13 19:43:45] svanhoose Description: ------------ The Date::compare function uses incorrect logic to compare two dates. It first gets the value of each date in days (without converting to standard time). If the days are equal, it goes on to check hours, minutes, etc. However, it uses getStandardHour, etc. for those comparisons. This can cause problems for dates near day boundaries where this conversion can cause the hours to rollover to the previous day. e.g. 2009-08-07 10:00:00 and 2009-08-07 00:00:00 (both in America/Los_Angeles) The # of days are equal, so hours must be compared. For the first date getStandardHour() returns 9. On the second date, getStandardHour() returns 23. This causes compare to return that the 1st date is before the 2nd. Test script: --------------- <?php require_once 'Date.php'; $d1 = new Date("2009-08-07 10:00:00"); $d2 = new Date("2009-08-07 00:00:00"); $d1->setTZByID("UTC-0800"); $d2->setTZByID("UTC-8000"); $result = Date::compare($d1, $d2); echo "Is " . $d1->getDate() . " before " . $d2->getDate() . " ?\n"; echo ($result < 0 ? "Yes" : "No"); ?> Expected result: ---------------- The code should print No since obviously August 8th at 10 AM is not before August 8th at 12 AM. Actual result: -------------- The code prints Yes. ------------------------------------------------------------------------ -- Edit this bug report at http://pear.php.net/bugs/bug.php?id=16520&edit=1

« previous php.pear.bugs (#1734) next »