00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class Mage_Core_Model_Date
00034 {
00035
00036
00037
00038
00039
00040 private $_offset = 0;
00041
00042
00043
00044
00045
00046
00047 private $_systemOffset = 0;
00048
00049
00050
00051
00052
00053 public function __construct()
00054 {
00055 $this->_offset = $this->calculateOffset($this->_getConfigTimezone());
00056 $this->_systemOffset = $this->calculateOffset();
00057 }
00058
00059
00060
00061
00062
00063
00064 protected function _getConfigTimezone()
00065 {
00066 return Mage::app()->getStore()->getConfig('general/locale/timezone');
00067 }
00068
00069
00070
00071
00072
00073
00074
00075 public function calculateOffset($timezone = null)
00076 {
00077 $result = true;
00078 $offset = 0;
00079
00080 if (!is_null($timezone)){
00081 $oldzone = @date_default_timezone_get();
00082 $result = date_default_timezone_set($timezone);
00083 }
00084
00085 if ($result === true) {
00086 $offset = gmmktime(0, 0, 0, 1, 2, 1970) - mktime(0, 0, 0, 1, 2, 1970);
00087 }
00088
00089 if (!is_null($timezone)){
00090 date_default_timezone_set($oldzone);
00091 }
00092
00093 return $offset;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103 public function gmtDate($format = null, $input = null)
00104 {
00105 if (is_null($format)) {
00106 $format = 'Y-m-d H:i:s';
00107 }
00108
00109 $result = date($format, $this->gmtTimestamp($input));
00110 return $result;
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120 public function date($format = null, $input = null)
00121 {
00122 if (is_null($format)) {
00123 $format = 'Y-m-d H:i:s';
00124 }
00125
00126 $result = date($format, $this->timestamp($input));
00127 return $result;
00128 }
00129
00130
00131
00132
00133
00134
00135 public function gmtTimestamp($input = null)
00136 {
00137 if (is_null($input)) {
00138 return gmdate('U');
00139 } else if (is_numeric($input)) {
00140 $result = $input;
00141 } else {
00142 $result = strtotime($input);
00143 }
00144
00145 $date = Mage::app()->getLocale()->date($result);
00146 $timestamp = $date->get(Zend_Date::TIMESTAMP) - $date->get(Zend_Date::TIMEZONE_SECS);
00147
00148 unset($date);
00149 return $timestamp;
00150
00151 }
00152
00153
00154
00155
00156
00157
00158
00159 public function timestamp($input = null)
00160 {
00161 if (is_null($input)) {
00162 $result = $this->gmtTimestamp();
00163 } else if (is_numeric($input)) {
00164 $result = $input;
00165 } else {
00166 $result = strtotime($input);
00167 }
00168
00169 $date = Mage::app()->getLocale()->date($result);
00170 $timestamp = $date->get(Zend_Date::TIMESTAMP) + $date->get(Zend_Date::TIMEZONE_SECS);
00171
00172 unset($date);
00173 return $timestamp;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182 public function getGmtOffset($type = 'seconds')
00183 {
00184 $result = $this->_offset;
00185 switch ($type) {
00186 case 'seconds':
00187 default:
00188 break;
00189
00190 case 'minutes':
00191 $result = $result / 60;
00192 break;
00193
00194 case 'hours':
00195 $result = $result / 60 / 60;
00196 break;
00197 }
00198 return $result;
00199 }
00200
00201
00202
00203
00204 public function checkDateTime($year, $month, $day, $hour = 0, $minute = 0, $second = 0)
00205 {
00206 if (!checkdate($month, $day, $year)) {
00207 return false;
00208 }
00209 foreach (array('hour' => 23, 'minute' => 59, 'second' => 59) as $var => $maxValue) {
00210 $value = (int)$$var;
00211 if (($value < 0) || ($value > $maxValue)) {
00212 return false;
00213 }
00214 }
00215 return true;
00216 }
00217
00218
00219
00220
00221 public function parseDateTime($dateTimeString, $dateTimeFormat)
00222 {
00223
00224 $isSupportedFormatFound = false;
00225 foreach (array(
00226
00227 '%m/%d/%y %I:%M' => array('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2})/', array('y' => 3, 'm' => 1, 'd' => 2, 'h' => 4, 'i' => 5)),
00228 'm/d/y h:i' => array('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2})/', array('y' => 3, 'm' => 1, 'd' => 2, 'h' => 4, 'i' => 5)),
00229 '%m/%d/%y' => array('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/', array('y' => 3, 'm' => 1, 'd' => 2)),
00230 'm/d/y' => array('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/', array('y' => 3, 'm' => 1, 'd' => 2)),
00231 ) as $supportedFormat => $regRule) {
00232 if (false !== strpos($dateTimeFormat, $supportedFormat, 0)) {
00233 $isSupportedFormatFound = true;
00234 break;
00235 }
00236 }
00237 if (!$isSupportedFormatFound) {
00238 Mage::throwException(Mage::helper('core')->__('Date/time format "%s" is not supported.', $dateTimeFormat));
00239 }
00240
00241
00242 $regex = array_shift($regRule);
00243 $mask = array_shift($regRule);
00244 if (!preg_match($regex, $dateTimeString, $matches)) {
00245 Mage::throwException(Mage::helper('core')->__('Specified date/time "%1$s" do not match format "%2$s".', $dateTimeString, $dateTimeFormat));
00246 }
00247
00248
00249 $result = array();
00250 foreach (array('y', 'm', 'd', 'h', 'i', 's') as $key) {
00251 $value = 0;
00252 if (isset($mask[$key]) && isset($matches[$mask[$key]])) {
00253 $value = (int)$matches[$mask[$key]];
00254 }
00255 $result[] = $value;
00256 }
00257
00258
00259 if ($result[0] < 100) {
00260 $result[0] = 2000 + $result[0];
00261 }
00262
00263 return $result;
00264 }
00265 }