From 4b2577bc839248cbbaebc06e5afe574c0e2b4c26 Mon Sep 17 00:00:00 2001 From: Charles Woodcock Date: Sun, 11 May 2008 13:52:45 +0000 Subject: [PATCH] Added functions Date::isNull(), Date::isValidDate() 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 --- Date.php | 84 ++++++++++++++++++++++++++++++++++----- tests/bugs/bug-11313.phpt | 5 +-- 2 files changed, 76 insertions(+), 13 deletions(-) diff --git a/Date.php b/Date.php index 8eb7cf2..564b278 100644 --- a/Date.php +++ b/Date.php @@ -142,8 +142,9 @@ *
    *
  1. the user uses a time zone that does not observe Summer time, e.g. UTC
  2. *
  3. 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
  4. + * {@link Date::getHour()} or {@link Date::formatLikeStrftime()} using + * format code '%H', for example, even if he sets the time to + * something invalid *
  5. the user sets DATE_CORRECTINVALIDTIME_DEFAULT to true
  6. *
* @@ -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); @@ -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; @@ -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() @@ -585,12 +648,12 @@ 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] @@ -598,6 +661,7 @@ function setDate($date, $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 @@ -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; @@ -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); } } @@ -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: @@ -4917,7 +4980,7 @@ function _secondsInDayIsValid() // }}} - // {{{ isTimeValid() + // {{{ isValidTime() /** * Whether the stored time is valid as a local time @@ -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; } diff --git a/tests/bugs/bug-11313.phpt b/tests/bugs/bug-11313.phpt index 30ffff6..716107a 100644 --- a/tests/bugs/bug-11313.phpt +++ b/tests/bugs/bug-11313.phpt @@ -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 -