[PEAR-BUG] Bug #16520 [Com]: Compare returns invalid result for certain dates due to standard time conversion

[email protected] ("jeffdafoe")
Newsgroups php.pear.bugs
Message-ID <[email protected]>
Edit report at http://pear.php.net/bugs/bug.php?id=16520&edit=1

 ID:               16520
 Comment by:       jeffdafoe
 Reported By:      jeffdafoe at gmail dot com
 Summary:          Compare returns invalid result for certain dates due
                   to standard time conversion
 Status:           Open
 Type:             Bug
 Package:          Date
 Operating System: Irrelevant
 Package Version:  1.5.0a1
 PHP Version:      5.2.5
 New Comment:

Yes, the problem is the Standard time variables are used for the time
portion of the compare but not for the date portion.  We just noticed
the same issue here.  The patch for Date.php is:

@@ -4297,12 +4297,12 @@
             }
         }

-        $days1 = Date_Calc::dateToDays($d1->getDay(),
-                                       $d1->getMonth(),
-                                       $d1->getYear());
-        $days2 = Date_Calc::dateToDays($d2->getDay(),
-                                       $d2->getMonth(),
-                                       $d2->getYear());
+        $days1 = Date_Calc::dateToDays($d1->getStandardDay(),
+                                       $d1->getStandardMonth(),
+                                       $d1->getStandardYear());
+        $days2 = Date_Calc::dateToDays($d2->getStandardDay(),
+                                       $d2->getStandardMonth(),
+                                       $d2->getStandardYear());
         if ($days1 < $days2)
             return -1;
         if ($days1 > $days2)


Previous Comments:
------------------------------------------------------------------------

[2009-09-26 18:32:02] svanhoose

<div id="changeset">
<span class="removed">-Summary:          compare sometimes returns
invalid
                   result due to standard time conversion</span>
<span class="added">+Summary:          Compare returns invalid result
for
                   certain dates due to standard time conversion</span>
<span class="removed">-Operating System: Windows XP Professional</span>
<span class="added">+Operating System: Irrelevant</span>
</div>

------------------------------------------------------------------------

[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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.