summaryrefslogtreecommitdiffstats
path: root/lib/private/datetimezone.php
diff options
context:
space:
mode:
authorJoas Schilling <nickvergessen@gmx.de>2015-02-23 12:33:15 +0100
committerJoas Schilling <nickvergessen@owncloud.com>2015-03-03 09:08:47 +0100
commit276d67808013927c8777ffd92a2cbe09285f449f (patch)
treeb93f9ea225624e4a648076766e88afc6e7067a48 /lib/private/datetimezone.php
parente271fa3ee8b1ce13940a33ec7266cee345323e2d (diff)
downloadnextcloud-server-276d67808013927c8777ffd92a2cbe09285f449f.tar.gz
nextcloud-server-276d67808013927c8777ffd92a2cbe09285f449f.zip
Etc timezones don't exist for .5 and .75 offsets
Diffstat (limited to 'lib/private/datetimezone.php')
-rw-r--r--lib/private/datetimezone.php69
1 files changed, 58 insertions, 11 deletions
diff --git a/lib/private/datetimezone.php b/lib/private/datetimezone.php
index e4a0af4fc69..727ce321dba 100644
--- a/lib/private/datetimezone.php
+++ b/lib/private/datetimezone.php
@@ -44,19 +44,66 @@ class DateTimeZone implements IDateTimeZone {
$timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null);
if ($timeZone === null) {
if ($this->session->exists('timezone')) {
- $offsetHours = $this->session->get('timezone');
- // Note: the timeZone name is the inverse to the offset,
- // so a positive offset means negative timeZone
- // and the other way around.
- if ($offsetHours > 0) {
- return new \DateTimeZone('Etc/GMT-' . $offsetHours);
- } else {
- return new \DateTimeZone('Etc/GMT+' . abs($offsetHours));
- }
+ return $this->guessTimeZoneFromOffset($this->session->get('timezone'));
+ }
+ $timeZone = $this->getDefaultTimeZone();
+ }
+
+ try {
+ return new \DateTimeZone($timeZone);
+ } catch (\Exception $e) {
+ \OCP\Util::writeLog('datetimezone', 'Failed to created DateTimeZone "' . $timeZone . "'", \OCP\Util::DEBUG);
+ return new \DateTimeZone($this->getDefaultTimeZone());
+ }
+ }
+
+ /**
+ * Guess the DateTimeZone for a given offset
+ *
+ * We first try to find a Etc/GMT* timezone, if that does not exist,
+ * we try to find it manually, before falling back to UTC.
+ *
+ * @param mixed $offset
+ * @return \DateTimeZone
+ */
+ protected function guessTimeZoneFromOffset($offset) {
+ try {
+ // Note: the timeZone name is the inverse to the offset,
+ // so a positive offset means negative timeZone
+ // and the other way around.
+ if ($offset > 0) {
+ $timeZone = 'Etc/GMT-' . $offset;
} else {
- return new \DateTimeZone('UTC');
+ $timeZone = 'Etc/GMT+' . abs($offset);
}
+
+ return new \DateTimeZone($timeZone);
+ } catch (\Exception $e) {
+ // If the offset has no Etc/GMT* timezone,
+ // we try to guess one timezone that has the same offset
+ foreach (\DateTimeZone::listIdentifiers() as $timeZone) {
+ $dtz = new \DateTimeZone($timeZone);
+ $dtOffset = $dtz->getOffset(new \DateTime());
+ if ($dtOffset == 3600 * $offset) {
+ return $dtz;
+ }
+ }
+
+ // No timezone found, fallback to UTC
+ \OCP\Util::writeLog('datetimezone', 'Failed to find DateTimeZone for offset "' . $offset . "'", \OCP\Util::DEBUG);
+ return new \DateTimeZone($this->getDefaultTimeZone());
}
- return new \DateTimeZone($timeZone);
+ }
+
+ /**
+ * 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';
}
}