/** * Tests getDateTime */ public function testgetDateTime() { try { $value = Zend_Locale_Format::getDateTime('no content'); $this->fail("exception expected"); } catch (Zend_Locale_Exception $e) { // success } $this->assertTrue(is_array(Zend_Locale_Format::getDateTime('10.11.2006 13:14:55', array('date_format' => 'dd.MM.yyyy HH:mm:ss')))); $options = array('date_format' => 'dd.MM.yy h:mm:ss a', 'locale' => 'en'); $this->assertTrue(is_array(Zend_Locale_Format::getDateTime('15.10.09 11:14:55 am', $options))); $this->assertTrue(is_array(Zend_Locale_Format::getDateTime('15.10.09 12:14:55 am', $options))); $this->assertTrue(is_array(Zend_Locale_Format::getDateTime('15.10.09 11:14:55 pm', $options))); $this->assertTrue(is_array(Zend_Locale_Format::getDateTime('15.10.09 12:14:55 pm', $options))); try { $value = Zend_Locale_Format::getDateTime('15.10.09 13:14:55', array('date_format' => 'nocontent')); $this->fail("exception expected"); } catch (Zend_Locale_Exception $e) { // success } try { $value = Zend_Locale_Format::getDateTime('15.10.09 13:14:55', array('date_format' => 'ZZZZ')); $this->fail("exception expected"); } catch (Zend_Locale_Exception $e) { // success } $value = Zend_Locale_Format::getDateTime('15.10.09 13:14:55', array('date_format' => 'dd.MM.yy HH:mm:ss.x')); $this->assertEquals(15, $value['day']); $this->assertEquals(10, $value['month']); $this->assertEquals(2009, $value['year']); $this->assertEquals(13, $value['hour']); $this->assertEquals(14, $value['minute']); $this->assertEquals(55, $value['second']); $this->assertEquals(8, count(Zend_Locale_Format::getDateTime('15.10.09 13:14:55', array('date_format' => 'dd.MM.yy HH:mm:ss')))); $value = Zend_Locale_Format::getDateTime('15.10.09 13:14:55', array('date_format' => 'dd.MM.yy HH:mm:ss')); $this->assertEquals(15, $value['day']); $this->assertEquals(10, $value['month']); $this->assertEquals(2009, $value['year']); $this->assertEquals(13, $value['hour']); $this->assertEquals(14, $value['minute']); $this->assertEquals(55, $value['second']); $value = Zend_Locale_Format::getDateTime('151009131455', array('date_format' => 'dd.MM.yy HH:mm:ss')); $this->assertEquals(15, $value['day']); $this->assertEquals(10, $value['month']); $this->assertEquals(2009, $value['year']); $this->assertEquals(13, $value['hour']); $this->assertEquals(14, $value['minute']); $this->assertEquals(55, $value['second']); }
/** * Calculates the date or object * * @param string $calc Calculation to make, one of: 'add'|'sub'|'cmp'|'copy'|'set' * @param string|integer|array|Zend_Date $date Date or datepart to calculate with * @param string $part Part of the date to calculate, if null the timestamp is used * @param string|Zend_Locale $locale Locale for parsing input * @return integer|string|Zend_Date new timestamp * @throws Zend_Date_Exception */ private function _calculate($calc, $date, $part, $locale) { if ($date === null) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception('parameter $date must be set, null is not allowed'); } if ($part !== null && strlen($part) !== 2 && Zend_Locale::isLocale($part, null, false)) { $locale = $part; $part = null; } if ($locale === null) { $locale = $this->getLocale(); } $locale = (string) $locale; // Create date parts $year = $this->toString(self::YEAR, 'iso'); $month = $this->toString(self::MONTH_SHORT, 'iso'); $day = $this->toString(self::DAY_SHORT, 'iso'); $hour = $this->toString(self::HOUR_SHORT, 'iso'); $minute = $this->toString(self::MINUTE_SHORT, 'iso'); $second = $this->toString(self::SECOND_SHORT, 'iso'); // If object extract value if ($date instanceof Zend_Date) { $date = $date->toString($part, 'iso', $locale); } if (is_array($date) === true) { if (empty($part) === false) { switch ($part) { // Fall through case self::DAY: case self::DAY_SHORT: if (isset($date['day']) === true) { $date = $date['day']; } break; // Fall through // Fall through case self::WEEKDAY_SHORT: case self::WEEKDAY: case self::WEEKDAY_8601: case self::WEEKDAY_DIGIT: case self::WEEKDAY_NARROW: case self::WEEKDAY_NAME: if (isset($date['weekday']) === true) { $date = $date['weekday']; $part = self::WEEKDAY_DIGIT; } break; case self::DAY_OF_YEAR: if (isset($date['day_of_year']) === true) { $date = $date['day_of_year']; } break; // Fall through // Fall through case self::MONTH: case self::MONTH_SHORT: case self::MONTH_NAME: case self::MONTH_NAME_SHORT: case self::MONTH_NAME_NARROW: if (isset($date['month']) === true) { $date = $date['month']; } break; // Fall through // Fall through case self::YEAR: case self::YEAR_SHORT: case self::YEAR_8601: case self::YEAR_SHORT_8601: if (isset($date['year']) === true) { $date = $date['year']; } break; // Fall through // Fall through case self::HOUR: case self::HOUR_AM: case self::HOUR_SHORT: case self::HOUR_SHORT_AM: if (isset($date['hour']) === true) { $date = $date['hour']; } break; // Fall through // Fall through case self::MINUTE: case self::MINUTE_SHORT: if (isset($date['minute']) === true) { $date = $date['minute']; } break; // Fall through // Fall through case self::SECOND: case self::SECOND_SHORT: if (isset($date['second']) === true) { $date = $date['second']; } break; // Fall through // Fall through case self::TIMEZONE: case self::TIMEZONE_NAME: if (isset($date['timezone']) === true) { $date = $date['timezone']; } break; case self::TIMESTAMP: if (isset($date['timestamp']) === true) { $date = $date['timestamp']; } break; case self::WEEK: if (isset($date['week']) === true) { $date = $date['week']; } break; case self::TIMEZONE_SECS: if (isset($date['gmtsecs']) === true) { $date = $date['gmtsecs']; } break; default: require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("datepart for part ({$part}) not found in array"); break; } } else { $hours = 0; if (isset($date['hour']) === true) { $hours = $date['hour']; } $minutes = 0; if (isset($date['minute']) === true) { $minutes = $date['minute']; } $seconds = 0; if (isset($date['second']) === true) { $seconds = $date['second']; } $months = 0; if (isset($date['month']) === true) { $months = $date['month']; } $days = 0; if (isset($date['day']) === true) { $days = $date['day']; } $years = 0; if (isset($date['year']) === true) { $years = $date['year']; } return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, $months, $days, $years, true), $this->mktime($hour, $minute, $second, $month, $day, $year, true), $hour); } } // $date as object, part of foreign date as own date switch ($part) { // day formats case self::DAY: if (is_numeric($date)) { return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true), $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, day expected", 0, null, $date); break; case self::WEEKDAY_SHORT: $daylist = Zend_Locale_Data::getList($locale, 'day'); $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale); $cnt = 0; foreach ($daylist as $key => $value) { if (strtoupper(iconv_substr($value, 0, 3, 'UTF-8')) == strtoupper($date)) { $found = $cnt; break; } ++$cnt; } // Weekday found if ($cnt < 7) { return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true), $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour); } // Weekday not found require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, weekday expected", 0, null, $date); break; case self::DAY_SHORT: if (is_numeric($date)) { return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true), $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, day expected", 0, null, $date); break; case self::WEEKDAY: $daylist = Zend_Locale_Data::getList($locale, 'day'); $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale); $cnt = 0; foreach ($daylist as $key => $value) { if (strtoupper($value) == strtoupper($date)) { $found = $cnt; break; } ++$cnt; } // Weekday found if ($cnt < 7) { return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true), $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour); } // Weekday not found require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, weekday expected", 0, null, $date); break; case self::WEEKDAY_8601: $weekday = (int) $this->toString(self::WEEKDAY_8601, 'iso', $locale); if (intval($date) > 0 and intval($date) < 8) { return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true), $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour); } // Weekday not found require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, weekday expected", 0, null, $date); break; case self::DAY_SUFFIX: require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception('day suffix not supported', 0, null, $date); break; case self::WEEKDAY_DIGIT: $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale); if (is_numeric($date) and intval($date) >= 0 and intval($date) < 7) { return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true), $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour); } // Weekday not found require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, weekday expected", 0, null, $date); break; case self::DAY_OF_YEAR: if (is_numeric($date)) { if ($calc == 'add' || $calc == 'sub') { $year = 1970; ++$date; ++$day; } return $this->_assign($calc, $this->mktime(0, 0, 0, 1, $date, $year, true), $this->mktime(0, 0, 0, $month, $day, $year, true), $hour); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, day expected", 0, null, $date); break; case self::WEEKDAY_NARROW: $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated')); $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale); $cnt = 0; foreach ($daylist as $key => $value) { if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($date)) { $found = $cnt; break; } ++$cnt; } // Weekday found if ($cnt < 7) { return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true), $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour); } // Weekday not found require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, weekday expected", 0, null, $date); break; case self::WEEKDAY_NAME: $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated')); $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale); $cnt = 0; foreach ($daylist as $key => $value) { if (strtoupper($value) == strtoupper($date)) { $found = $cnt; break; } ++$cnt; } // Weekday found if ($cnt < 7) { return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true), $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour); } // Weekday not found require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, weekday expected", 0, null, $date); break; // week formats // week formats case self::WEEK: if (is_numeric($date)) { $week = (int) $this->toString(self::WEEK, 'iso', $locale); return $this->_assign($calc, parent::mktime(0, 0, 0, 1, 1 + $date * 7, 1970, true), parent::mktime(0, 0, 0, 1, 1 + $week * 7, 1970, true), $hour); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, week expected", 0, null, $date); break; // month formats // month formats case self::MONTH_NAME: $monthlist = Zend_Locale_Data::getList($locale, 'month'); $cnt = 0; foreach ($monthlist as $key => $value) { if (strtoupper($value) == strtoupper($date)) { $found = $key; break; } ++$cnt; } $date = array_search($date, $monthlist); // Monthname found if ($cnt < 12) { $fixday = 0; if ($calc == 'add') { $date += $found; $calc = 'set'; if (self::$_options['extend_month'] == false) { $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false)); if ($parts['mday'] != $day) { $fixday = $parts['mday'] < $day ? -$parts['mday'] : $parts['mday'] - $day; } } } else { if ($calc == 'sub') { $date = $month - $found; $calc = 'set'; if (self::$_options['extend_month'] == false) { $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false)); if ($parts['mday'] != $day) { $fixday = $parts['mday'] < $day ? -$parts['mday'] : $parts['mday'] - $day; } } } } return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true), $this->mktime(0, 0, 0, $month, $day, $year, true), $hour); } // Monthname not found require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, month expected", 0, null, $date); break; case self::MONTH: if (is_numeric($date)) { $fixday = 0; if ($calc == 'add') { $date += $month; $calc = 'set'; if (self::$_options['extend_month'] == false) { $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false)); if ($parts['mday'] != $day) { $fixday = $parts['mday'] < $day ? -$parts['mday'] : $parts['mday'] - $day; } } } else { if ($calc == 'sub') { $date = $month - $date; $calc = 'set'; if (self::$_options['extend_month'] == false) { $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false)); if ($parts['mday'] != $day) { $fixday = $parts['mday'] < $day ? -$parts['mday'] : $parts['mday'] - $day; } } } } return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true), $this->mktime(0, 0, 0, $month, $day, $year, true), $hour); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, month expected", 0, null, $date); break; case self::MONTH_NAME_SHORT: $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated')); $cnt = 0; foreach ($monthlist as $key => $value) { if (strtoupper($value) == strtoupper($date)) { $found = $key; break; } ++$cnt; } $date = array_search($date, $monthlist); // Monthname found if ($cnt < 12) { $fixday = 0; if ($calc == 'add') { $date += $found; $calc = 'set'; if (self::$_options['extend_month'] === false) { $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false)); if ($parts['mday'] != $day) { $fixday = $parts['mday'] < $day ? -$parts['mday'] : $parts['mday'] - $day; } } } else { if ($calc == 'sub') { $date = $month - $found; $calc = 'set'; if (self::$_options['extend_month'] === false) { $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false)); if ($parts['mday'] != $day) { $fixday = $parts['mday'] < $day ? -$parts['mday'] : $parts['mday'] - $day; } } } } return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true), $this->mktime(0, 0, 0, $month, $day, $year, true), $hour); } // Monthname not found require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, month expected", 0, null, $date); break; case self::MONTH_SHORT: if (is_numeric($date) === true) { $fixday = 0; if ($calc === 'add') { $date += $month; $calc = 'set'; if (self::$_options['extend_month'] === false) { $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false)); if ($parts['mday'] != $day) { $fixday = $parts['mday'] < $day ? -$parts['mday'] : $parts['mday'] - $day; } } } else { if ($calc === 'sub') { $date = $month - $date; $calc = 'set'; if (self::$_options['extend_month'] === false) { $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false)); if ($parts['mday'] != $day) { $fixday = $parts['mday'] < $day ? -$parts['mday'] : $parts['mday'] - $day; } } } } return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true), $this->mktime(0, 0, 0, $month, $day, $year, true), $hour); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, month expected", 0, null, $date); break; case self::MONTH_DAYS: require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception('month days not supported', 0, null, $date); break; case self::MONTH_NAME_NARROW: $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'stand-alone', 'narrow')); $cnt = 0; foreach ($monthlist as $key => $value) { if (strtoupper($value) === strtoupper($date)) { $found = $key; break; } ++$cnt; } $date = array_search($date, $monthlist); // Monthname found if ($cnt < 12) { $fixday = 0; if ($calc === 'add') { $date += $found; $calc = 'set'; if (self::$_options['extend_month'] === false) { $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false)); if ($parts['mday'] != $day) { $fixday = $parts['mday'] < $day ? -$parts['mday'] : $parts['mday'] - $day; } } } else { if ($calc === 'sub') { $date = $month - $found; $calc = 'set'; if (self::$_options['extend_month'] === false) { $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false)); if ($parts['mday'] != $day) { $fixday = $parts['mday'] < $day ? -$parts['mday'] : $parts['mday'] - $day; } } } } return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true), $this->mktime(0, 0, 0, $month, $day, $year, true), $hour); } // Monthname not found require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, month expected", 0, null, $date); break; // year formats // year formats case self::LEAPYEAR: require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception('leap year not supported', 0, null, $date); break; case self::YEAR_8601: if (is_numeric($date)) { if ($calc === 'add') { $date += $year; $calc = 'set'; } else { if ($calc === 'sub') { $date = $year - $date; $calc = 'set'; } } return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true), $this->mktime(0, 0, 0, $month, $day, $year, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, year expected", 0, null, $date); break; case self::YEAR: if (is_numeric($date)) { if ($calc === 'add') { $date += $year; $calc = 'set'; } else { if ($calc === 'sub') { $date = $year - $date; $calc = 'set'; } } return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true), $this->mktime(0, 0, 0, $month, $day, $year, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, year expected", 0, null, $date); break; case self::YEAR_SHORT: if (is_numeric($date)) { $date = intval($date); if ($calc == 'set' || $calc == 'cmp') { $date = self::getFullYear($date); } if ($calc === 'add') { $date += $year; $calc = 'set'; } else { if ($calc === 'sub') { $date = $year - $date; $calc = 'set'; } } return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true), $this->mktime(0, 0, 0, $month, $day, $year, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, year expected", 0, null, $date); break; case self::YEAR_SHORT_8601: if (is_numeric($date)) { $date = intval($date); if ($calc === 'set' || $calc === 'cmp') { $date = self::getFullYear($date); } if ($calc === 'add') { $date += $year; $calc = 'set'; } else { if ($calc === 'sub') { $date = $year - $date; $calc = 'set'; } } return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true), $this->mktime(0, 0, 0, $month, $day, $year, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, year expected", 0, null, $date); break; // time formats // time formats case self::MERIDIEM: require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception('meridiem not supported', 0, null, $date); break; case self::SWATCH: if (is_numeric($date)) { $rest = intval($date); $hours = floor($rest * 24 / 1000); $rest = $rest - $hours * 1000 / 24; $minutes = floor($rest * 1440 / 1000); $rest = $rest - $minutes * 1000 / 1440; $seconds = floor($rest * 86400 / 1000); return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, 1, 1, 1970, true), $this->mktime($hour, $minute, $second, 1, 1, 1970, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, swatchstamp expected", 0, null, $date); break; case self::HOUR_SHORT_AM: if (is_numeric($date)) { return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true), $this->mktime($hour, 0, 0, 1, 1, 1970, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, hour expected", 0, null, $date); break; case self::HOUR_SHORT: if (is_numeric($date)) { return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true), $this->mktime($hour, 0, 0, 1, 1, 1970, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, hour expected", 0, null, $date); break; case self::HOUR_AM: if (is_numeric($date)) { return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true), $this->mktime($hour, 0, 0, 1, 1, 1970, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, hour expected", 0, null, $date); break; case self::HOUR: if (is_numeric($date)) { return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true), $this->mktime($hour, 0, 0, 1, 1, 1970, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, hour expected", 0, null, $date); break; case self::MINUTE: if (is_numeric($date)) { return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true), $this->mktime(0, $minute, 0, 1, 1, 1970, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, minute expected", 0, null, $date); break; case self::SECOND: if (is_numeric($date)) { return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true), $this->mktime(0, 0, $second, 1, 1, 1970, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, second expected", 0, null, $date); break; case self::MILLISECOND: if (is_numeric($date)) { switch ($calc) { case 'set': return $this->setMillisecond($date); break; case 'add': return $this->addMillisecond($date); break; case 'sub': return $this->subMillisecond($date); break; } return $this->compareMillisecond($date); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, milliseconds expected", 0, null, $date); break; case self::MINUTE_SHORT: if (is_numeric($date)) { return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true), $this->mktime(0, $minute, 0, 1, 1, 1970, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, minute expected", 0, null, $date); break; case self::SECOND_SHORT: if (is_numeric($date)) { return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true), $this->mktime(0, 0, $second, 1, 1, 1970, true), false); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, second expected", 0, null, $date); break; // timezone formats // break intentionally omitted // timezone formats // break intentionally omitted case self::TIMEZONE_NAME: case self::TIMEZONE: case self::TIMEZONE_SECS: require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception('timezone not supported', 0, null, $date); break; case self::DAYLIGHT: require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception('daylight not supported', 0, null, $date); break; case self::GMT_DIFF: case self::GMT_DIFF_SEP: require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception('gmtdiff not supported', 0, null, $date); break; // date strings // date strings case self::ISO_8601: // (-)YYYY-MM-dd preg_match('/^(-{0,1}\\d{4})-(\\d{2})-(\\d{2})/', $date, $datematch); // (-)YY-MM-dd if (empty($datematch)) { preg_match('/^(-{0,1}\\d{2})-(\\d{2})-(\\d{2})/', $date, $datematch); } // (-)YYYYMMdd if (empty($datematch)) { preg_match('/^(-{0,1}\\d{4})(\\d{2})(\\d{2})/', $date, $datematch); } // (-)YYMMdd if (empty($datematch)) { preg_match('/^(-{0,1}\\d{2})(\\d{2})(\\d{2})/', $date, $datematch); } $tmpdate = $date; if (!empty($datematch)) { $dateMatchCharCount = iconv_strlen($datematch[0], 'UTF-8'); $tmpdate = iconv_substr($date, $dateMatchCharCount, iconv_strlen($date, 'UTF-8') - $dateMatchCharCount, 'UTF-8'); } // (T)hh:mm:ss preg_match('/[T,\\s]{0,1}(\\d{2}):(\\d{2}):(\\d{2})/', $tmpdate, $timematch); if (empty($timematch)) { preg_match('/[T,\\s]{0,1}(\\d{2})(\\d{2})(\\d{2})/', $tmpdate, $timematch); } if (empty($datematch) and empty($timematch)) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("unsupported ISO8601 format ({$date})", 0, null, $date); } if (!empty($timematch)) { $timeMatchCharCount = iconv_strlen($timematch[0], 'UTF-8'); $tmpdate = iconv_substr($tmpdate, $timeMatchCharCount, iconv_strlen($tmpdate, 'UTF-8') - $timeMatchCharCount, 'UTF-8'); } if (empty($datematch)) { $datematch[1] = 1970; $datematch[2] = 1; $datematch[3] = 1; } else { if (iconv_strlen($datematch[1], 'UTF-8') == 2) { $datematch[1] = self::getFullYear($datematch[1]); } } if (empty($timematch)) { $timematch[1] = 0; $timematch[2] = 0; $timematch[3] = 0; } if ($calc == 'set' || $calc == 'cmp') { --$datematch[2]; --$month; --$datematch[3]; --$day; $datematch[1] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime($timematch[1], $timematch[2], $timematch[3], 1 + $datematch[2], 1 + $datematch[3], 1970 + $datematch[1], false), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false); break; case self::RFC_2822: $result = preg_match('/^\\w{3},\\s(\\d{1,2})\\s(\\w{3})\\s(\\d{4})\\s' . '(\\d{2}):(\\d{2}):{0,1}(\\d{0,2})\\s([+-]' . '{1}\\d{4}|\\w{1,20})$/', $date, $match); if (!$result) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("no RFC 2822 format ({$date})", 0, null, $date); } $months = $this->_getDigitFromName($match[2]); if ($calc == 'set' || $calc == 'cmp') { --$months; --$month; --$match[1]; --$day; $match[3] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false); break; case self::TIMESTAMP: if (is_numeric($date)) { return $this->_assign($calc, $date, $this->getUnixTimestamp()); } require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, timestamp expected", 0, null, $date); break; // additional formats // break intentionally omitted // additional formats // break intentionally omitted case self::ERA: case self::ERA_NAME: require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception('era not supported', 0, null, $date); break; case self::DATES: try { $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true)); if ($calc == 'set' || $calc == 'cmp') { --$parsed['month']; --$month; --$parsed['day']; --$day; $parsed['year'] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true), $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::DATE_FULL: try { $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full')); $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale)); if ($calc == 'set' || $calc == 'cmp') { --$parsed['month']; --$month; --$parsed['day']; --$day; $parsed['year'] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true), $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::DATE_LONG: try { $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long')); $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale)); if ($calc == 'set' || $calc == 'cmp') { --$parsed['month']; --$month; --$parsed['day']; --$day; $parsed['year'] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true), $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::DATE_MEDIUM: try { $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium')); $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale)); if ($calc == 'set' || $calc == 'cmp') { --$parsed['month']; --$month; --$parsed['day']; --$day; $parsed['year'] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true), $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::DATE_SHORT: try { $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short')); $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale)); $parsed['year'] = self::getFullYear($parsed['year']); if ($calc == 'set' || $calc == 'cmp') { --$parsed['month']; --$month; --$parsed['day']; --$day; $parsed['year'] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true), $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::TIMES: try { if ($calc != 'set') { $month = 1; $day = 1; $year = 1970; } $parsed = Zend_Locale_Format::getTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true)); return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true), $this->mktime($hour, $minute, $second, $month, $day, $year, true), false); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::TIME_FULL: try { $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full')); $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale)); if ($calc != 'set') { $month = 1; $day = 1; $year = 1970; } if (!isset($parsed['second'])) { $parsed['second'] = 0; } return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true), $this->mktime($hour, $minute, $second, $month, $day, $year, true), false); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::TIME_LONG: try { $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long')); $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale)); if ($calc != 'set') { $month = 1; $day = 1; $year = 1970; } return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true), $this->mktime($hour, $minute, $second, $month, $day, $year, true), false); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::TIME_MEDIUM: try { $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium')); $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale)); if ($calc != 'set') { $month = 1; $day = 1; $year = 1970; } return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true), $this->mktime($hour, $minute, $second, $month, $day, $year, true), false); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::TIME_SHORT: try { $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short')); $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale)); if ($calc != 'set') { $month = 1; $day = 1; $year = 1970; } if (!isset($parsed['second'])) { $parsed['second'] = 0; } return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true), $this->mktime($hour, $minute, $second, $month, $day, $year, true), false); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::DATETIME: try { $parsed = Zend_Locale_Format::getDateTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true)); if ($calc == 'set' || $calc == 'cmp') { --$parsed['month']; --$month; --$parsed['day']; --$day; $parsed['year'] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::DATETIME_FULL: try { $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full')); $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale)); if ($calc == 'set' || $calc == 'cmp') { --$parsed['month']; --$month; --$parsed['day']; --$day; $parsed['year'] -= 1970; $year -= 1970; } if (!isset($parsed['second'])) { $parsed['second'] = 0; } return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::DATETIME_LONG: try { $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long')); $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale)); if ($calc == 'set' || $calc == 'cmp') { --$parsed['month']; --$month; --$parsed['day']; --$day; $parsed['year'] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::DATETIME_MEDIUM: try { $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium')); $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale)); if ($calc == 'set' || $calc == 'cmp') { --$parsed['month']; --$month; --$parsed['day']; --$day; $parsed['year'] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; case self::DATETIME_SHORT: try { $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short')); $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale)); $parsed['year'] = self::getFullYear($parsed['year']); if ($calc == 'set' || $calc == 'cmp') { --$parsed['month']; --$month; --$parsed['day']; --$day; $parsed['year'] -= 1970; $year -= 1970; } if (!isset($parsed['second'])) { $parsed['second'] = 0; } return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } break; // ATOM and RFC_3339 are identical // ATOM and RFC_3339 are identical case self::ATOM: case self::RFC_3339: $result = preg_match('/^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})\\d{0,4}([+-]{1}\\d{2}:\\d{2}|Z)$/', $date, $match); if (!$result) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, ATOM format expected", 0, null, $date); } if ($calc == 'set' || $calc == 'cmp') { --$match[2]; --$month; --$match[3]; --$day; $match[1] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false); break; case self::COOKIE: $result = preg_match("/^\\w{6,9},\\s(\\d{2})-(\\w{3})-(\\d{2})\\s(\\d{2}):(\\d{2}):(\\d{2})\\s.{3,20}\$/", $date, $match); if (!$result) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, COOKIE format expected", 0, null, $date); } $matchStartPos = iconv_strpos($match[0], ' ', 0, 'UTF-8') + 1; $match[0] = iconv_substr($match[0], $matchStartPos, iconv_strlen($match[0], 'UTF-8') - $matchStartPos, 'UTF-8'); $months = $this->_getDigitFromName($match[2]); $match[3] = self::getFullYear($match[3]); if ($calc == 'set' || $calc == 'cmp') { --$months; --$month; --$match[1]; --$day; $match[3] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false); break; case self::RFC_822: case self::RFC_1036: // new RFC 822 format, identical to RFC 1036 standard $result = preg_match('/^\\w{0,3},{0,1}\\s{0,1}(\\d{1,2})\\s(\\w{3})\\s(\\d{2})\\s(\\d{2}):(\\d{2}):{0,1}(\\d{0,2})\\s([+-]{1}\\d{4}|\\w{1,20})$/', $date, $match); if (!$result) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, RFC 822 date format expected", 0, null, $date); } $months = $this->_getDigitFromName($match[2]); $match[3] = self::getFullYear($match[3]); if ($calc == 'set' || $calc == 'cmp') { --$months; --$month; --$match[1]; --$day; $match[3] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false); break; case self::RFC_850: $result = preg_match('/^\\w{6,9},\\s(\\d{2})-(\\w{3})-(\\d{2})\\s(\\d{2}):(\\d{2}):(\\d{2})\\s.{3,21}$/', $date, $match); if (!$result) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, RFC 850 date format expected", 0, null, $date); } $months = $this->_getDigitFromName($match[2]); $match[3] = self::getFullYear($match[3]); if ($calc == 'set' || $calc == 'cmp') { --$months; --$month; --$match[1]; --$day; $match[3] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false); break; case self::RFC_1123: $result = preg_match('/^\\w{0,3},{0,1}\\s{0,1}(\\d{1,2})\\s(\\w{3})\\s(\\d{2,4})\\s(\\d{2}):(\\d{2}):{0,1}(\\d{0,2})\\s([+-]{1}\\d{4}|\\w{1,20})$/', $date, $match); if (!$result) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, RFC 1123 date format expected", 0, null, $date); } $months = $this->_getDigitFromName($match[2]); if ($calc == 'set' || $calc == 'cmp') { --$months; --$month; --$match[1]; --$day; $match[3] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false); break; case self::RSS: $result = preg_match('/^\\w{3},\\s(\\d{2})\\s(\\w{3})\\s(\\d{2,4})\\s(\\d{1,2}):(\\d{2}):(\\d{2})\\s.{1,21}$/', $date, $match); if (!$result) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, RSS date format expected", 0, null, $date); } $months = $this->_getDigitFromName($match[2]); $match[3] = self::getFullYear($match[3]); if ($calc == 'set' || $calc == 'cmp') { --$months; --$month; --$match[1]; --$day; $match[3] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false); break; case self::W3C: $result = preg_match('/^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})[+-]{1}\\d{2}:\\d{2}$/', $date, $match); if (!$result) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("invalid date ({$date}) operand, W3C date format expected", 0, null, $date); } if ($calc == 'set' || $calc == 'cmp') { --$match[2]; --$month; --$match[3]; --$day; $match[1] -= 1970; $year -= 1970; } return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true), $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false); break; default: if (!is_numeric($date) || !empty($part)) { try { if (empty($part)) { $part = Zend_Locale_Format::getDateFormat($locale) . " "; $part .= Zend_Locale_Format::getTimeFormat($locale); } $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $part, 'locale' => $locale, 'fix_date' => true, 'format_type' => 'iso')); if (strpos(strtoupper($part), 'YY') !== false and strpos(strtoupper($part), 'YYYY') === false) { $parsed['year'] = self::getFullYear($parsed['year']); } if ($calc == 'set' || $calc == 'cmp') { if (isset($parsed['month'])) { --$parsed['month']; } else { $parsed['month'] = 0; } if (isset($parsed['day'])) { --$parsed['day']; } else { $parsed['day'] = 0; } if (!isset($parsed['year'])) { $parsed['year'] = 1970; } } return $this->_assign($calc, $this->mktime(isset($parsed['hour']) ? $parsed['hour'] : 0, isset($parsed['minute']) ? $parsed['minute'] : 0, isset($parsed['second']) ? $parsed['second'] : 0, isset($parsed['month']) ? 1 + $parsed['month'] : 1, isset($parsed['day']) ? 1 + $parsed['day'] : 1, $parsed['year'], false), $this->getUnixTimestamp(), false); } catch (Zend_Locale_Exception $e) { if (!is_numeric($date)) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date); } } } return $this->_assign($calc, $date, $this->getUnixTimestamp(), false); break; } }
/** * Reads a localized value, normalizes and returns it. * Returns an empty string if no value is passed. * @param mixed $value * @param string $type (boolean|date|time|integer|float|decimal|currency) * @return mixed */ private function _normalize($value, $type, $num_of_decimals) { if (strlen($value) == 0) { return ''; } switch ($type) { case 'boolean': $yes_no = Zend_Locale_Data::getContent($this->_locale_engine, 'questionstrings'); $yes_regexp = '/^(' . str_replace(':', '|', $yes_no['yes']) . ')$/i'; if (preg_match($yes_regexp, $value)) { return 1; } return 0; case 'date': $date = Zend_Locale_Format::getDate($value, array('locale' => $this->_locale_engine, 'fix_date' => true)); $date['month'] = str_pad($date['month'], 2, 0, STR_PAD_LEFT); $date['day'] = str_pad($date['day'], 2, 0, STR_PAD_LEFT); return "{$date['year']}-{$date['month']}-{$date['day']}"; case 'time': $date_format = Zend_Locale_Format::getTimeFormat($this->_locale_engine); $date = Zend_Locale_Format::getDate($value, array('date_format' => $date_format, 'locale' => $this->_locale_engine)); if (!isset($date['hour'])) { $date['hour'] = '00'; } if (!isset($date['minute'])) { $date['minute'] = '00'; } if (!isset($date['second'])) { $date['second'] = '00'; } return "{$date['hour']}:{$date['minute']}:{$date['second']}"; case 'datetime': $date = Zend_Locale_Format::getDateTime($value, array('locale' => $this->_locale_engine, 'fix_date' => true)); $date['month'] = str_pad($date['month'], 2, 0, STR_PAD_LEFT); $date['day'] = str_pad($date['day'], 2, 0, STR_PAD_LEFT); $date['hour'] = str_pad($date['hour'], 2, 0, STR_PAD_LEFT); $date['minute'] = str_pad($date['minute'], 2, 0, STR_PAD_LEFT); $date['second'] = str_pad($date['second'], 2, 0, STR_PAD_LEFT); return "{$date['year']}-{$date['month']}-{$date['day']} {$date['hour']}:{$date['minute']}:{$date['second']}"; break; case 'integer': return Zend_Locale_Format::getInteger($value, array('locale' => $this->_locale_engine)); case 'float': if ($num_of_decimals === null) { $num_of_decimals = 3; } return Zend_Locale_Format::getFloat($value, array('precision' => $num_of_decimals, 'locale' => $this->_locale_engine)); case 'decimal': if ($num_of_decimals === null) { $num_of_decimals = 2; } return Zend_Locale_Format::getFloat($value, array('precision' => $num_of_decimals, 'locale' => $this->_locale_engine)); } return $value; }