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

/system/expressionengine/libraries/Localize.php

https://bitbucket.org/mbaily/tremain
PHP | 1159 lines | 578 code | 181 blank | 400 comment | 88 complexity | 5c5de4d324d38f6865d8ceb67a507053 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2013, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Core Localization Class
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Core
  19. * @category Core
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Localize {
  24. public $now = ''; // Local server time as GMT accounting for server offset
  25. public $format = array(
  26. 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q',
  27. 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
  28. 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%Q',
  29. 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
  30. 'DATE_RFC850' => '%l, %d-%M-%y %H:%m:%i UTC',
  31. 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
  32. 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
  33. 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
  34. 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
  35. 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q'
  36. );
  37. // Cached timezone and country data, properties not to be accessed directly
  38. private $_countries = array();
  39. private $_timezones_by_country = array();
  40. /**
  41. * Constructor
  42. */
  43. function __construct()
  44. {
  45. // Make a local reference to the ExpressionEngine super object
  46. $this->EE =& get_instance();
  47. // Fetch current Unix timestamp
  48. $this->now = time();
  49. // Apply server offset (in minutes) to $now
  50. if (($offset = ee()->config->item('server_offset'))
  51. && is_numeric($offset))
  52. {
  53. $this->now += $offset * 60;
  54. }
  55. }
  56. // --------------------------------------------------------------------
  57. /**
  58. * String to Timestamp
  59. *
  60. * Converts a human-readable date (and possibly time) to a Unix timestamp
  61. * using the current member's locale
  62. *
  63. * @param string Human-readable date
  64. * @param bool Is the human date prelocalized?
  65. * @return mixed int if successful, otherwise FALSE
  66. */
  67. public function string_to_timestamp($human_string, $localized = TRUE)
  68. {
  69. if (trim($human_string) == '')
  70. {
  71. return '';
  72. }
  73. $dt = $this->_datetime($human_string, $localized);
  74. return ($dt) ? $dt->format('U') : FALSE;
  75. }
  76. // --------------------------------------------------------------------
  77. /**
  78. * Given an EE date format and a Unix timestamp, returns the human-readable
  79. * date in the specified timezone or member's current timezone.
  80. *
  81. * @param string Date format, like "%D, %F %d, %Y - %g:%i:%s"
  82. * @param int Unix timestamp
  83. * @param bool Return date localized or not
  84. * @return string Formatted date
  85. */
  86. public function format_date($format, $timestamp = NULL, $localize = TRUE)
  87. {
  88. if ( ! ($dt = $this->_datetime($timestamp, $localize)))
  89. {
  90. return FALSE;
  91. }
  92. // Match all EE date vars, which are essentially the normal PHP date
  93. // vars with a percent sign in front of them
  94. if ( ! preg_match_all("/(%\S)/", $format, $matches))
  95. {
  96. return $dt->format('U');
  97. }
  98. // Loop through matched date vars and replace them in the $format string
  99. foreach($matches[1] as $var)
  100. {
  101. $format = str_replace($var, $this->_date_string_for_variable($var, $dt), $format);
  102. }
  103. return $format;
  104. }
  105. // --------------------------------------------------------------------
  106. /**
  107. * Given an date variable and a DateTime object, returns the associated
  108. * formatting for the date variable and DateTime object
  109. *
  110. * @param string Date variable with percent sign prefix, like "%D"
  111. * @param datetime DateTime object on which to call format()
  112. * @return string Value of variable in DateTime object, translated
  113. */
  114. private function _date_string_for_variable($var, $dt)
  115. {
  116. // These letters following a percent sign we will convert to their
  117. // matching PHP date variable value
  118. $allowed_date_vars = array(
  119. 'a', 'A', 'B', 'd', 'D', 'F', 'g', 'G', 'h', 'H', 'i', 'I',
  120. 'j', 'l', 'L', 'm', 'M', 'n', 'O', 'P', 'Q', 'r', 's', 'S',
  121. 't', 'T', 'U', 'w', 'W', 'y', 'Y', 'z', 'Z'
  122. );
  123. // These date variables have month or day names and need to be ran
  124. // through the language library
  125. $translatable_date_vars = array(
  126. 'a', 'A', 'D', 'F', 'l', 'M', 'r', 'S'
  127. );
  128. // If TRUE, the translatable date variables will be run through the
  129. // language library; this check has been brought over from legacy code
  130. $translate = ! (isset(ee()->TMPL)
  131. && is_object(ee()->TMPL)
  132. && ee()->TMPL->template_type == 'feed');
  133. // Remove percent sign for easy comparing and passing to DateTime::format
  134. $date_var = str_replace('%', '', $var);
  135. if (in_array($date_var, $allowed_date_vars))
  136. {
  137. // Special cases
  138. switch ($date_var)
  139. {
  140. // F returns the full month name, but "May" is the same short
  141. // and long, so we need to catch it and modify the lang key so
  142. // the correct translation is returned
  143. case 'F':
  144. if ($dt->format('F') == 'May' && $translate)
  145. {
  146. return lang('May_l');
  147. }
  148. break;
  149. // Concatenate the RFC 2822 format with translations
  150. case 'r':
  151. if ($translate)
  152. {
  153. $rfc = lang($dt->format('D')); // Thu
  154. $rfc .= $dt->format(', d '); // , 21
  155. $rfc .= lang($dt->format('M')); // Dec
  156. $rfc .= $dt->format(' Y H:i:s O'); // 2000 16:01:07 +0200
  157. return $rfc;
  158. }
  159. break;
  160. // Q was our replacement for P because P wasn't available < PHP 5.1.3,
  161. // so keep it around for backwards compatability
  162. case 'Q':
  163. $date_var = 'P';
  164. break;
  165. }
  166. // If it's translatable, return the value for the lang key,
  167. // otherwise send it straight to DateTime::format
  168. if ($translate && in_array($date_var, $translatable_date_vars))
  169. {
  170. return lang($dt->format($date_var));
  171. }
  172. else
  173. {
  174. return $dt->format($date_var);
  175. }
  176. }
  177. return $var;
  178. }
  179. // --------------------------------------------------------------------
  180. /**
  181. * Provides common date format for things like date fields, and takes
  182. * into consideration the member's time_format preference
  183. *
  184. * Example: 2015-10-21 06:30 PM
  185. *
  186. * @param int Unix timestamp
  187. * @param bool Localize to member's timezone or leave as GMT
  188. * @param bool Include seconds in returned string or not
  189. * @return string Formatted string
  190. */
  191. public function human_time($timestamp = NULL, $localize = TRUE, $seconds = FALSE)
  192. {
  193. /* -------------------------------------------
  194. /* Hidden Configuration Variables
  195. /* - include_seconds => Determines whether to include seconds in our human time.
  196. /* -------------------------------------------*/
  197. if (func_num_args() != 3 && ee()->config->item('include_seconds') == 'y')
  198. {
  199. $seconds = TRUE;
  200. }
  201. $fmt = (ee()->session->userdata('time_format') != '')
  202. ? ee()->session->userdata('time_format') : ee()->config->item('time_format');
  203. // 2015-10-21
  204. $format_string = '%Y-%m-%d ';
  205. // 06:30 or 18:30
  206. $format_string .= ($fmt == 'us') ? '%h:%i' : '%H:%i';
  207. // Seconds
  208. if ($seconds)
  209. {
  210. $format_string .= ':%s';
  211. }
  212. // AM/PM
  213. if ($fmt == 'us')
  214. {
  215. $format_string .= ' %A';
  216. }
  217. return $this->format_date($format_string, $timestamp, $localize);
  218. }
  219. // --------------------------------------------------------------------
  220. /**
  221. * Returns a DateTime object for the current time and member timezone
  222. * OR a specified time and timezone
  223. *
  224. * @param string Date string or Unix timestamp, current time used if NULL
  225. * @param mixed Bool: whether or not to localize to current member's
  226. * timezone, or string of timezone to convert to
  227. * @return datetime DateTime object set to the given time and altered
  228. * for server offset
  229. */
  230. private function _datetime($date_string = NULL, $timezone = TRUE)
  231. {
  232. // Localize to member's timezone or leave as GMT
  233. if (is_bool($timezone))
  234. {
  235. $timezone = ($timezone) ? ee()->session->userdata('timezone') : 'UTC';
  236. }
  237. // If timezone isn't known by PHP, it may be our legacy timezone
  238. // notation and needs to be converted
  239. if ( ! in_array($timezone, DateTimeZone::listIdentifiers()))
  240. {
  241. $timezone = $this->get_php_timezone($timezone);
  242. }
  243. try
  244. {
  245. $timezone = new DateTimeZone($timezone);
  246. // If $date_string appears to be a Unix timestamp, prepend the
  247. // string with '@' so DateTime knows it's a timestamp; the
  248. // timezone parameter is ignored when a timestamp is passed,
  249. // so set it separately after instantiation
  250. if (is_numeric($date_string))
  251. {
  252. $dt = new DateTime('@'.$date_string);
  253. $dt->setTimezone($timezone);
  254. }
  255. // Otherwise, we must instantiate the DateTime object with the
  256. // correct DateTimeZone so that the date string passed in is
  257. // immediately interpreted using the member's timezone and not
  258. // the server timezone; otherwise, using setTimezone to set
  259. // the timezone later will transform the date
  260. else
  261. {
  262. $dt = new DateTime($date_string, $timezone);
  263. }
  264. }
  265. catch (Exception $e)
  266. {
  267. return FALSE;
  268. }
  269. // Apply server offset only
  270. if (empty($date_string)
  271. && ($offset = ee()->config->item('server_offset'))
  272. && is_numeric($offset))
  273. {
  274. $offset = ($offset > 0) ? '+'.$offset : $offset;
  275. $dt->modify($offset.' minutes');
  276. }
  277. return $dt;
  278. }
  279. // --------------------------------------------------------------------
  280. /**
  281. * Generates an HTML menu of timezones
  282. *
  283. * @param string Default timezone selection
  284. * @param string Name of dropdown form field element
  285. * @return string HTML for dropdown list
  286. */
  287. public function timezone_menu($default = NULL, $name = 'server_timezone')
  288. {
  289. // For the installer
  290. ee()->load->helper('language');
  291. // We only want timezones with these prefixes
  292. $continents = array('Africa', 'America', 'Antarctica', 'Arctic',
  293. 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific');
  294. $zones_by_country = $this->_get_timezones_by_country();
  295. $countries = $this->_get_countries();
  296. $timezones = array();
  297. foreach ($countries as $code => $country)
  298. {
  299. // If country code does not match any timezones, skip the loop
  300. if ( ! isset($zones_by_country[$code]))
  301. {
  302. continue;
  303. }
  304. // We'll store timezones for the current country here
  305. $local_zones = array();
  306. foreach ($zones_by_country[$code] as $zone)
  307. {
  308. // Explode ID by slashes while replacing underscores with spaces
  309. $zone_array = str_replace('_', ' ', explode('/', $zone));
  310. // Exclude deprecated PHP timezones
  311. if ( ! in_array($zone_array[0], $continents))
  312. {
  313. continue;
  314. }
  315. // Construct the localized zone name
  316. if (isset($zone_array[1]))
  317. {
  318. $zone_name = lang($zone_array[1]);
  319. if (isset($zone_array[2]))
  320. {
  321. $zone_name .= ' - ' . lang($zone_array[2]);
  322. }
  323. $local_zones[$zone] = $zone_name;
  324. }
  325. }
  326. // Sort timezones by their new names
  327. asort($local_zones);
  328. $timezones[$code] = $local_zones;
  329. }
  330. // Convert to JSON for fast switching of timezone dropdown
  331. $timezone_json = json_encode($timezones);
  332. $no_timezones_lang = lang('no_timezones');
  333. // Start the output with some javascript to handle the timezone
  334. // dropdown population based on the country dropdown
  335. $output = <<<EOF
  336. <script type="text/javascript">
  337. var timezones = $timezone_json
  338. function ee_tz_change(countryselect)
  339. {
  340. var timezoneselect = document.getElementById('timezone_select');
  341. var countrycode = countryselect.options[countryselect.selectedIndex].value;
  342. timezoneselect.options.length = 0;
  343. if (timezones[countrycode] == '' || timezones[countrycode] == undefined)
  344. {
  345. timezoneselect.add(new Option('$no_timezones_lang', ''));
  346. return;
  347. }
  348. for (var key in timezones[countrycode])
  349. {
  350. if (timezones[countrycode].hasOwnProperty(key))
  351. {
  352. timezoneselect.add(new Option(timezones[countrycode][key], key));
  353. }
  354. }
  355. }
  356. </script>
  357. EOF;
  358. // Prepend to the top of countries dropdown with common country selections
  359. $countries = array_merge(
  360. array(
  361. lang('select_timezone'),
  362. '-------------',
  363. 'us' => $countries['us'], // United States
  364. 'gb' => $countries['gb'], // United Kingdom
  365. 'au' => $countries['au'], // Australia
  366. 'ca' => $countries['ca'], // Canada
  367. 'fr' => $countries['fr'], // France
  368. 'ie' => $countries['ie'], // Ireland
  369. 'nz' => $countries['nz'], // New Zealand
  370. '-------------'
  371. ),
  372. $countries
  373. );
  374. // Get ready to load preselected values into the dropdowns if one exists
  375. $selected_country = NULL;
  376. $timezone_prepoplated = array('' => lang('no_timezones'));
  377. if ( ! empty($default))
  378. {
  379. $timezone_ids = DateTimeZone::listIdentifiers();
  380. // If default selection isn't valid, it may be our legacy timezone format
  381. if ( ! in_array($default, $timezone_ids))
  382. {
  383. $default = $this->get_php_timezone($default);
  384. }
  385. $selected_country = $this->_get_country_for_php_timezone($default);
  386. // Preselect timezone if we got a valid country back
  387. if ($selected_country)
  388. {
  389. $timezone_prepoplated = $timezones[$selected_country];
  390. }
  391. }
  392. // Construct the form
  393. ee()->load->helper('form');
  394. $output .= form_dropdown('tz_country', $countries, $selected_country, 'onchange="ee_tz_change(this)"');
  395. $output .= '&nbsp;&nbsp;'; // NBS constant doesn't work in installer
  396. $output .= form_dropdown($name, $timezone_prepoplated, $default, 'id="timezone_select"');
  397. return $output;
  398. }
  399. // --------------------------------------------------------------------
  400. /**
  401. * Loads countries config file and creates localized array of country
  402. * codes corresponding to country names
  403. *
  404. * @access private
  405. * @param boolean Whether or not to return timezones mapped to
  406. * countries instead
  407. * @return string
  408. */
  409. private function _get_countries($return_timezones = FALSE)
  410. {
  411. if ( ! empty($this->_countries) AND ! $return_timezones)
  412. {
  413. return $this->_countries;
  414. }
  415. $countries_path = APPPATH.'config/countries.php';
  416. if ( ! file_exists($countries_path))
  417. {
  418. $countries_path = EE_APPPATH.'config/countries.php';
  419. }
  420. if ( ! include($countries_path))
  421. {
  422. show_error(lang('countryfile_missing'));
  423. }
  424. if ($return_timezones)
  425. {
  426. return $timezones;
  427. }
  428. foreach ($countries as $code => $country)
  429. {
  430. $countries[$code] = lang($country);
  431. }
  432. $this->_countries = $countries;
  433. return $this->_countries;
  434. }
  435. // --------------------------------------------------------------------
  436. /**
  437. * Creates and returns a cached array of timezones by country.
  438. *
  439. * @access private
  440. * @return array Array of timezones by country code
  441. */
  442. private function _get_timezones_by_country()
  443. {
  444. if ( ! empty($this->_timezones_by_country))
  445. {
  446. return $this->_timezones_by_country;
  447. }
  448. // PHP 5.3+
  449. if (defined('DateTimeZone::PER_COUNTRY'))
  450. {
  451. foreach ($this->_get_countries() as $code => $country)
  452. {
  453. $this->_timezones_by_country[$code] = DateTimeZone::listIdentifiers(
  454. DateTimeZone::PER_COUNTRY, strtoupper($code)
  455. );
  456. }
  457. }
  458. // < PHP 5.3
  459. else
  460. {
  461. $this->_timezones_by_country = $this->_get_countries(TRUE);
  462. }
  463. return $this->_timezones_by_country;
  464. }
  465. // --------------------------------------------------------------------
  466. /**
  467. * Returns the country code for a given PHP timezone
  468. *
  469. * @access private
  470. * @param string PHP timezone
  471. * @return string Two-letter country code for timezone
  472. */
  473. private function _get_country_for_php_timezone($timezone)
  474. {
  475. foreach ($this->_get_timezones_by_country() as $code => $timezones)
  476. {
  477. if (in_array($timezone, $timezones))
  478. {
  479. return $code;
  480. }
  481. }
  482. return FALSE;
  483. }
  484. // --------------------------------------------------------------------
  485. /**
  486. * Gets the PHP timezone for the legacy timezone format EE used to
  487. * store timezones with which was based on offsets; for example, given
  488. * "UM5", it returns "America/New_York"
  489. *
  490. * @access public
  491. * @param string
  492. * @return string
  493. */
  494. public function get_php_timezone($zone = 'UTC')
  495. {
  496. $zones = array(
  497. 'UM12' => 'Kwajalein', // -12
  498. 'UM11' => 'Pacific/Midway', // -11
  499. 'UM10' => 'Pacific/Honolulu', // -10
  500. 'UM95' => 'Pacific/Marquesas', // -9.5
  501. 'UM9' => 'America/Anchorage', // -9
  502. 'UM8' => 'America/Los_Angeles', // -8
  503. 'UM7' => 'America/Denver', // -7
  504. 'UM6' => 'America/Chicago', // -6
  505. 'UM5' => 'America/New_York', // -5
  506. 'UM45' => 'America/Caracas', // -4.5
  507. 'UM4' => 'America/Halifax', // -4
  508. 'UM35' => 'America/St_Johns', // -3.5
  509. 'UM3' => 'America/Argentina/Buenos_Aires',// -3
  510. 'UM2' => 'Atlantic/South_Georgia', // -2
  511. 'UM1' => 'Atlantic/Azores', // -1
  512. 'UTC' => 'Europe/Dublin', // 0
  513. 'UP1' => 'Europe/Belgrade', // +1
  514. 'UP2' => 'Europe/Minsk', // +2
  515. 'UP3' => 'Asia/Kuwait', // +3
  516. 'UP35' => 'Asia/Tehran', // +3.5
  517. 'UP4' => 'Asia/Muscat', // +4
  518. 'UP45' => 'Asia/Kabul', // +4.5
  519. 'UP5' => 'Asia/Yekaterinburg', // +5
  520. 'UP55' => 'Asia/Kolkata', // +5.5
  521. 'UP575' => 'Asia/Katmandu', // +5.75
  522. 'UP6' => 'Asia/Dhaka', // +6
  523. 'UP65' => 'Asia/Rangoon', // +6.5
  524. 'UP7' => 'Asia/Krasnoyarsk', // +7
  525. 'UP8' => 'Asia/Brunei', // 8
  526. 'UP875' => 'Australia/Eucla', // +8.75
  527. 'UP9' => 'Asia/Seoul', // +9
  528. 'UP95' => 'Australia/Darwin', // +9.5
  529. 'UP10' => 'Australia/Canberra', // +10
  530. 'UP105' => 'Australia/Lord_Howe', // +10.5
  531. 'UP11' => 'Asia/Magadan', // +11
  532. 'UP115' => 'Pacific/Norfolk', // +11.5
  533. 'UP12' => 'Pacific/Fiji', // +12
  534. 'UP1275' => 'Pacific/Chatham', // +12.75
  535. 'UP13' => 'Pacific/Tongatapu', // +13
  536. 'UP14' => 'Pacific/Kiritimati' // +14
  537. );
  538. // Fall back to UTC if something went wrong
  539. if ( ! isset($zones[$zone]))
  540. {
  541. return 'UTC';
  542. }
  543. return $zones[$zone];
  544. }
  545. // --------------------------------------------------------------------
  546. /**
  547. * Localize month name
  548. *
  549. * Helper function used to translate month names.
  550. *
  551. * @access public
  552. * @param string
  553. * @return string
  554. */
  555. function localize_month($month = '')
  556. {
  557. $months = array(
  558. '01' => array('Jan', 'January'),
  559. '02' => array('Feb', 'February'),
  560. '03' => array('Mar', 'March'),
  561. '04' => array('Apr', 'April'),
  562. '05' => array('May', 'May_l'),
  563. '06' => array('Jun', 'June'),
  564. '07' => array('Jul', 'July'),
  565. '08' => array('Aug', 'August'),
  566. '09' => array('Sep', 'September'),
  567. '10' => array('Oct', 'October'),
  568. '11' => array('Nov', 'November'),
  569. '12' => array('Dec', 'December')
  570. );
  571. if (isset($months[$month]))
  572. {
  573. return $months[$month];
  574. }
  575. }
  576. // --------------------------------------------------------------------
  577. // Everything below this line has been deprecated.
  578. // --------------------------------------------------------------------
  579. // --------------------------------------------------------------------
  580. /**
  581. * Convert a MySQL timestamp to GMT
  582. *
  583. * @access public
  584. * @param string
  585. * @return string
  586. */
  587. public function timestamp_to_gmt($str = '')
  588. {
  589. ee()->load->library('logger');
  590. ee()->logger->deprecated('2.6', 'Date helper\'s mysql_to_unix()');
  591. ee()->load->helper('date');
  592. return mysql_to_unix($str);
  593. }
  594. // --------------------------------------------------------------------
  595. /**
  596. * Set localized time
  597. *
  598. * Converts GMT time to the localized values of the current logged-in user
  599. *
  600. * @access public
  601. * @param string
  602. * @param string
  603. * @param string
  604. * @return string
  605. */
  606. function set_localized_time($now = '', $timezone = '', $dst = '')
  607. {
  608. ee()->load->library('logger');
  609. ee()->logger->deprecated('2.6');
  610. ee()->load->helper('date');
  611. $zones = timezones();
  612. if ($now == '')
  613. {
  614. $now = $this->now;
  615. }
  616. // This lets us use a different timezone then the logged in user to calculate a time.
  617. // Right now we only use this to show the local time of other users
  618. if ($timezone == '')
  619. {
  620. $timezone = ee()->session->userdata['timezone'];
  621. }
  622. // If the current user has not set localization preferences
  623. // we'll instead use the master server settings
  624. if ($timezone == '')
  625. {
  626. return $this->set_server_time($now);
  627. }
  628. $now += $zones[$timezone] * 3600;
  629. if ($dst == '')
  630. {
  631. $dst = ee()->session->userdata('daylight_savings');
  632. }
  633. if ($dst == 'y')
  634. {
  635. $now += 3600;
  636. }
  637. return $this->set_server_offset($now);
  638. }
  639. // --------------------------------------------------------------------
  640. /**
  641. * Set localized server time
  642. *
  643. * Converts GMT time to the localized server timezone
  644. *
  645. * @access public
  646. * @param string
  647. * @return string
  648. */
  649. function set_server_time($now = '')
  650. {
  651. ee()->load->library('logger');
  652. ee()->logger->deprecated('2.6');
  653. ee()->load->helper('date');
  654. $zones = timezones();
  655. if ($now == '')
  656. {
  657. $now = $this->now;
  658. }
  659. if ($tz = ee()->config->item('server_timezone'))
  660. {
  661. $now += $zones[$tz] * 3600;
  662. }
  663. if (ee()->config->item('daylight_savings') == 'y')
  664. {
  665. $now += 3600;
  666. }
  667. $now = $this->set_server_offset($now);
  668. return $now;
  669. }
  670. // --------------------------------------------------------------------
  671. /**
  672. * Set server offset
  673. *
  674. * Takes a Unix timestamp as input and adds/subtracts the number of
  675. * minutes specified in the master server time offset preference
  676. *
  677. * The optional second parameter lets us reverse the offset (positive number becomes negative)
  678. * We use the second parameter with set_localized_offset()
  679. *
  680. * @access public
  681. * @param string
  682. * @return string
  683. */
  684. function set_server_offset($time, $reverse = 0)
  685. {
  686. ee()->load->library('logger');
  687. ee()->logger->deprecated('2.6');
  688. $offset = ( ! ee()->config->item('server_offset')) ? 0 : ee()->config->item('server_offset') * 60;
  689. if ($offset == 0)
  690. {
  691. return $time;
  692. }
  693. if ($reverse == 1)
  694. {
  695. $offset = $offset * -1;
  696. }
  697. $time += $offset;
  698. return $time;
  699. }
  700. // --------------------------------------------------------------------
  701. /**
  702. * Set localized offset
  703. *
  704. * This function lets us calculate the time difference between the
  705. * timezone of the current user and the timezone of the server hosting
  706. * the site. It solves a dilemma we face when using functions like mktime()
  707. * which base their output on the server's timezone. When a channel entry is
  708. * submitted, the entry date is converted to a Unix timestamp. But since
  709. * the user submitting the entry might not be in the same timezone as the
  710. * server we need to offset the timestamp to reflect this difference.
  711. *
  712. * @access public
  713. * @return void
  714. */
  715. function set_localized_offset()
  716. {
  717. ee()->load->library('logger');
  718. ee()->logger->deprecated('2.6');
  719. $offset = 0;
  720. ee()->load->helper('date');
  721. $zones = timezones();
  722. if (ee()->session->userdata['timezone'] == '')
  723. {
  724. if ($tz = ee()->config->item('server_timezone'))
  725. {
  726. $offset += $zones[$tz];
  727. }
  728. if (ee()->config->item('daylight_savings') == 'y')
  729. {
  730. $offset += 1;
  731. }
  732. }
  733. else
  734. {
  735. $offset += $zones[ee()->session->userdata['timezone']];
  736. if (ee()->session->userdata['daylight_savings'] == 'y')
  737. {
  738. $offset += 1;
  739. }
  740. }
  741. // Offset this number based on the server offset (if it exists)
  742. $time = $this->set_server_offset(0, 1);
  743. // Divide by 3600, making our offset into hours
  744. $time = $time/3600;
  745. // add or subtract it from our timezone offset
  746. $offset -= $time;
  747. // Multiply by -1 to invert the value (positive becomes negative and vice versa)
  748. $offset = $offset * -1;
  749. // Convert it to seconds
  750. if ($offset != 0)
  751. {
  752. $offset = $offset * (60 * 60);
  753. }
  754. return $offset;
  755. }
  756. // --------------------------------------------------------------------
  757. /**
  758. * Human-readable time
  759. *
  760. * Formats Unix/GMT timestamp to the following format: 2003-08-21 11:35 PM
  761. *
  762. * Will also switch to Euro time based on the user preference
  763. *
  764. * @access public
  765. * @param string
  766. * @param bool
  767. * @param bool
  768. * @return string
  769. */
  770. function set_human_time($now = '', $localize = TRUE, $seconds = FALSE)
  771. {
  772. ee()->load->library('logger');
  773. ee()->logger->deprecated('2.6', 'Localize::human_time');
  774. return $this->human_time($now, $localize, $seconds);
  775. }
  776. // --------------------------------------------------------------------
  777. /**
  778. * Convert "human" date to GMT
  779. *
  780. * Converts the human-readable date used in the channel entry
  781. * submission page back to Unix/GMT
  782. *
  783. * @access public
  784. * @param string
  785. * @return int
  786. */
  787. function convert_human_date_to_gmt($datestr = '')
  788. {
  789. ee()->load->library('logger');
  790. ee()->logger->deprecated('2.6', 'Localize::string_to_timestamp');
  791. return $this->string_to_timestamp($datestr);
  792. }
  793. // --------------------------------------------------------------------
  794. /**
  795. * Simple Offset
  796. *
  797. * This allows a timestamp to be offset by the submitted timezone.
  798. * Currently this is only used in the PUBLISH page
  799. *
  800. * @access public
  801. * @param string
  802. * @param string
  803. * @return int
  804. */
  805. function simpl_offset($time = '', $timezone = '')
  806. {
  807. ee()->load->library('logger');
  808. ee()->logger->deprecated('2.6');
  809. return $time;
  810. }
  811. // --------------------------------------------------------------------
  812. /**
  813. * Format timespan
  814. *
  815. * Returns a span of seconds in this format: 10 days 14 hours 36 minutes 47 seconds
  816. *
  817. * @access public
  818. * @param string
  819. * @return string
  820. */
  821. function format_timespan($seconds = '')
  822. {
  823. ee()->load->library('logger');
  824. ee()->logger->deprecated('2.6', 'Date helper\'s timespan()');
  825. ee()->load->helper('date');
  826. return timespan($seconds);
  827. }
  828. // --------------------------------------------------------------------
  829. /**
  830. * Fetch Date Params (via template parser)
  831. *
  832. * @access public
  833. * @param string
  834. * @return string
  835. */
  836. function fetch_date_params($datestr = '')
  837. {
  838. ee()->load->library('logger');
  839. ee()->logger->deprecated('2.6');
  840. if ($datestr == '')
  841. return;
  842. if ( ! preg_match_all("/(%\S)/", $datestr, $matches))
  843. return;
  844. return $matches[1];
  845. }
  846. // --------------------------------------------------------------------
  847. /**
  848. * Decode date string (via template parser)
  849. *
  850. * This function takes a string containing text and
  851. * date codes and extracts only the codes. Then,
  852. * the codes are converted to their actual timestamp
  853. * values and the string is reassembled.
  854. *
  855. * @access public
  856. * @param string
  857. * @param string
  858. * @param bool
  859. * @return string
  860. */
  861. function decode_date($datestr = '', $unixtime = '', $localize = TRUE)
  862. {
  863. ee()->load->library('logger');
  864. ee()->logger->deprecated('2.6', 'Localize::format_date');
  865. return $this->format_date($datestr, $unixtime, $localize);
  866. }
  867. // --------------------------------------------------------------------
  868. /**
  869. * Convert timestamp codes
  870. *
  871. * All text codes are converted to the user-specified language.
  872. *
  873. * @access public
  874. * @param string
  875. * @param string
  876. * @param bool
  877. * @return mixed
  878. */
  879. function convert_timestamp($format = '', $time = '', $localize = TRUE, $prelocalized = FALSE)
  880. {
  881. ee()->load->library('logger');
  882. ee()->logger->deprecated('2.6', 'Localize::format_date');
  883. $return_array = FALSE;
  884. if (is_array($format) && isset($format[0]))
  885. {
  886. $return_array = TRUE;
  887. $format = $format[0];
  888. }
  889. $format = $this->format_date($format, $time, $localize);
  890. return ($return_array) ? array($format) : $format;
  891. }
  892. // --------------------------------------------------------------------
  893. /**
  894. * GMT Offset - Ouputs: +01:00
  895. *
  896. * @access public
  897. * @param string
  898. * @return int
  899. */
  900. function zone_offset($tz = '')
  901. {
  902. ee()->load->library('logger');
  903. ee()->logger->deprecated('2.6');
  904. return $tz;
  905. }
  906. // --------------------------------------------------------------------
  907. /**
  908. * Timezones
  909. *
  910. * This array is used to render the localization pull-down menu
  911. *
  912. * @access public
  913. * @return array
  914. */
  915. function zones()
  916. {
  917. ee()->load->library('logger');
  918. ee()->logger->deprecated('2.6', 'Date helper\'s timezones()');
  919. ee()->load->helper('date');
  920. return timezones();
  921. }
  922. // --------------------------------------------------------------------
  923. /**
  924. * Set localized timezone
  925. *
  926. * @access public
  927. * @return string
  928. */
  929. function set_localized_timezone()
  930. {
  931. ee()->load->library('logger');
  932. ee()->logger->deprecated('2.6');
  933. return 'GMT';
  934. }
  935. // --------------------------------------------------------------------
  936. /**
  937. * Fetch Days in Month
  938. *
  939. * Returns the number of days for the given month/year
  940. * Takes leap years into consideration
  941. *
  942. * @access public
  943. * @param string
  944. * @param string
  945. * @return int
  946. */
  947. function fetch_days_in_month($month, $year)
  948. {
  949. ee()->load->library('logger');
  950. ee()->logger->deprecated('2.6', 'Date helper\'s days_in_month()');
  951. ee()->load->helper('date');
  952. return days_in_month($month, $year);
  953. }
  954. // --------------------------------------------------------------------
  955. /**
  956. * Adjust Date
  957. *
  958. * This function is used by the calendar. It verifies that
  959. * the month/day are within the correct range and adjusts
  960. * if necessary. For example: Day 34 in Feburary would
  961. * be adjusted to March 6th.
  962. *
  963. * @access public
  964. * @param string
  965. * @param string
  966. * @param bool
  967. * @return array
  968. */
  969. function adjust_date($month, $year, $pad = FALSE)
  970. {
  971. ee()->load->library(array('logger', 'calendar'));
  972. ee()->logger->deprecated('2.6', 'Calendar::adjust_date');
  973. return ee()->calendar->adjust_date($month, $year, $pad);
  974. }
  975. }
  976. // END CLASS
  977. /* End of file Localize.php */
  978. /* Location: ./system/expressionengine/libraries/Localize.php */