Bug #72243 [Com]: DateTimes fail to compare correctly when a DateTimeZone is set
| From: | Rican7 at gmail dot com | Date: | Thu, 19 May 2016 18:15:44 +0000 |
| Subject: | Bug #72243 [Com]: DateTimes fail to compare correctly when a DateTimeZone is set | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-201200@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72243&edit=1
ID: 72243
Comment by: Rican7 at gmail dot com
Reported by: Rican7 at gmail dot com
Summary: DateTimes fail to compare correctly when a
DateTimeZone is set
Status: Open
Type: Bug
Package: Date/time related
PHP Version: 7.0.6
Block user comment: N
Private report: N
New Comment:
These 3v4l scripts show the bug in action:
- Broken (TZ abbreviation): https://3v4l.org/WkjAA
- Working (IANA TZ): https://3v4l.org/MDqHr
Previous Comments:
------------------------------------------------------------------------
[2016-05-19 18:13:59] Rican7 at gmail dot com
Description:
------------
DateTime instances are supposed to be natively "comparable" (using
==,
>, <, etc).
Unfortunately, a colleague and I just noticed that if a Time Zone is updated on a compared instance,
than the comparison fails... but only if the time zone isn't an IANA identifier. Really
strange. What's worse, is that it seems to "magically" start to work again if a
(seemingly idempotent) getTimestamp() call is made on the instance that had the new
time-zone applied. Is this due to a lazy calculation of the internal timestamp that isn't
getting validated when a new time zone is applied?
Test script:
---------------
<?php
$datetime = new DateTime(null, new DateTimeZone('UTC'));
$datetime_2 = clone $datetime;
$datetime_2->add(new DateInterval('PT2S'));
var_dump($datetime < $datetime_2);
var_dump($datetime->getTimestamp() < $datetime_2->getTimestamp());
$datetime_2->setTimezone(new DateTimeZone('EST'));
var_dump($datetime < $datetime_2);
var_dump($datetime->getTimestamp() < $datetime_2->getTimestamp());
// Now that getTimestamp() has been called, it'll "magically" work...
var_dump($datetime < $datetime_2);
Expected result:
----------------
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Actual result:
--------------
bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72243&edit=1