DateTimeZone.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\ISession;
  27. class DateTimeZone implements IDateTimeZone {
  28. /** @var IConfig */
  29. protected $config;
  30. /** @var ISession */
  31. protected $session;
  32. /**
  33. * Constructor
  34. *
  35. * @param IConfig $config
  36. * @param ISession $session
  37. */
  38. public function __construct(IConfig $config, ISession $session) {
  39. $this->config = $config;
  40. $this->session = $session;
  41. }
  42. /**
  43. * Get the timezone of the current user, based on his session information and config data
  44. *
  45. * @param bool|int $timestamp
  46. * @return \DateTimeZone
  47. */
  48. public function getTimeZone($timestamp = false) {
  49. $timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null);
  50. if ($timeZone === null) {
  51. if ($this->session->exists('timezone')) {
  52. return $this->guessTimeZoneFromOffset($this->session->get('timezone'), $timestamp);
  53. }
  54. $timeZone = $this->getDefaultTimeZone();
  55. }
  56. try {
  57. return new \DateTimeZone($timeZone);
  58. } catch (\Exception $e) {
  59. \OCP\Util::writeLog('datetimezone', 'Failed to created DateTimeZone "' . $timeZone . "'", \OCP\Util::DEBUG);
  60. return new \DateTimeZone($this->getDefaultTimeZone());
  61. }
  62. }
  63. /**
  64. * Guess the DateTimeZone for a given offset
  65. *
  66. * We first try to find a Etc/GMT* timezone, if that does not exist,
  67. * we try to find it manually, before falling back to UTC.
  68. *
  69. * @param mixed $offset
  70. * @param bool|int $timestamp
  71. * @return \DateTimeZone
  72. */
  73. protected function guessTimeZoneFromOffset($offset, $timestamp) {
  74. try {
  75. // Note: the timeZone name is the inverse to the offset,
  76. // so a positive offset means negative timeZone
  77. // and the other way around.
  78. if ($offset > 0) {
  79. $timeZone = 'Etc/GMT-' . $offset;
  80. } else {
  81. $timeZone = 'Etc/GMT+' . abs($offset);
  82. }
  83. return new \DateTimeZone($timeZone);
  84. } catch (\Exception $e) {
  85. // If the offset has no Etc/GMT* timezone,
  86. // we try to guess one timezone that has the same offset
  87. foreach (\DateTimeZone::listIdentifiers() as $timeZone) {
  88. $dtz = new \DateTimeZone($timeZone);
  89. $dateTime = new \DateTime();
  90. if ($timestamp !== false) {
  91. $dateTime->setTimestamp($timestamp);
  92. }
  93. $dtOffset = $dtz->getOffset($dateTime);
  94. if ($dtOffset == 3600 * $offset) {
  95. return $dtz;
  96. }
  97. }
  98. // No timezone found, fallback to UTC
  99. \OCP\Util::writeLog('datetimezone', 'Failed to find DateTimeZone for offset "' . $offset . "'", \OCP\Util::DEBUG);
  100. return new \DateTimeZone($this->getDefaultTimeZone());
  101. }
  102. }
  103. /**
  104. * Get the default timezone of the server
  105. *
  106. * Falls back to UTC if it is not yet set.
  107. *
  108. * @return string
  109. */
  110. protected function getDefaultTimeZone() {
  111. $serverTimeZone = date_default_timezone_get();
  112. return $serverTimeZone ?: 'UTC';
  113. }
  114. }