ZF-9891: Zend_Date::equals() issues when 2 objects have different TZs
Description
In debugging an issue in Zend_Feed, I came across an odd issue in Zend_Date, summarized in the code below:
$ts = strtotime('Sat, 07 Mar 2009 08:03:50 +0000');
$date1 = new Zend_Date($ts);
echo "Via strtotime: ", $date1->get(Zend_Date::RFC_2822), "\n";
$date2 = new Zend_Date();
$date2->set('Sat, 07 Mar 2009 08:03:50 +0000', Zend_Date::RFC_2822);
echo "Via direct set: ", $date2->get(Zend_Date::RFC_2822), "\n";
echo "Date 1 equals Date 2? " . ($date2->equals($date1) ? 'yes' : 'no') . "\n";
This returns the following:
Via strtotime: Sat, 07 Mar 2009 03:03:50 -0500
Via direct set: Sat, 07 Mar 2009 08:03:50 +0000
Date 1 equals Date 2? no
(TZ information will differ based on your current date.timezone settings)
The important thing to note is that the two dates are, in fact, the same -- they simply display different timezones. However, Zend_Date::equals() evaluates them as unequal.
Using DateTime and a similar approach:
$ts = strtotime('Sat, 07 Mar 2009 08:03:50 +0000');
$date1 = new DateTime('@' . $ts);
echo "DateTime via strtotime: ", $date1->format('r'), "\n";
$date2 = new DateTime('Sat, 07 Mar 2009 08:03:50 +0000');
echo "DateTime via string: ", $date2->format('r'), "\n";
echo "Date 1 equals Date 2? " . (($date1 == $date2) ? 'yes' : 'no') . "\n";
I get the expected result -- these are equal dates. Interestingly, the TZ information is kept separate, and the dates as echoed match; if I set the timezone separately, it will then reflect that in the output, but the dates still will be equal.
summary: equals() should be looking at timestamps when evaluating.
Comments
Posted by Thomas Weidner (thomas) on 2010-05-30T03:15:13.000+0000
Fixed with r22321