DateTimeZone.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC;
  8. use OCP\IConfig;
  9. use OCP\IDateTimeZone;
  10. use OCP\ISession;
  11. use Psr\Log\LoggerInterface;
  12. class DateTimeZone implements IDateTimeZone {
  13. /** @var IConfig */
  14. protected $config;
  15. /** @var ISession */
  16. protected $session;
  17. /**
  18. * Constructor
  19. *
  20. * @param IConfig $config
  21. * @param ISession $session
  22. */
  23. public function __construct(IConfig $config, ISession $session) {
  24. $this->config = $config;
  25. $this->session = $session;
  26. }
  27. /**
  28. * Get the timezone of the current user, based on their session information and config data
  29. *
  30. * @param bool|int $timestamp
  31. * @return \DateTimeZone
  32. */
  33. public function getTimeZone($timestamp = false) {
  34. $timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null);
  35. if ($timeZone === null) {
  36. if ($this->session->exists('timezone')) {
  37. return $this->guessTimeZoneFromOffset($this->session->get('timezone'), $timestamp);
  38. }
  39. $timeZone = $this->getDefaultTimeZone();
  40. }
  41. try {
  42. return new \DateTimeZone($timeZone);
  43. } catch (\Exception $e) {
  44. \OC::$server->get(LoggerInterface::class)->debug('Failed to created DateTimeZone "' . $timeZone . '"', ['app' => 'datetimezone']);
  45. return new \DateTimeZone($this->getDefaultTimeZone());
  46. }
  47. }
  48. /**
  49. * Guess the DateTimeZone for a given offset
  50. *
  51. * We first try to find a Etc/GMT* timezone, if that does not exist,
  52. * we try to find it manually, before falling back to UTC.
  53. *
  54. * @param mixed $offset
  55. * @param bool|int $timestamp
  56. * @return \DateTimeZone
  57. */
  58. protected function guessTimeZoneFromOffset($offset, $timestamp) {
  59. try {
  60. // Note: the timeZone name is the inverse to the offset,
  61. // so a positive offset means negative timeZone
  62. // and the other way around.
  63. if ($offset > 0) {
  64. $timeZone = 'Etc/GMT-' . $offset;
  65. } else {
  66. $timeZone = 'Etc/GMT+' . abs($offset);
  67. }
  68. return new \DateTimeZone($timeZone);
  69. } catch (\Exception $e) {
  70. // If the offset has no Etc/GMT* timezone,
  71. // we try to guess one timezone that has the same offset
  72. foreach (\DateTimeZone::listIdentifiers() as $timeZone) {
  73. $dtz = new \DateTimeZone($timeZone);
  74. $dateTime = new \DateTime();
  75. if ($timestamp !== false) {
  76. $dateTime->setTimestamp($timestamp);
  77. }
  78. $dtOffset = $dtz->getOffset($dateTime);
  79. if ($dtOffset == 3600 * $offset) {
  80. return $dtz;
  81. }
  82. }
  83. // No timezone found, fallback to UTC
  84. \OC::$server->get(LoggerInterface::class)->debug('Failed to find DateTimeZone for offset "' . $offset . '"', ['app' => 'datetimezone']);
  85. return new \DateTimeZone($this->getDefaultTimeZone());
  86. }
  87. }
  88. /**
  89. * Get the default timezone of the server
  90. *
  91. * Falls back to UTC if it is not yet set.
  92. *
  93. * @return string
  94. */
  95. protected function getDefaultTimeZone() {
  96. $serverTimeZone = date_default_timezone_get();
  97. return $serverTimeZone ?: 'UTC';
  98. }
  99. }