]> git.mxchange.org Git - friendica.git/commitdiff
Remove most calls to date_default_timezone_* calls
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 3 Oct 2021 16:38:47 +0000 (12:38 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sun, 3 Oct 2021 17:42:31 +0000 (13:42 -0400)
- 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
mod/item.php
mod/settings.php
src/App.php
src/Core/Worker.php
src/Module/Conversation/Network.php
src/Module/Profile/Status.php
src/Protocol/DFRN.php
src/Security/Authentication.php
src/Util/DateTimeFormat.php
src/Util/Temporal.php

index 89da0c2c0eb40b91bde9b7513a29890322d4a2cc..75c87df44eab1883ff08f577a8dfa52546315511 100644 (file)
@@ -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');
index f40f6ad45d30fb37f3851f55b8bbf7bed8ac8d10..bfb865b790553d3ee293acfc40f117a4beba0652 100644 (file)
@@ -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']);
index 9ea99789d18c7b3a520fe68e0557a51d7b55e844..e84917c35c67c9d9361e72ac214b4676fc563812 100644 (file)
@@ -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
index 7c7496c3baf138a0b47d66b9241d2653d39354eb..5c85c9f26d46ad29a7260c44d117bee4676750fd 100644 (file)
@@ -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);
        }
 
        /**
index 307451c05205eb7f811bccf4bcab7f160776dda9..3d3e11d8dd642e7c20eb9716fb3d439465a0fe2c 100644 (file)
@@ -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
index 405e3772d15c6dff47f80a7abd4100b0e753f700..f2dc1fae1c744f98550d48c84d0bcaa3e70c32a3 100644 (file)
@@ -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) {
index 3c1ed8c5ac8ad6fb35dd361d9afc04e76753b45f..8235c3265db6077d622888d07ae09fa77371d55d 100644 (file)
@@ -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?
index 6c692b21fd24fcca4f122e9b3149f8384a63cd73..d73eabc2d277036ab340ba5cc83f2bd35bfdaf59 100644 (file)
@@ -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");
index e894c6ddaf776fbe3e925fc7d98b290c53b44f80..c0dc1e9aa7402144a632b2a65a8c22cc062fd531 100644 (file)
@@ -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']);
                }
 
index a1eb749948fcea4ea8047923942411a43dd71b5d..fdb49dabdceeef7a1212b21b4ee7b245146d01ef 100644 (file)
@@ -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);
        }
index b5619cf49cbce0a273d61b608e6ff1e722fdf48e..0b1b26fe9d673e3f3673a8282dfd75e1f8f3257a 100644 (file)
@@ -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);