diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index 07df0637d57..6a21af44a08 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -3611,11 +3611,6 @@ - - - - - diff --git a/lib/private/DateTimeZone.php b/lib/private/DateTimeZone.php index c693968fa3d..75eb3a6b769 100644 --- a/lib/private/DateTimeZone.php +++ b/lib/private/DateTimeZone.php @@ -31,25 +31,34 @@ class DateTimeZone implements IDateTimeZone { } /** - * Get the timezone of the current user, based on their session information and config data - * - * @param bool|int $timestamp - * @return \DateTimeZone + * @inheritdoc */ - public function getTimeZone($timestamp = false) { - $timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null); - if ($timeZone === null) { - if ($this->session->exists('timezone')) { + public function getTimeZone($timestamp = false, ?string $userId = null): \DateTimeZone { + $uid = $userId ?? $this->session->get('user_id'); + $timezoneName = $this->config->getUserValue($uid, 'core', 'timezone', ''); + if ($timezoneName === '') { + if ($uid === $userId && $this->session->exists('timezone')) { return $this->guessTimeZoneFromOffset($this->session->get('timezone'), $timestamp); } - $timeZone = $this->getDefaultTimeZone(); + return $this->getDefaultTimeZone(); } try { - return new \DateTimeZone($timeZone); + return new \DateTimeZone($timezoneName); } catch (\Exception $e) { - \OC::$server->get(LoggerInterface::class)->debug('Failed to created DateTimeZone "' . $timeZone . '"', ['app' => 'datetimezone']); - return new \DateTimeZone($this->getDefaultTimeZone()); + \OCP\Server::get(LoggerInterface::class)->debug('Failed to created DateTimeZone "' . $timezoneName . '"', ['app' => 'datetimezone']); + return $this->getDefaultTimeZone(); + } + } + + public function getDefaultTimeZone(): \DateTimeZone { + /** @var non-empty-string */ + $timezone = $this->config->getSystemValueString('default_timezone', 'UTC'); + try { + return new \DateTimeZone($timezone); + } catch (\Exception) { + // its always UTC see lib/base.php + return new \DateTimeZone('UTC'); } } @@ -60,7 +69,7 @@ class DateTimeZone implements IDateTimeZone { * we try to find it manually, before falling back to UTC. * * @param mixed $offset - * @param bool|int $timestamp + * @param int|false $timestamp * @return \DateTimeZone */ protected function guessTimeZoneFromOffset($offset, $timestamp) { @@ -93,20 +102,8 @@ class DateTimeZone implements IDateTimeZone { } // No timezone found, fallback to UTC - \OC::$server->get(LoggerInterface::class)->debug('Failed to find DateTimeZone for offset "' . $offset . '"', ['app' => 'datetimezone']); - return new \DateTimeZone($this->getDefaultTimeZone()); + \OCP\Server::get(LoggerInterface::class)->debug('Failed to find DateTimeZone for offset "' . $offset . '"', ['app' => 'datetimezone']); + return $this->getDefaultTimeZone(); } } - - /** - * Get the default timezone of the server - * - * Falls back to UTC if it is not yet set. - * - * @return string - */ - protected function getDefaultTimeZone() { - $serverTimeZone = date_default_timezone_get(); - return $serverTimeZone ?: 'UTC'; - } } diff --git a/lib/public/IDateTimeZone.php b/lib/public/IDateTimeZone.php index a68cb4144af..650f9d6b243 100644 --- a/lib/public/IDateTimeZone.php +++ b/lib/public/IDateTimeZone.php @@ -13,10 +13,27 @@ namespace OCP; * @since 8.0.0 */ interface IDateTimeZone { + /** + * Get the timezone for a given user. + * If a timestamp is passed the timezone for that given timestamp is retrieved (might differ due to DST). + * If no userId is passed the current user is used. + * * @param bool|int $timestamp + * @param ?string $userId - The user to fetch the timezone for (defaults to current user) * @return \DateTimeZone - * @since 8.0.0 - parameter $timestamp was added in 8.1.0 + * @since 8.0.0 + * @since 8.1.0 - parameter $timestamp was added + * @since 32.0.0 - parameter $userId was added + */ + public function getTimeZone($timestamp = false, ?string $userId = null); + + /** + * Get the timezone configured as the default for this Nextcloud server. + * While the PHP timezone is always set to UTC in Nextcloud this is the timezone + * to use for all time offset calculations if no user value is specified. + * + * @since 32.0.0 */ - public function getTimeZone($timestamp = false); + public function getDefaultTimeZone(): \DateTimeZone; }