Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Actions/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ public static function getEventRange(string $low_date, string $high_date, bool $
$occurrences = [];

$one_day = new \DateInterval('P1D');
$tz = new \DateTimeZone(User::getTimezone());
$tz = TimeZone::create(User::getTimezone());
$high_date = (new \DateTimeImmutable($high_date . ' +1 day'))->format('Y-m-d');

foreach (Event::getOccurrencesInRange($low_date, $high_date, $use_permissions) as $occurrence) {
Expand Down
9 changes: 5 additions & 4 deletions Sources/Actions/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use SMF\Routable;
use SMF\Theme;
use SMF\Time;
use SMF\TimeZone;
use SMF\Url;
use SMF\User;
use SMF\Utils;
Expand Down Expand Up @@ -684,7 +685,7 @@ public function getXmlMembers(): array
[
'tag' => 'time',
'attributes' => ['label' => Lang::getTxt('date_registered', file: 'General'), 'UTC' => Time::gmstrftime('%F %T', (int) $row['date_registered'])],
'content' => Utils::htmlspecialchars(strip_tags(Time::create('@' . $row['date_registered'], new \DateTimeZone(Config::$modSettings['default_timezone']))->format(null, false))),
'content' => Utils::htmlspecialchars(strip_tags(Time::create('@' . $row['date_registered'], TimeZone::create(Config::$modSettings['default_timezone']))->format(null, false))),
],
[
'tag' => 'id',
Expand Down Expand Up @@ -1480,7 +1481,7 @@ function ($a, $b) {
[
'tag' => 'time',
'attributes' => ['label' => Lang::getTxt('date', file: 'General'), 'UTC' => Time::gmstrftime('%F %T', (int) $row['poster_time'])],
'content' => Utils::htmlspecialchars(strip_tags(Time::create('@' . $row['poster_time'], new \DateTimeZone(Config::$modSettings['default_timezone']))->format(null, false))),
'content' => Utils::htmlspecialchars(strip_tags(Time::create('@' . $row['poster_time'], TimeZone::create(Config::$modSettings['default_timezone']))->format(null, false))),
],
[
'tag' => 'id',
Expand Down Expand Up @@ -1779,12 +1780,12 @@ public function getXmlProfile(): array
[
'tag' => 'last-login',
'attributes' => ['label' => Lang::getTxt('lastLoggedIn', file: 'Profile'), 'UTC' => Time::gmstrftime('%F %T', (int) $profile['last_login_timestamp'])],
'content' => Time::create('@' . $profile['last_login_timestamp'], new \DateTimeZone(Config::$modSettings['default_timezone']))->format(null, false),
'content' => Time::create('@' . $profile['last_login_timestamp'], TimeZone::create(Config::$modSettings['default_timezone']))->format(null, false),
],
[
'tag' => 'registered',
'attributes' => ['label' => Lang::getTxt('date_registered', file: 'General'), 'UTC' => Time::gmstrftime('%F %T', (int) $profile['registered_timestamp'])],
'content' => Time::create('@' . $profile['registered_timestamp'], new \DateTimeZone(Config::$modSettings['default_timezone']))->format(null, false),
'content' => Time::create('@' . $profile['registered_timestamp'], TimeZone::create(Config::$modSettings['default_timezone']))->format(null, false),
],
[
'tag' => 'avatar',
Expand Down
2 changes: 1 addition & 1 deletion Sources/Logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public static function trackStatsUsersOnline(int $total_users_online): void
];
}

$date = (new \DateTime('now', new \DateTimeZone(Config::$modSettings['default_timezone'])))->format('Y-m-d');
$date = (new \DateTime('now', TimeZone::create(Config::$modSettings['default_timezone'])))->format('Y-m-d');

// No entry exists for today yet?
if (!isset(Config::$modSettings['mostOnlineUpdated']) || Config::$modSettings['mostOnlineUpdated'] != $date) {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ class Time extends \DateTime implements \ArrayAccess
public function __construct(string $datetime = 'now', \DateTimeZone|string|null $timezone = null)
{
if (!isset(self::$user_tz)) {
self::$user_tz = new \DateTimeZone(User::getTimezone());
self::$user_tz = TimeZone::create(User::getTimezone());
}

if (\is_string($timezone)) {
$timezone = new \DateTimeZone($timezone);
$timezone = TimeZone::create($timezone);
}

$datetime = self::sanitize($datetime);
Expand Down Expand Up @@ -731,7 +731,7 @@ public function setTimezone(\DateTimeZone|string $timezone): static
if ($timezone instanceof \DateTimeZone) {
date_timezone_set($this, $timezone);
} elseif (\in_array($timezone, \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC))) {
date_timezone_set($this, new \DateTimeZone($timezone));
date_timezone_set($this, TimeZone::create($timezone));
} else {
throw new \ValueError();
}
Expand Down Expand Up @@ -815,7 +815,7 @@ public static function strftime(string $format, ?int $timestamp = null, ?string
}

$date = new self('@' . $timestamp);
$date->setTimezone(new \DateTimeZone($tzid));
$date->setTimezone(TimeZone::create($tzid));

return $date->format($format, false, true);
}
Expand Down
15 changes: 15 additions & 0 deletions Sources/TimeZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,21 @@ public function getStandardOffset(int|string $when = 'now'): int
* Public static methods
***********************/

/**
* Creates a new DateTimeZone object.
*
* @param string $timezone One of the supported timezone names , an offset value (+0200), or a timezone abbreviation (BST).
* @return bool|\DateTimeZone Returns DateTimeZone on success. Procedural style returns `false` on failure.
*/
public static function create(string $timezone): bool|\DateTimeZone {
try {
return new \DateTimeZone($timezone);
}
catch (\Exception $ex) {
return new \DateTimeZone(Config::$modSettings['default_timezone']);
}
}

/**
* Get a list of time zones.
*
Expand Down
6 changes: 3 additions & 3 deletions Sources/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4321,16 +4321,16 @@ protected function fixTimezoneSetting(): void
// Figure out the new time offset.
if (!empty(self::$profiles[$this->id]['timezone'])) {
// Get the offsets from UTC for the server, then for the user.
$tz_system = new \DateTimeZone(Config::$modSettings['default_timezone']);
$tz_user = new \DateTimeZone(self::$profiles[$this->id]['timezone']);
$tz_system = TimeZone::create(Config::$modSettings['default_timezone']);
$tz_user = TimeZone::create(self::$profiles[$this->id]['timezone']);
$time_system = new \DateTime('now', $tz_system);
$time_user = new \DateTime('now', $tz_user);
self::$profiles[$this->id]['time_offset'] = ($tz_user->getOffset($time_user) - $tz_system->getOffset($time_system)) / 3600;
}
// We need a time zone.
else {
if (!empty(self::$profiles[$this->id]['time_offset'])) {
$tz_system = new \DateTimeZone(Config::$modSettings['default_timezone']);
$tz_system = TimeZone::create(Config::$modSettings['default_timezone']);
$time_system = new \DateTime('now', $tz_system);

self::$profiles[$this->id]['timezone'] = @timezone_name_from_abbr('', (int) ($tz_system->getOffset($time_system) + self::$profiles[$this->id]['time_offset'] * 3600), (int) $time_system->format('I'));
Expand Down