From 6db211568ae283d152ee8fa0cfe17a2bab809779 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 3 Oct 2021 12:38:47 -0400 Subject: [PATCH] Remove most calls to date_default_timezone_* calls - It was wrongly used to set the node-wide ot user-specific timezone - It is now fully managed from the App object - Add a static variable to DateTimeFormat maintain the convenient local() method --- mod/events.php | 8 ++++---- mod/item.php | 2 +- mod/settings.php | 4 ++-- src/App.php | 17 ++++++++++------- src/Core/Worker.php | 3 --- src/Module/Conversation/Network.php | 4 ++-- src/Module/Profile/Status.php | 4 ++-- src/Protocol/DFRN.php | 5 +---- src/Security/Authentication.php | 1 - src/Util/DateTimeFormat.php | 11 +++++++++-- src/Util/Temporal.php | 12 ++++-------- 11 files changed, 35 insertions(+), 36 deletions(-) diff --git a/mod/events.php b/mod/events.php index 89da0c2c0e..75c87df44e 100644 --- a/mod/events.php +++ b/mod/events.php @@ -100,9 +100,9 @@ function events_post(App $a) } if ($adjust) { - $start = DateTimeFormat::convert($start, 'UTC', date_default_timezone_get()); + $start = DateTimeFormat::convert($start, 'UTC', $a->getTimeZone()); if (!$nofinish) { - $finish = DateTimeFormat::convert($finish, 'UTC', date_default_timezone_get()); + $finish = DateTimeFormat::convert($finish, 'UTC', $a->getTimeZone()); } } else { $start = DateTimeFormat::utc($start); @@ -481,9 +481,9 @@ function events_content(App $a) $sdt = $orig_event['start'] ?? 'now'; $fdt = $orig_event['finish'] ?? 'now'; - $tz = date_default_timezone_get(); + $tz = $a->getTimeZone(); if (isset($orig_event['adjust'])) { - $tz = ($orig_event['adjust'] ? date_default_timezone_get() : 'UTC'); + $tz = ($orig_event['adjust'] ? $a->getTimeZone() : 'UTC'); } $syear = DateTimeFormat::convert($sdt, $tz, 'UTC', 'Y'); diff --git a/mod/item.php b/mod/item.php index f40f6ad45d..bfb865b790 100644 --- a/mod/item.php +++ b/mod/item.php @@ -686,7 +686,7 @@ function item_post(App $a) { Hook::callAll('post_local',$datarray); if (!empty($_REQUEST['scheduled_at'])) { - $scheduled_at = DateTimeFormat::convert($_REQUEST['scheduled_at'], 'UTC', $a->getTimezone()); + $scheduled_at = DateTimeFormat::convert($_REQUEST['scheduled_at'], 'UTC', $a->getTimeZone()); if ($scheduled_at > DateTimeFormat::utcNow()) { unset($datarray['created']); unset($datarray['edited']); diff --git a/mod/settings.php b/mod/settings.php index 9ea99789d1..e84917c35c 100644 --- a/mod/settings.php +++ b/mod/settings.php @@ -333,7 +333,7 @@ function settings_post(App $a) } if (($timezone != $user['timezone']) && strlen($timezone)) { - date_default_timezone_set($timezone); + $a->setTimeZone($timezone); } $aclFormatter = DI::aclFormatter(); @@ -601,7 +601,7 @@ function settings_content(App $a) $expire_network_only = DI::pConfig()->get(local_user(), 'expire', 'network_only', false); if (!strlen($user['timezone'])) { - $timezone = date_default_timezone_get(); + $timezone = $a->getTimeZone(); } // Set the account type to "Community" when the page is a community page but the account type doesn't fit diff --git a/src/App.php b/src/App.php index 7c7496c3ba..5c85c9f26d 100644 --- a/src/App.php +++ b/src/App.php @@ -40,6 +40,7 @@ use Friendica\Model\Profile; use Friendica\Module\Special\HTTPException as ModuleHTTPException; use Friendica\Network\HTTPException; use Friendica\Util\ConfigFileLoader; +use Friendica\Util\DateTimeFormat; use Friendica\Util\HTTPSignature; use Friendica\Util\Profiler; use Friendica\Util\Strings; @@ -217,12 +218,13 @@ class App /** * Set the timezone * - * @param int $timezone + * @param string $timezone A valid time zone identifier, see https://www.php.net/manual/en/timezones.php * @return void */ public function setTimeZone(string $timezone) { - $this->timezone = $timezone; + $this->timezone = (new \DateTimeZone($timezone))->getName(); + DateTimeFormat::setLocalTimeZone($this->timezone); } /** @@ -338,6 +340,9 @@ class App { set_time_limit(0); + // Ensure that all "strtotime" operations do run timezone independent + date_default_timezone_set('UTC'); + // This has to be quite large to deal with embedded private photos ini_set('pcre.backtrack_limit', 500000); @@ -372,15 +377,13 @@ class App private function loadDefaultTimezone() { if ($this->config->get('system', 'default_timezone')) { - $this->timezone = $this->config->get('system', 'default_timezone'); + $timezone = $this->config->get('system', 'default_timezone', 'UTC'); } else { global $default_timezone; - $this->timezone = !empty($default_timezone) ? $default_timezone : 'UTC'; + $timezone = $default_timezone ?? '' ?: 'UTC'; } - if ($this->timezone) { - date_default_timezone_set($this->timezone); - } + $this->setTimeZone($timezone); } /** diff --git a/src/Core/Worker.php b/src/Core/Worker.php index 307451c052..3d3e11d8dd 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -61,9 +61,6 @@ class Worker */ public static function processQueue($run_cron = true) { - // Ensure that all "strtotime" operations do run timezone independent - date_default_timezone_set('UTC'); - self::$up_start = microtime(true); // At first check the maximum load. We shouldn't continue with a high load diff --git a/src/Module/Conversation/Network.php b/src/Module/Conversation/Network.php index 405e3772d1..f2dc1fae1c 100644 --- a/src/Module/Conversation/Network.php +++ b/src/Module/Conversation/Network.php @@ -372,10 +372,10 @@ class Network extends BaseModule } if (self::$dateFrom) { - $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', date_default_timezone_get())]); + $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', DI::app()->getTimeZone())]); } if (self::$dateTo) { - $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', date_default_timezone_get())]); + $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', DI::app()->getTimeZone())]); } if (self::$groupId) { diff --git a/src/Module/Profile/Status.php b/src/Module/Profile/Status.php index 3c1ed8c5ac..8235c3265d 100644 --- a/src/Module/Profile/Status.php +++ b/src/Module/Profile/Status.php @@ -151,10 +151,10 @@ class Status extends BaseProfile } if (!empty($datequery)) { - $condition = DBA::mergeConditions($condition, ["`received` <= ?", DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get())]); + $condition = DBA::mergeConditions($condition, ["`received` <= ?", DateTimeFormat::convert($datequery, 'UTC', $a->getTimeZone())]); } if (!empty($datequery2)) { - $condition = DBA::mergeConditions($condition, ["`received` >= ?", DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get())]); + $condition = DBA::mergeConditions($condition, ["`received` >= ?", DateTimeFormat::convert($datequery2, 'UTC', $a->getTimeZone())]); } // Does the profile page belong to a forum? diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 6c692b21fd..d73eabc2d2 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -488,10 +488,7 @@ class DFRN XML::addElement($doc, $author, "poco:note", $profile["about"]); XML::addElement($doc, $author, "poco:preferredUsername", $profile["nickname"]); - $savetz = date_default_timezone_get(); - date_default_timezone_set($profile["timezone"]); - XML::addElement($doc, $author, "poco:utcOffset", date("P")); - date_default_timezone_set($savetz); + XML::addElement($doc, $author, "poco:utcOffset", DateTimeFormat::timezoneNow($profile["timezone"], "P")); if (trim($profile["homepage"]) != "") { $urls = $doc->createElement("poco:urls"); diff --git a/src/Security/Authentication.php b/src/Security/Authentication.php index e894c6ddaf..c0dc1e9aa7 100644 --- a/src/Security/Authentication.php +++ b/src/Security/Authentication.php @@ -305,7 +305,6 @@ class Authentication $this->session->set('new_member', time() < ($member_since + (60 * 60 * 24 * 14))); if (strlen($user_record['timezone'])) { - date_default_timezone_set($user_record['timezone']); $a->setTimeZone($user_record['timezone']); } diff --git a/src/Util/DateTimeFormat.php b/src/Util/DateTimeFormat.php index a1eb749948..fdb49dabdc 100644 --- a/src/Util/DateTimeFormat.php +++ b/src/Util/DateTimeFormat.php @@ -36,6 +36,13 @@ class DateTimeFormat const HTTP = 'D, d M Y H:i:s \G\M\T'; const JSON = 'Y-m-d\TH:i:s.v\Z'; + static $localTimezone = 'UTC'; + + public static function setLocalTimeZone(string $timezone) + { + self::$localTimezone = $timezone; + } + /** * convert() shorthand for UTC. * @@ -59,7 +66,7 @@ class DateTimeFormat */ public static function local($time, $format = self::MYSQL) { - return self::convert($time, date_default_timezone_get(), 'UTC', $format); + return self::convert($time, self::$localTimezone, 'UTC', $format); } /** @@ -160,7 +167,7 @@ class DateTimeFormat $to_obj = new DateTimeZone('UTC'); } - $d->setTimeZone($to_obj); + $d->setTimezone($to_obj); return $d->format($format); } diff --git a/src/Util/Temporal.php b/src/Util/Temporal.php index b5619cf49c..0b1b26fe9d 100644 --- a/src/Util/Temporal.php +++ b/src/Util/Temporal.php @@ -360,23 +360,19 @@ class Temporal * Returns the age in years, given a date of birth and the timezone of the person * whose date of birth is provided. * - * @param string $dob Date of Birth - * @param string $owner_tz (optional) Timezone of the person of interest + * @param string $dob Date of Birth + * @param string $timezone Timezone of the person of interest * * @return int Age in years * @throws \Exception */ - public static function getAgeByTimezone($dob, $owner_tz = ''): int + public static function getAgeByTimezone(string $dob, string $timezone): int { if (!intval($dob)) { return 0; } - if (!$owner_tz) { - $owner_tz = date_default_timezone_get(); - } - - $birthdate = new DateTime($dob . ' 00:00:00', new DateTimeZone($owner_tz)); + $birthdate = new DateTime($dob . ' 00:00:00', new DateTimeZone($timezone)); $currentDate = new DateTime('now', new DateTimeZone('UTC')); $interval = $birthdate->diff($currentDate);