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
This commit is contained in:
Hypolite Petovan 2021-10-03 12:38:47 -04:00
parent 08c24fd4fa
commit 6db211568a
11 changed files with 35 additions and 36 deletions

View 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');

View 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']);

View 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

View 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);
}
/**

View 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

View 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) {

View 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?

View 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");

View 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']);
}

View 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);
}

View file

@ -361,22 +361,18 @@ class Temporal
* 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 $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);