PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Date.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 288 lines | 153 code | 35 blank | 100 comment | 25 complexity | 1f7628558283dc16fc637116fc3df677 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Date conversion model
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. class Mage_Core_Model_Date
  32. {
  33. /**
  34. * Current config offset in seconds
  35. *
  36. * @var int
  37. */
  38. private $_offset = 0;
  39. /**
  40. * Current system offset in seconds
  41. *
  42. * @var int
  43. */
  44. private $_systemOffset = 0;
  45. /**
  46. * Init offset
  47. *
  48. */
  49. public function __construct()
  50. {
  51. $this->_offset = $this->calculateOffset($this->_getConfigTimezone());
  52. $this->_systemOffset = $this->calculateOffset();
  53. }
  54. /**
  55. * Gets the store config timezone
  56. *
  57. * @return string
  58. */
  59. protected function _getConfigTimezone()
  60. {
  61. return Mage::app()->getStore()->getConfig('general/locale/timezone');
  62. }
  63. /**
  64. * Calculates timezone offset
  65. *
  66. * @param string $timezone
  67. * @return int offset between timezone and gmt
  68. */
  69. public function calculateOffset($timezone = null)
  70. {
  71. $result = true;
  72. $offset = 0;
  73. if (!is_null($timezone)){
  74. $oldzone = @date_default_timezone_get();
  75. $result = date_default_timezone_set($timezone);
  76. }
  77. if ($result === true) {
  78. $offset = (int)date('Z');
  79. }
  80. if (!is_null($timezone)){
  81. date_default_timezone_set($oldzone);
  82. }
  83. return $offset;
  84. }
  85. /**
  86. * Forms GMT date
  87. *
  88. * @param string $format
  89. * @param int|string $input date in current timezone
  90. * @return string
  91. */
  92. public function gmtDate($format = null, $input = null)
  93. {
  94. if (is_null($format)) {
  95. $format = 'Y-m-d H:i:s';
  96. }
  97. $date = $this->gmtTimestamp($input);
  98. if ($date === false) {
  99. return false;
  100. }
  101. $result = date($format, $date);
  102. return $result;
  103. }
  104. /**
  105. * Converts input date into date with timezone offset
  106. * Input date must be in GMT timezone
  107. *
  108. * @param string $format
  109. * @param int|string $input date in GMT timezone
  110. * @return string
  111. */
  112. public function date($format = null, $input = null)
  113. {
  114. if (is_null($format)) {
  115. $format = 'Y-m-d H:i:s';
  116. }
  117. $result = date($format, $this->timestamp($input));
  118. return $result;
  119. }
  120. /**
  121. * Forms GMT timestamp
  122. *
  123. * @param int|string $input date in current timezone
  124. * @return int
  125. */
  126. public function gmtTimestamp($input = null)
  127. {
  128. if (is_null($input)) {
  129. return gmdate('U');
  130. } else if (is_numeric($input)) {
  131. $result = $input;
  132. } else {
  133. $result = strtotime($input);
  134. }
  135. if ($result === false) {
  136. // strtotime() unable to parse string (it's not a date or has incorrect format)
  137. return false;
  138. }
  139. $date = Mage::app()->getLocale()->date($result);
  140. $timestamp = $date->get(Zend_Date::TIMESTAMP) - $date->get(Zend_Date::TIMEZONE_SECS);
  141. unset($date);
  142. return $timestamp;
  143. }
  144. /**
  145. * Converts input date into timestamp with timezone offset
  146. * Input date must be in GMT timezone
  147. *
  148. * @param int|string $input date in GMT timezone
  149. * @return int
  150. */
  151. public function timestamp($input = null)
  152. {
  153. if (is_null($input)) {
  154. $result = $this->gmtTimestamp();
  155. } else if (is_numeric($input)) {
  156. $result = $input;
  157. } else {
  158. $result = strtotime($input);
  159. }
  160. $date = Mage::app()->getLocale()->date($result);
  161. $timestamp = $date->get(Zend_Date::TIMESTAMP) + $date->get(Zend_Date::TIMEZONE_SECS);
  162. unset($date);
  163. return $timestamp;
  164. }
  165. /**
  166. * Get current timezone offset in seconds/minutes/hours
  167. *
  168. * @param string $type
  169. * @return int
  170. */
  171. public function getGmtOffset($type = 'seconds')
  172. {
  173. $result = $this->_offset;
  174. switch ($type) {
  175. case 'seconds':
  176. default:
  177. break;
  178. case 'minutes':
  179. $result = $result / 60;
  180. break;
  181. case 'hours':
  182. $result = $result / 60 / 60;
  183. break;
  184. }
  185. return $result;
  186. }
  187. /**
  188. * Deprecated since 1.1.7
  189. */
  190. public function checkDateTime($year, $month, $day, $hour = 0, $minute = 0, $second = 0)
  191. {
  192. if (!checkdate($month, $day, $year)) {
  193. return false;
  194. }
  195. foreach (array('hour' => 23, 'minute' => 59, 'second' => 59) as $var => $maxValue) {
  196. $value = (int)$$var;
  197. if (($value < 0) || ($value > $maxValue)) {
  198. return false;
  199. }
  200. }
  201. return true;
  202. }
  203. /**
  204. * Deprecated since 1.1.7
  205. */
  206. public function parseDateTime($dateTimeString, $dateTimeFormat)
  207. {
  208. // look for supported format
  209. $isSupportedFormatFound = false;
  210. $formats = array(
  211. // priority is important!
  212. '%m/%d/%y %I:%M' => array(
  213. '/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2})/',
  214. array('y' => 3, 'm' => 1, 'd' => 2, 'h' => 4, 'i' => 5)
  215. ),
  216. 'm/d/y h:i' => array(
  217. '/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2})/',
  218. array('y' => 3, 'm' => 1, 'd' => 2, 'h' => 4, 'i' => 5)
  219. ),
  220. '%m/%d/%y' => array('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/', array('y' => 3, 'm' => 1, 'd' => 2)),
  221. 'm/d/y' => array('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/', array('y' => 3, 'm' => 1, 'd' => 2)),
  222. );
  223. foreach ($formats as $supportedFormat => $regRule) {
  224. if (false !== strpos($dateTimeFormat, $supportedFormat, 0)) {
  225. $isSupportedFormatFound = true;
  226. break;
  227. }
  228. }
  229. if (!$isSupportedFormatFound) {
  230. Mage::throwException(Mage::helper('core')->__('Date/time format "%s" is not supported.', $dateTimeFormat));
  231. }
  232. // apply reg rule to found format
  233. $regex = array_shift($regRule);
  234. $mask = array_shift($regRule);
  235. if (!preg_match($regex, $dateTimeString, $matches)) {
  236. Mage::throwException(Mage::helper('core')->__('Specified date/time "%1$s" do not match format "%2$s".', $dateTimeString, $dateTimeFormat));
  237. }
  238. // make result
  239. $result = array();
  240. foreach (array('y', 'm', 'd', 'h', 'i', 's') as $key) {
  241. $value = 0;
  242. if (isset($mask[$key]) && isset($matches[$mask[$key]])) {
  243. $value = (int)$matches[$mask[$key]];
  244. }
  245. $result[] = $value;
  246. }
  247. // make sure to return full year
  248. if ($result[0] < 100) {
  249. $result[0] = 2000 + $result[0];
  250. }
  251. return $result;
  252. }
  253. }