DateTimeZone.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC;
  24. use OCP\IConfig;
  25. use OCP\IDateTimeZone;
  26. use OCP\ILogger;
  27. use OCP\ISession;
  28. class DateTimeZone implements IDateTimeZone {
  29. /** @var IConfig */
  30. protected $config;
  31. /** @var ISession */
  32. protected $session;
  33. /**
  34. * Constructor
  35. *
  36. * @param IConfig $config
  37. * @param ISession $session
  38. */
  39. public function __construct(IConfig $config, ISession $session) {
  40. $this->config = $config;
  41. $this->session = $session;
  42. }
  43. /**
  44. * Get the timezone of the current user, based on his session information and config data
  45. *
  46. * @param bool|int $timestamp
  47. * @return \DateTimeZone
  48. */
  49. public function getTimeZone($timestamp = false) {
  50. $timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null);
  51. if ($timeZone === null) {
  52. if ($this->session->exists('timezone')) {
  53. return $this->guessTimeZoneFromOffset($this->session->get('timezone'), $timestamp);
  54. }
  55. $timeZone = $this->getDefaultTimeZone();
  56. }
  57. try {
  58. return new \DateTimeZone($timeZone);
  59. } catch (\Exception $e) {
  60. \OCP\Util::writeLog('datetimezone', 'Failed to created DateTimeZone "' . $timeZone . "'", ILogger::DEBUG);
  61. return new \DateTimeZone($this->getDefaultTimeZone());
  62. }
  63. }
  64. /**
  65. * Guess the DateTimeZone for a given offset
  66. *
  67. * We first try to find a Etc/GMT* timezone, if that does not exist,
  68. * we try to find it manually, before falling back to UTC.
  69. *
  70. * @param mixed $offset
  71. * @param bool|int $timestamp
  72. * @return \DateTimeZone
  73. */
  74. protected function guessTimeZoneFromOffset($offset, $timestamp) {
  75. try {
  76. // Note: the timeZone name is the inverse to the offset,
  77. // so a positive offset means negative timeZone
  78. // and the other way around.
  79. if ($offset > 0) {
  80. $timeZone = 'Etc/GMT-' . $offset;
  81. } else {
  82. $timeZone = 'Etc/GMT+' . abs($offset);
  83. }
  84. return new \DateTimeZone($timeZone);
  85. } catch (\Exception $e) {
  86. // If the offset has no Etc/GMT* timezone,
  87. // we try to guess one timezone that has the same offset
  88. foreach (\DateTimeZone::listIdentifiers() as $timeZone) {
  89. $dtz = new \DateTimeZone($timeZone);
  90. $dateTime = new \DateTime();
  91. if ($timestamp !== false) {
  92. $dateTime->setTimestamp($timestamp);
  93. }
  94. $dtOffset = $dtz->getOffset($dateTime);
  95. if ($dtOffset == 3600 * $offset) {
  96. return $dtz;
  97. }
  98. }
  99. // No timezone found, fallback to UTC
  100. \OCP\Util::writeLog('datetimezone', 'Failed to find DateTimeZone for offset "' . $offset . "'", ILogger::DEBUG);
  101. return new \DateTimeZone($this->getDefaultTimeZone());
  102. }
  103. }
  104. /**
  105. * Get the default timezone of the server
  106. *
  107. * Falls back to UTC if it is not yet set.
  108. *
  109. * @return string
  110. */
  111. protected function getDefaultTimeZone() {
  112. $serverTimeZone = date_default_timezone_get();
  113. return $serverTimeZone ?: 'UTC';
  114. }
  115. }