Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ticket/13426] Improve user timezone initialization #5916

Merged
merged 1 commit into from Apr 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 34 additions & 12 deletions phpBB/phpbb/user.php
Expand Up @@ -227,15 +227,7 @@ function setup($lang_set = false, $style_id = false)

$this->language->set_user_language($user_lang_name);

try
{
$this->timezone = new \DateTimeZone($user_timezone);
}
catch (\Exception $e)
{
// If the timezone the user has selected is invalid, we fall back to UTC.
$this->timezone = new \DateTimeZone('UTC');
}
$this->create_timezone($user_timezone);

$this->add_lang($lang_set);
unset($lang_set);
Expand Down Expand Up @@ -635,7 +627,7 @@ function format_date($gmepoch, $format = false, $forcedate = false)
if (!$format_date_override)
{
$time = new $this->datetime($this, '@' . (int) $gmepoch, $utc);
$time->setTimezone($this->timezone);
$time->setTimezone($this->create_timezone());

return $time->format($format, $forcedate);
}
Expand All @@ -645,6 +637,36 @@ function format_date($gmepoch, $format = false, $forcedate = false)
}
}

/**
* Create a DateTimeZone object in the context of the current user
*
* @param string $user_timezone Time zone of the current user.
* @return DateTimeZone DateTimeZone object linked to the current users locale
*/
public function create_timezone($user_timezone = null)
{
if (!$this->timezone)
{
if (!$user_timezone)
{
global $config;
$user_timezone = ($this->data['user_id'] != ANONYMOUS) ? $this->data['user_timezone'] : $config['board_timezone'];
}

try
{
$this->timezone = new \DateTimeZone($user_timezone);
}
catch (\Exception $e)
{
// If the timezone the user has selected is invalid, we fall back to UTC.
$this->timezone = new \DateTimeZone('UTC');
}
}

return $this->timezone;
rxu marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Create a \phpbb\datetime object in the context of the current user
*
Expand All @@ -655,7 +677,7 @@ function format_date($gmepoch, $format = false, $forcedate = false)
*/
public function create_datetime($time = 'now', \DateTimeZone $timezone = null)
{
$timezone = $timezone ?: $this->timezone;
$timezone = $timezone ?: $this->create_timezone();
return new $this->datetime($this, $time, $timezone);
}

Expand All @@ -669,7 +691,7 @@ public function create_datetime($time = 'now', \DateTimeZone $timezone = null)
*/
public function get_timestamp_from_format($format, $time, \DateTimeZone $timezone = null)
{
$timezone = $timezone ?: $this->timezone;
$timezone = $timezone ?: $this->create_timezone();
$date = \DateTime::createFromFormat($format, $time, $timezone);
return ($date !== false) ? $date->format('U') : false;
}
Expand Down