<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Date
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Date.php 2504 2011-12-28 07:35:29Z liu21st $
*/
/**
* Include needed Date classes
*/
require_once 'Zend/Date/DateObject.php';
require_once 'Zend/Locale.php';
require_once 'Zend/Locale/Format.php';
require_once 'Zend/Locale/Math.php';
/**
* @category Zend
* @package Zend_Date
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Date extends Zend_Date_DateObject
{
private $_locale = null;
// Fractional second variables
private $_fractional = 0;
private $_precision = 3;
private static $_options = array(
'format_type' => 'iso', // format for date strings 'iso' or 'php'
'fix_dst' => true, // fix dst on summer/winter time change
'extend_month' => false, // false - addMonth like SQL, true like excel
'cache' => null, // cache to set
'timesync' => null // timesync server to set
);
// Class wide Date Constants
const DAY = 'dd';
const DAY_SHORT = 'd';
const DAY_SUFFIX = 'SS';
const DAY_OF_YEAR = 'D';
const WEEKDAY = 'EEEE';
const WEEKDAY_SHORT = 'EEE';
const WEEKDAY_NARROW = 'E';
const WEEKDAY_NAME = 'EE';
const WEEKDAY_8601 = 'eee';
const WEEKDAY_DIGIT = 'e';
const WEEK = 'ww';
const MONTH = 'MM';
const MONTH_SHORT = 'M';
const MONTH_DAYS = 'ddd';
const MONTH_NAME = 'MMMM';
const MONTH_NAME_SHORT = 'MMM';
const MONTH_NAME_NARROW = 'MMMMM';
const YEAR = 'y';
const YEAR_SHORT = 'yy';
const YEAR_8601 = 'Y';
const YEAR_SHORT_8601 = 'YY';
const LEAPYEAR = 'l';
const MERIDIEM = 'a';
const SWATCH = 'B';
const HOUR = 'HH';
const HOUR_SHORT = 'H';
const HOUR_AM = 'hh';
const HOUR_SHORT_AM = 'h';
const MINUTE = 'mm';
const MINUTE_SHORT = 'm';
const SECOND = 'ss';
const SECOND_SHORT = 's';
const MILLISECOND = 'S';
const TIMEZONE_NAME = 'zzzz';
const DAYLIGHT = 'I';
const GMT_DIFF = 'Z';
const GMT_DIFF_SEP = 'ZZZZ';
const TIMEZONE = 'z';
const TIMEZONE_SECS = 'X';
const ISO_8601 = 'c';
const RFC_2822 = 'r';
const TIMESTAMP = 'U';
const ERA = 'G';
const ERA_NAME = 'GGGG';
const ERA_NARROW = 'GGGGG';
const DATES = 'F';
const DATE_FULL = 'FFFFF';
const DATE_LONG = 'FFFF';
const DATE_MEDIUM = 'FFF';
const DATE_SHORT = 'FF';
const TIMES = 'WW';
const TIME_FULL = 'TTTTT';
const TIME_LONG = 'TTTT';
const TIME_MEDIUM = 'TTT';
const TIME_SHORT = 'TT';
const DATETIME = 'K';
const DATETIME_FULL = 'KKKKK';
const DATETIME_LONG = 'KKKK';
const DATETIME_MEDIUM = 'KKK';
const DATETIME_SHORT = 'KK';
const ATOM = 'OOO';
const COOKIE = 'CCC';
const RFC_822 = 'R';
const RFC_850 = 'RR';
const RFC_1036 = 'RRR';
const RFC_1123 = 'RRRR';
const RFC_3339 = 'RRRRR';
const RSS = 'SSS';
const W3C = 'WWW';
/**
* Generates the standard date object, could be a unix timestamp, localized date,
* string, integer, array and so on. Also parts of dates or time are supported
* Always set the default timezone: http://php.net/date_default_timezone_set
* For example, in your bootstrap: date_default_timezone_set('America/Los_Angeles');
* For detailed instructions please look in the docu.
*
* @param string|integer|Zend_Date|array $date OPTIONAL Date value or value of date part to set
* ,depending on $part. If null the actual time is set
* @param string $part OPTIONAL Defines the input format of $date
* @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
* @return Zend_Date
* @throws Zend_Date_Exception
*/
public function __construct($date = null, $part = null, $locale = null)
{
if (is_object($date) and !($date instanceof Zend_TimeSync_Protocol) and
!($date instanceof Zend_Date)) {
if ($locale instanceof Zend_Locale) {
$locale = $date;
$date = null;
$part = null;
} else {
$date = (string) $date;
}
}
if (($date !== null) and !is_array($date) and !($date instanceof Zend_TimeSync_Protocol) and
!($date instanceof Zend_Date) and !defined($date) and Zend_Locale::isLocale($date, true, false)) {
$locale = $date;
$date = null;
$part = null;
} else if (($part !== null) and !defined($part) and Zend_Locale::isLocale($part, true, false)) {
$locale = $part;
$part = null;
}
$this->setLocale($locale);
if (is_string($date) && ($part === null) && (strlen($date) <= 5)) {
$part = $date;
$date = null;
}
if ($date === null) {
if ($part === null) {
$date = time();
} else if ($part !== self::TIMESTAMP) {
$date = self::now($locale);
$date = $date->get($part);
}
}
if ($date instanceof Zend_TimeSync_Protocol) {
$date = $date->getInfo();
$date = $this->_getTime($date['offset']);
$part = null;
} else if (parent::$_defaultOffset != 0) {
$date = $this->_getTime(parent::$_defaultOffset);
}
// set the timezone and offset for $this
$zone = @date_default_timezone_get();
$this->setTimezone($zone);
// try to get timezone from date-string
if (!is_int($date)) {
$zone = $this->getTimezoneFromString($date);
$this->setTimezone($zone);
}
// set datepart
if (($part !== null && $part !== self::TIMESTAMP) or (!is_numeric($date))) {
// switch off dst handling for value setting
$this->setUnixTimestamp($this->getGmtOffset());
$this->set($date, $part, $this->_locale);
// DST fix
if (is_array($date) === true) {
if (!isset($date['hour'])) {
$date['hour'] = 0;
}
$hour = $this->toString('H');
$hour = $date['hour'] - $hour;
switch ($hour) {
case 1 :
case -23 :
$this->addTimestamp(3600);