Merge pull request #54483 from nextcloud/feat/fetch-user-timezone

feat(IDateTimeZone): allow to fetch timezone of specified user
pull/54474/head
Ferdinand Thiessen 2025-08-18 20:36:46 +07:00 committed by GitHub
commit 6c003eda5c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 34 deletions

@ -3598,11 +3598,6 @@
<code><![CDATA[string]]></code> <code><![CDATA[string]]></code>
</InvalidReturnType> </InvalidReturnType>
</file> </file>
<file src="lib/private/DateTimeZone.php">
<InvalidScalarArgument>
<code><![CDATA[$timestamp]]></code>
</InvalidScalarArgument>
</file>
<file src="lib/private/Diagnostics/Query.php"> <file src="lib/private/Diagnostics/Query.php">
<ImplementedReturnTypeMismatch> <ImplementedReturnTypeMismatch>
<code><![CDATA[float]]></code> <code><![CDATA[float]]></code>

@ -31,25 +31,34 @@ class DateTimeZone implements IDateTimeZone {
} }
/** /**
* Get the timezone of the current user, based on their session information and config data * @inheritdoc
*
* @param bool|int $timestamp
* @return \DateTimeZone
*/ */
public function getTimeZone($timestamp = false) { public function getTimeZone($timestamp = false, ?string $userId = null): \DateTimeZone {
$timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null); $uid = $userId ?? $this->session->get('user_id');
if ($timeZone === null) { $timezoneName = $this->config->getUserValue($uid, 'core', 'timezone', '');
if ($this->session->exists('timezone')) { if ($timezoneName === '') {
if ($uid === $userId && $this->session->exists('timezone')) {
return $this->guessTimeZoneFromOffset($this->session->get('timezone'), $timestamp); return $this->guessTimeZoneFromOffset($this->session->get('timezone'), $timestamp);
} }
$timeZone = $this->getDefaultTimeZone(); return $this->getDefaultTimeZone();
} }
try { try {
return new \DateTimeZone($timeZone); return new \DateTimeZone($timezoneName);
} catch (\Exception $e) { } catch (\Exception $e) {
\OC::$server->get(LoggerInterface::class)->debug('Failed to created DateTimeZone "' . $timeZone . '"', ['app' => 'datetimezone']); \OCP\Server::get(LoggerInterface::class)->debug('Failed to created DateTimeZone "' . $timezoneName . '"', ['app' => 'datetimezone']);
return new \DateTimeZone($this->getDefaultTimeZone()); 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. * we try to find it manually, before falling back to UTC.
* *
* @param mixed $offset * @param mixed $offset
* @param bool|int $timestamp * @param int|false $timestamp
* @return \DateTimeZone * @return \DateTimeZone
*/ */
protected function guessTimeZoneFromOffset($offset, $timestamp) { protected function guessTimeZoneFromOffset($offset, $timestamp) {
@ -93,20 +102,8 @@ class DateTimeZone implements IDateTimeZone {
} }
// No timezone found, fallback to UTC // No timezone found, fallback to UTC
\OC::$server->get(LoggerInterface::class)->debug('Failed to find DateTimeZone for offset "' . $offset . '"', ['app' => 'datetimezone']); \OCP\Server::get(LoggerInterface::class)->debug('Failed to find DateTimeZone for offset "' . $offset . '"', ['app' => 'datetimezone']);
return new \DateTimeZone($this->getDefaultTimeZone()); 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';
}
} }

@ -13,10 +13,27 @@ namespace OCP;
* @since 8.0.0 * @since 8.0.0
*/ */
interface IDateTimeZone { 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 bool|int $timestamp
* @param ?string $userId - The user to fetch the timezone for (defaults to current user)
* @return \DateTimeZone * @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;
} }