mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-08 03:01:43 +09:00
Port all time-related functions to new timezone handling system
This commit is contained in:
parent
d5ba13d7e1
commit
c27da2ed24
5 changed files with 68 additions and 34 deletions
|
|
@ -531,7 +531,6 @@ class Context
|
|||
$db_info->time_zone = $config['locale']['internal_timezone'];
|
||||
$db_info->time_zone = sprintf('%s%02d%02d', $db_info->time_zone >= 0 ? '+' : '-', abs($db_info->time_zone) / 3600, (abs($db_info->time_zone) % 3600 / 60));
|
||||
$GLOBALS['_time_zone'] = $db_info->time_zone;
|
||||
$GLOBALS['_time_zone_offset'] = $config['locale']['internal_timezone'];
|
||||
$db_info->delay_session = $config['session']['delay'] ? 'Y' : 'N';
|
||||
$db_info->use_db_session = $config['session']['use_db'] ? 'Y' : 'N';
|
||||
$db_info->minify_scripts = $config['view']['minify_scripts'] ? 'Y' : 'N';
|
||||
|
|
|
|||
|
|
@ -257,7 +257,7 @@ class Config
|
|||
$config['locale']['enabled_lang'] = array_values($lang_selected);
|
||||
|
||||
// Convert timezone configuration.
|
||||
$old_timezone = get_time_zone_offset($db_info->time_zone ?: '+0900');
|
||||
$old_timezone = DateTime::getTimezoneOffsetByLegacyFormat($db_info->time_zone ?: '+0900');
|
||||
switch ($old_timezone)
|
||||
{
|
||||
case 32400:
|
||||
|
|
|
|||
|
|
@ -13,19 +13,43 @@ class DateTime
|
|||
protected static $_timezones = array();
|
||||
|
||||
/**
|
||||
* Get the timezone to display for the current user.
|
||||
* Format a Unix timestamp for the current user's timezone.
|
||||
*
|
||||
* @param string $format Format used in PHP date() function
|
||||
* @param int $timestamp Unix timestamp (optional, default is now)
|
||||
* @return string
|
||||
*/
|
||||
public static function formatTimestampForCurrentUser($format, $timestamp = null)
|
||||
{
|
||||
$timezone = self::getTimezoneForCurrentUser();
|
||||
if (!isset(self::$_timezones[$timezone]))
|
||||
{
|
||||
self::$_timezones[$timezone] = new \DateTimeZone($timezone);
|
||||
}
|
||||
$datetime = new \DateTime();
|
||||
$datetime->setTimestamp($timestamp ?: time());
|
||||
$datetime->setTimezone(self::$_timezones[$timezone]);
|
||||
return $datetime->format($format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current user's timezone.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getCurrentUserTimezone()
|
||||
public static function getTimezoneForCurrentUser()
|
||||
{
|
||||
if (isset($_SESSION['timezone']))
|
||||
if (isset($_SESSION['timezone']) && $_SESSION['timezone'])
|
||||
{
|
||||
return $_SESSION['timezone'];
|
||||
}
|
||||
elseif ($default = Config::get('locale.default_timezone'))
|
||||
{
|
||||
return $default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Config::get('locale.default_timezone');
|
||||
return @date_default_timezone_get();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -84,6 +108,20 @@ class DateTime
|
|||
return self::getTimezoneOffset($timezone, $timestamp) - Config::get('locale.internal_timezone');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the absolute (UTC) offset of a timezone written in XE legacy format ('+0900').
|
||||
*
|
||||
* @param string $timezone
|
||||
* @return int
|
||||
*/
|
||||
public static function getTimezoneOffsetByLegacyFormat($timezone)
|
||||
{
|
||||
$multiplier = ($timezone[0] === '-') ? -60 : 60;
|
||||
$timezone = preg_replace('/[^0-9]/', '', $timezone);
|
||||
list($hours, $minutes) = str_split($timezone, 2);
|
||||
return (((int)$hours * 60) + (int)$minutes) * $multiplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a PHP time zone by UTC offset.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -531,35 +531,32 @@ function cut_str($string, $cut_size = 0, $tail = '...')
|
|||
}
|
||||
|
||||
/**
|
||||
* Get integer offset of time zone
|
||||
* Convert XE legacy time zone format into UTC offset.
|
||||
*
|
||||
* @param string $time_zone Time zone in +0900 format
|
||||
* @param string $time_zone Time zone in '+0900' format
|
||||
* @return int
|
||||
*/
|
||||
function get_time_zone_offset($time_zone)
|
||||
function get_time_zone_offset($timezone)
|
||||
{
|
||||
$multiplier = ($time_zone[0] === '-') ? -60 : 60;
|
||||
$time_zone = preg_replace('/[^0-9]/', '', $time_zone);
|
||||
list($hours, $minutes) = str_split($time_zone, 2);
|
||||
return (((int)$hours * 60) + (int)$minutes) * $multiplier;
|
||||
return Rhymix\Framework\DateTime::getTimezoneOffsetByLegacyFormat($timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a time gap between server's timezone and XE's timezone
|
||||
* Get the offset between the current user's time zone and Rhymix's internal time zone.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function zgap()
|
||||
function zgap($timestamp = null)
|
||||
{
|
||||
$time_zone_offset = $GLOBALS['_time_zone_offset'];
|
||||
$server_offset = date('Z');
|
||||
return $time_zone_offset - $server_offset;
|
||||
$current_user_timezone = Rhymix\Framework\DateTime::getTimezoneForCurrentUser();
|
||||
return Rhymix\Framework\DateTime::getTimezoneOffsetFromInternal($current_user_timezone, $timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* YYYYMMDDHHIISS format changed to unix time value
|
||||
* Convert YYYYMMDDHHIISS format to Unix timestamp.
|
||||
* This function assumes the internal timezone.
|
||||
*
|
||||
* @param string $str Time value in format of YYYYMMDDHHIISS
|
||||
* @param string $str Time in YYYYMMDDHHIISS format
|
||||
* @return int
|
||||
*/
|
||||
function ztime($str)
|
||||
|
|
@ -576,24 +573,24 @@ function ztime($str)
|
|||
$hour = (int)substr($str, 8, 2);
|
||||
$min = (int)substr($str, 10, 2);
|
||||
$sec = (int)substr($str, 12, 2);
|
||||
$offset = zgap();
|
||||
}
|
||||
else
|
||||
{
|
||||
$hour = $min = $sec = $offset = 0;
|
||||
$hour = $min = $sec = 0;
|
||||
}
|
||||
return mktime($hour, $min, $sec, $month, $day, $year) + $offset;
|
||||
return mktime($hour, $min, $sec, $month, $day, $year);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the time format YYYYMMDDHHIISS to the user defined format
|
||||
* Convert YYYYMMDDHHIISS format to user-defined format.
|
||||
* This function assumes the internal timezone.
|
||||
*
|
||||
* @param string|int $str YYYYMMDDHHIISS format time values
|
||||
* @param string $format Time format of php date() function
|
||||
* @param bool $conversion Means whether to convert automatically according to the language
|
||||
* @param string $str Time in YYYYMMDDHHIISS format
|
||||
* @param string $format Time format for date() function
|
||||
* @param bool $conversion If true, convert automatically for the current language.
|
||||
* @return string
|
||||
*/
|
||||
function zdate($str, $format = 'Y-m-d H:i:s', $conversion = TRUE)
|
||||
function zdate($str, $format = 'Y-m-d H:i:s', $conversion = true)
|
||||
{
|
||||
if(!$str)
|
||||
{
|
||||
|
|
@ -637,18 +634,18 @@ function zdate($str, $format = 'Y-m-d H:i:s', $conversion = TRUE)
|
|||
}
|
||||
}
|
||||
|
||||
// get unixtime by using ztime() for date() function's argument.
|
||||
$string = date($format, ztime($str));
|
||||
// get unixtime by using ztime() for date() function's argument.
|
||||
$result = Rhymix\Framework\DateTime::formatTimestampForCurrentUser($format, ztime($str));
|
||||
|
||||
// change day and am/pm for each language
|
||||
if(preg_match('/[MFAa]/', $format))
|
||||
{
|
||||
$unit_week = Context::getLang('unit_week');
|
||||
$unit_meridiem = Context::getLang('unit_meridiem');
|
||||
$string = str_replace(array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'), $unit_week, $string);
|
||||
$string = str_replace(array('am', 'pm', 'AM', 'PM'), $unit_meridiem, $string);
|
||||
$string = str_replace(array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'), $unit_week, $result);
|
||||
$string = str_replace(array('am', 'pm', 'AM', 'PM'), $unit_meridiem, $result);
|
||||
}
|
||||
return $string;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -163,7 +163,7 @@ class installController extends install
|
|||
}
|
||||
else
|
||||
{
|
||||
$user_timezone = intval(get_time_zone_offset($time_zone ?: '+0900') / 3600);
|
||||
$user_timezone = intval(Rhymix\Framework\DateTime::getTimezoneOffsetByLegacyFormat($time_zone ?: '+0900') / 3600);
|
||||
switch ($user_timezone)
|
||||
{
|
||||
case 9:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue