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:
17
src/App.php
17
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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']);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user