[CalendarServer-changes] [10141] CalendarServer/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Fri Dec 7 18:33:21 PST 2012
Revision: 10141
http://trac.calendarserver.org//changeset/10141
Author: sagen at apple.com
Date: 2012-12-07 18:33:21 -0800 (Fri, 07 Dec 2012)
Log Message:
-----------
Default timezone is automatically determined (on OS X), or can now be configured via plist
Modified Paths:
--------------
CalendarServer/trunk/calendarserver/webcal/resource.py
CalendarServer/trunk/twistedcaldav/stdconfig.py
Added Paths:
-----------
CalendarServer/trunk/calendarserver/platform/darwin/test_timezone.py
CalendarServer/trunk/calendarserver/platform/darwin/timezone.py
Added: CalendarServer/trunk/calendarserver/platform/darwin/test_timezone.py
===================================================================
--- CalendarServer/trunk/calendarserver/platform/darwin/test_timezone.py (rev 0)
+++ CalendarServer/trunk/calendarserver/platform/darwin/test_timezone.py 2012-12-08 02:33:21 UTC (rev 10141)
@@ -0,0 +1,66 @@
+##
+# Copyright (c) 2012 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from twistedcaldav.test.util import TestCase
+from twistedcaldav.config import config
+import calendarserver.platform.darwin.timezone
+import twistedcaldav.timezones
+from calendarserver.platform.darwin.timezone import getLocalTimezone, DEFAULT_TIMEZONE
+
+class DefaultTimezoneTests(TestCase):
+
+ def stubLookup(self):
+ return self._storedLookup
+
+ def stubHasTZ(self, ignored):
+ return self._storedHasTZ.pop()
+
+ def setUp(self):
+ self.patch(calendarserver.platform.darwin.timezone,
+ "lookupSystemTimezone", self.stubLookup)
+ self.patch(twistedcaldav.timezones,
+ "hasTZ", self.stubHasTZ)
+
+ def test_getLocalTimezone(self):
+
+ # Empty config, system timezone known = use system timezone
+ self.patch(config, "DefaultTimezone", "")
+ self._storedLookup = "America/New_York"
+ self._storedHasTZ = [True]
+ self.assertEquals(getLocalTimezone(), "America/New_York")
+
+ # Empty config, system timezone unknown = use DEFAULT_TIMEZONE
+ self.patch(config, "DefaultTimezone", "")
+ self._storedLookup = "Unknown/Unknown"
+ self._storedHasTZ = [False]
+ self.assertEquals(getLocalTimezone(), DEFAULT_TIMEZONE)
+
+ # Known config value = use config value
+ self.patch(config, "DefaultTimezone", "America/New_York")
+ self._storedHasTZ = [True]
+ self.assertEquals(getLocalTimezone(), "America/New_York")
+
+ # Unknown config value, system timezone known = use system timezone
+ self.patch(config, "DefaultTimezone", "Unknown/Unknown")
+ self._storedLookup = "America/New_York"
+ self._storedHasTZ = [True, False]
+ self.assertEquals(getLocalTimezone(), "America/New_York")
+
+ # Unknown config value, system timezone unknown = use DEFAULT_TIMEZONE
+ self.patch(config, "DefaultTimezone", "Unknown/Unknown")
+ self._storedLookup = "Unknown/Unknown"
+ self._storedHasTZ = [False, False]
+ self.assertEquals(getLocalTimezone(), DEFAULT_TIMEZONE)
Added: CalendarServer/trunk/calendarserver/platform/darwin/timezone.py
===================================================================
--- CalendarServer/trunk/calendarserver/platform/darwin/timezone.py (rev 0)
+++ CalendarServer/trunk/calendarserver/platform/darwin/timezone.py 2012-12-08 02:33:21 UTC (rev 10141)
@@ -0,0 +1,51 @@
+##
+# Copyright (c) 2012 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+
+from twistedcaldav.config import config
+import twistedcaldav.timezones
+
+DEFAULT_TIMEZONE = "America/Los_Angeles"
+
+try:
+ from Foundation import NSTimeZone
+ def lookupSystemTimezone():
+ return NSTimeZone.localTimeZone().name().encode("utf-8")
+
+except:
+ def lookupSystemTimezone():
+ return ""
+
+
+def getLocalTimezone():
+ """
+ Returns the default timezone for the server. The order of precedence is:
+ config.DefaultTimezone, lookupSystemTimezone( ), DEFAULT_TIMEZONE.
+ Also, if neither of the first two values in that list are in the timezone
+ database, DEFAULT_TIMEZONE is returned.
+
+ @return: The server's local timezone name
+ @rtype: C{str}
+ """
+ if config.DefaultTimezone:
+ if twistedcaldav.timezones.hasTZ(config.DefaultTimezone):
+ return config.DefaultTimezone
+
+ systemTimezone = lookupSystemTimezone()
+ if twistedcaldav.timezones.hasTZ(systemTimezone):
+ return systemTimezone
+
+ return DEFAULT_TIMEZONE
Modified: CalendarServer/trunk/calendarserver/webcal/resource.py
===================================================================
--- CalendarServer/trunk/calendarserver/webcal/resource.py 2012-12-08 01:22:16 UTC (rev 10140)
+++ CalendarServer/trunk/calendarserver/webcal/resource.py 2012-12-08 02:33:21 UTC (rev 10141)
@@ -40,7 +40,9 @@
from twisted.internet.defer import succeed
+from calendarserver.platform.darwin.timezone import getLocalTimezone
+
class WebCalendarResource (ReadOnlyResourceMixIn, DAVFile):
def defaultAccessControlList(self):
@@ -152,7 +154,8 @@
#
tzid = queryValue("tzid")
if not tzid:
- tzid = "America/Los_Angeles"
+ tzid = getLocalTimezone()
+ self.log_debug("Determined timezone to be %s" % (tzid,))
#
# Make some HTML
Modified: CalendarServer/trunk/twistedcaldav/stdconfig.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/stdconfig.py 2012-12-08 01:22:16 UTC (rev 10140)
+++ CalendarServer/trunk/twistedcaldav/stdconfig.py 2012-12-08 02:33:21 UTC (rev 10141)
@@ -989,6 +989,11 @@
# during migration
"MigratedInboxDaysCutoff": 60,
+ # The default timezone for the server; on OS X you can leave this empty and the
+ # system's timezone will be used. If empty and not on OS X it will default to
+ # America/Los_Angeles.
+ "DefaultTimezone" : "",
+
"Includes": [], # Other plists to parse after this one
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20121207/05606372/attachment-0001.html>
More information about the calendarserver-changes
mailing list