mirror of
https://github.com/nextcloud/server.git
synced 2026-06-08 16:26:59 -04:00
Merge pull request #54483 from nextcloud/feat/fetch-user-timezone
feat(IDateTimeZone): allow to fetch timezone of specified user
This commit is contained in:
commit
6c003eda5c
3 changed files with 43 additions and 34 deletions
|
|
@ -3598,11 +3598,6 @@
|
|||
<code><![CDATA[string]]></code>
|
||||
</InvalidReturnType>
|
||||
</file>
|
||||
<file src="lib/private/DateTimeZone.php">
|
||||
<InvalidScalarArgument>
|
||||
<code><![CDATA[$timestamp]]></code>
|
||||
</InvalidScalarArgument>
|
||||
</file>
|
||||
<file src="lib/private/Diagnostics/Query.php">
|
||||
<ImplementedReturnTypeMismatch>
|
||||
<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
|
||||
*
|
||||
* @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';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
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 getDefaultTimeZone(): \DateTimeZone;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue