Req #49914 [Com]: DateInterval doesn't implement comparison functions
| From: | albertcasademont at gmail dot com | Date: | Fri, 25 Nov 2011 11:01:25 +0000 |
| Subject: | Req #49914 [Com]: DateInterval doesn't implement comparison functions | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-165492@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=49914&edit=1
ID: 49914
Comment by: albertcasademont at gmail dot com
Reported by: aharvey@php.net
Summary: DateInterval doesn't implement comparison functions
Status: Open
Type: Feature/Change Request
Package: Date/time related
Operating System: *
PHP Version: 5.3SVN-2009-10-18 (SVN)
Block user comment: N
Private report: N
New Comment:
Did this patch make it into the trunk?
Previous Comments:
------------------------------------------------------------------------
[2009-10-18 17:20:41] aharvey@php.net
Description:
------------
(This is really a feature request, rather than a bug per se.)
Unlike DateTime objects, DateInterval objects cannot easily be compared within PHP. While it would
be possible to concoct a workaround in userspace using a few calls to DateInterval::format() and
some arithmetic, it would probably be preferable to implement it within ext/date itself.
I've prepared a patch (yes, it even has a simple test) against PHP_5_3 at http://www.adamharvey.name/patches/DateInterval-comparators.patch
which implements comparator support. I can probably prepare a HEAD patch if necessary; I just
don't have a HEAD checkout to hand to do so at present.
There's one fairly significant issue with this patch worth noting: I've implemented a new
function (timelib_rel_time_to_seconds) which converts a timelib_rel_time structure to the number of
seconds it represents. The issue with this is that, as per bug #49778, we don't always know
exactly how many days a timelib_rel_time actually represents because of the varying number of days
in a month and year.
As per the comments in the function, for now I've fudged it and effectively chosen the default
length of a month and year out of thin air if the days field isn't set within the structure. If
the resolution to bug #49778 results in the days field always being filled in, then
timelib_rel_time_to_seconds can be simplified accordingly. Alternatively, we could only support this
in cases where we definitely know the days within the timelib_rel_time structure and error out
otherwise.
As a side note, I have a second patch that I can upload that implements support for a
DateInterval::getSeconds() method which effectively provides a PHP wrapper around the proposed
internal timelib_rel_time_to_seconds function. If it's decided to accept this approach, I can
create another bug to track getting that in.
Reproduce code:
---------------
<?php
$ten = new DateInterval('PT10S');
$twenty = new DateInterval('PT20S');
var_dump($ten < $twenty);
?>
Expected result:
----------------
bool(true)
Actual result:
--------------
bool(false)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=49914&edit=1