Navigation Menu

Skip to content

Commit

Permalink
Added functions Date::isNull(), Date::isValidDate()
Browse files Browse the repository at this point in the history
Renamed Date::isTimeValid() to Date::isValidTime() for consistency
(function has only been available since current alpha release)


git-svn-id: http://svn.php.net/repository/pear/packages/Date/trunk@259537 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
Charles Woodcock committed May 11, 2008
1 parent 6f2f19c commit 4b2577b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 13 deletions.
84 changes: 74 additions & 10 deletions Date.php
Expand Up @@ -142,8 +142,9 @@
* <ol>
* <li>the user uses a time zone that does not observe Summer time, e.g. UTC</li>
* <li>the user never accesses the time, that is, he never makes a call to
* {@link Date::getHour()} or {@link Date::formatLikeStrftime("%H")}, for
* example, even if he sets the time to something invalid</li>
* {@link Date::getHour()} or {@link Date::formatLikeStrftime()} using
* format code '<b>%H</b>', for example, even if he sets the time to
* something invalid</li>
* <li>the user sets DATE_CORRECTINVALIDTIME_DEFAULT to true</li>
* </ol>
*
Expand Down Expand Up @@ -172,6 +173,7 @@
* allow an invalid date/time to be set regardless of the value of this
* constant.
*
* @see Date::isValidDate(), Date::isValidTime(), Date::isNull()
* @since Constant available since Release 1.5.0
*/
define('DATE_VALIDATE_DATE_BY_DEFAULT', false);
Expand Down Expand Up @@ -413,7 +415,7 @@ class Date
*
* @var bool
* @access private
* @see Date::isTimeValid()
* @see Date::isValidTime()
* @since Property available since Release 1.5.0
*/
var $ob_invalidtime = null;
Expand Down Expand Up @@ -552,6 +554,67 @@ function __clone()
}


// }}}
// {{{ isNull()

/**
* Returns whether the object is null (i.e. no date has been set)
*
* If the object is set to an invalid date, then this function will
* still return 'false'. To check whether the date is valid use
* either {@link Date::isValidDate()} (to check the day-month-year
* part of the object only) or {@link Date::isValidTime()} (to check
* the time, in addition to the day-month-year part).
*
* @return bool
* @access public
* @see Date::setDate(), Date::isValidDate(), Date::isValidTime()
* @since Method available since Release 1.5.0
*/
function isNull()
{
return is_null($this->year);
}


// }}}
// {{{ isValidDate()

/**
* Returns whether the date (i.e. day-month-year) is valid
*
* It is not possible to set the object to an invalid date using
* {@link Date::setDate()}, but it is possible to do so using the
* following functions:
*
* - {@link Date::setYear()}
* - {@link Date::setMonth()}
* - {@link Date::setDay()}
*
* However you can prevent this possibility (by default) by setting
* {@link DATE_VALIDATE_DATE_BY_DEFAULT} to 'true', in which case
* these three functions will return an error if they specify an
* invalid date.
*
* Note that this function only checks the day-month-year part of
* the object. Even if this is valid, it is still possible for the
* time to be invalid (see {@link DATE_CORRECTINVALIDTIME_DEFAULT}).
* To check the time as well, use {@link Date::isValidTime()}.
*
* @return bool
* @access public
* @see Date::setDate(), Date::isNull(), Date::isValidTime(),
* DATE_CORRECTINVALIDTIME_DEFAULT
* @since Method available since Release 1.5.0
*/
function isValidDate()
{
return
!Date::isNull() &&
Date_Calc::isValidDate($this->year, $this->month, $this->day);
}


// }}}
// {{{ setDate()

Expand Down Expand Up @@ -585,19 +648,20 @@ function __clone()
*
* @return void
* @access public
* @see Date::isNull(), Date::isValidDate(), Date::isValidTime()
*/
function setDate($date,
$format = DATE_FORMAT_ISO,
$pb_repeatedhourdefault = false)
{

if (preg_match('/^([0-9]{4,4})-?(0[1-9]|1[0-2])-?(0[1-9]|[12][0-9]|3[01])' .
'([T\s]?([01][0-9]|2[0-3]):?' . // [hh]
'([0-5][0-9]):?([0-5][0-9]|60)(\.\d+)?' . // [mi]:[ss]
'(Z|[+\-][0-9]{2,2}(:?[0-5][0-9])?)?)?$/i', // offset
$date, $regs) &&
$format != DATE_FORMAT_UNIXTIME
) {

// DATE_FORMAT_ISO, ISO_BASIC, ISO_EXTENDED, and TIMESTAMP
// These formats are extremely close to each other. This regex
// is very loose and accepts almost any butchered format you could
Expand Down Expand Up @@ -631,7 +695,6 @@ function setDate($date,
isset($regs[7]) ? $regs[7] : 0,
isset($regs[8]) ? $regs[8] : 0.0,
$pb_repeatedhourdefault);

} else if (is_numeric($date)) {
// Unix Time; N.B. Unix Time is defined relative to GMT,
// so it needs to be adjusted for the current time zone;
Expand All @@ -652,7 +715,7 @@ function setDate($date,
//
$this->convertTZByID($hs_id);
} else {
return PEAR::raiseError("Date not in ISO 8601 format",
return PEAR::raiseError("Date '$date' not in ISO 8601 format",
DATE_ERROR_INVALIDDATEFORMAT);
}
}
Expand Down Expand Up @@ -4381,9 +4444,9 @@ function compare($od1, $od2)
if (!Date::inEquivalentTimeZones($d1, $d2)) {
// Only a time zone with a valid time can be converted:
//
if ($d2->isTimeValid()) {
if ($d2->isValidTime()) {
$d2->convertTZByID($d1->getTZID());
} else if ($d1->isTimeValid()) {
} else if ($d1->isValidTime()) {
$d1->convertTZByID($d2->getTZID());
} else {
// No comparison can be made without guessing the time:
Expand Down Expand Up @@ -4917,7 +4980,7 @@ function _secondsInDayIsValid()


// }}}
// {{{ isTimeValid()
// {{{ isValidTime()

/**
* Whether the stored time is valid as a local time
Expand All @@ -4944,9 +5007,10 @@ function _secondsInDayIsValid()
*
* @return bool
* @access public
* @see Date::isValidDate, Date::isNull()
* @since Method available since Release 1.5.0
*/
function isTimeValid()
function isValidTime()
{
return !$this->ob_invalidtime;
}
Expand Down
5 changes: 2 additions & 3 deletions tests/bugs/bug-11313.phpt
Expand Up @@ -43,9 +43,8 @@ $tmp->format($PRINT_FORMAT));
?>
--EXPECT--
Actual date: 2007-03-25 03:00:04 MSD+04:00
Subtracting 5 seconds: 2007-03-25 01:59:59 MSD+03:00
Subtracting 20 minutes: 2007-03-25 01:40:04 MSD+03:00
Subtracting 5 seconds: 2007-03-25 01:59:59 MSK+03:00
Subtracting 20 minutes: 2007-03-25 01:40:04 MSK+03:00
Subtracting 2 hours 30 minutes: 2007-03-24 23:30:04 MSK+03:00
Subtracting 10 hours: 2007-03-24 16:00:04 MSK+03:00
Subtracting 3 days: 2007-03-22 02:00:04 MSK+03:00

0 comments on commit 4b2577b

Please sign in to comment.