# HG changeset patch # User Thierry Florac # Date 1267744587 -3600 # Node ID 393fad193b6b3bcf871a4b4bebdbe55e8c7bd0f8 # Parent 14084f98ac50fb5e423b32ac224a28e4ce819532 Moved timezone module to a sub-package with utility and methods to handle server timezone diff -r 14084f98ac50 -r 393fad193b6b ztfy/utils/configure.zcml --- a/ztfy/utils/configure.zcml Tue Mar 02 23:36:04 2010 +0100 +++ b/ztfy/utils/configure.zcml Fri Mar 05 00:16:27 2010 +0100 @@ -5,9 +5,7 @@ - - + diff -r 14084f98ac50 -r 393fad193b6b ztfy/utils/timezone.py --- a/ztfy/utils/timezone.py Tue Mar 02 23:36:04 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,62 +0,0 @@ -############################################################################## -# -# Copyright (c) 2008 Thierry Florac -# All Rights Reserved. -# -# This software is subject to the provisions of the Zope Public License, -# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. -# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED -# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS -# FOR A PARTICULAR PURPOSE. -# -############################################################################## -""" - -$Id: $ -""" - -__version__ = "$Revision: $" -__release__ = "$Id: $" -__docformat__ = "restructuredtext" - -# import standard packages -import pytz - -# import Zope3 interfaces -from zope.interface.common.idatetime import ITZInfo -from zope.publisher.interfaces.browser import IBrowserRequest - -# import local interfaces - -# import Zope3 packages -from zope.component import adapter -from zope.interface import implementer - -# import local packages - - -GMT = pytz.timezone('GMT') -_tz = pytz.timezone('Europe/Paris') -tz = _tz - -@implementer(ITZInfo) -@adapter(IBrowserRequest) -def tzinfo(request=None): - return tz - - -def tztime(value): - if not value: - return None - if not value.tzinfo: - value = GMT.localize(value) - return value.astimezone(tz) - - -def gmtime(value): - if not value: - return None - if not value.tzinfo: - value = GMT.localize(value) - return value.astimezone(GMT) diff -r 14084f98ac50 -r 393fad193b6b ztfy/utils/timezone/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ztfy/utils/timezone/__init__.py Fri Mar 05 00:16:27 2010 +0100 @@ -0,0 +1,62 @@ +### -*- coding: utf-8 -*- #################################################### +############################################################################## +# +# Copyright (c) 2008-2010 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## + +__docformat__ = "restructuredtext" + +# import standard packages +import pytz + +# import Zope3 interfaces +from zope.interface.common.idatetime import ITZInfo +from zope.publisher.interfaces.browser import IBrowserRequest + +# import local interfaces +from interfaces import IServerTimezone + +# import Zope3 packages +from zope.app import zapi +from zope.component import adapter +from zope.interface import implementer + +# import local packages + + +GMT = pytz.timezone('GMT') +_tz = pytz.timezone('Europe/Paris') +tz = _tz + +@implementer(ITZInfo) +@adapter(IBrowserRequest) +def tzinfo(request=None): + util = zapi.queryUtility(IServerTimezone) + if util is not None: + return pytz.timezone(util.timezone) + return GMT + + +def tztime(value): + if not value: + return None + if not value.tzinfo: + value = GMT.localize(value) + return value.astimezone(tzinfo()) + + +def gmtime(value): + if not value: + return None + if not value.tzinfo: + value = GMT.localize(value) + return value.astimezone(GMT) diff -r 14084f98ac50 -r 393fad193b6b ztfy/utils/timezone/configure.zcml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ztfy/utils/timezone/configure.zcml Fri Mar 05 00:16:27 2010 +0100 @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff -r 14084f98ac50 -r 393fad193b6b ztfy/utils/timezone/interfaces.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ztfy/utils/timezone/interfaces.py Fri Mar 05 00:16:27 2010 +0100 @@ -0,0 +1,37 @@ +### -*- coding: utf-8 -*- #################################################### +############################################################################## +# +# Copyright (c) 2008-2010 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## + +__docformat__ = "restructuredtext" + +# import standard packages + +# import Zope3 interfaces + +# import local interfaces + +# import Zope3 packages +from zope.interface import Interface + +# import local packages +from schema import Timezone + +from ztfy.utils import _ + + +class IServerTimezone(Interface): + + timezone = Timezone(title=_("Server timezone"), + description=_("Default server timezone"), + required=True) diff -r 14084f98ac50 -r 393fad193b6b ztfy/utils/timezone/schema.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ztfy/utils/timezone/schema.py Fri Mar 05 00:16:27 2010 +0100 @@ -0,0 +1,57 @@ +### -*- coding: utf-8 -*- #################################################### +############################################################################## +# +# Copyright (c) 2008-2010 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## + +__docformat__ = "restructuredtext" + +# import standard packages +import pytz + +# import Zope3 interfaces +from zope.schema.interfaces import IChoice, IVocabularyFactory + +# import local interfaces + +# import Zope3 packages +from zope.interface import implements, classProvides +from zope.schema import Choice +from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary + +# import local packages + + +class TimezonesVocabulary(SimpleVocabulary): + + classProvides(IVocabularyFactory) + + def __init__(self, *args, **kw): + terms = [SimpleTerm(t, t, t) for t in pytz.all_timezones] + super(TimezonesVocabulary, self).__init__(terms) + + +class ITimezone(IChoice): + """Marker interface for timezone field""" + + +class Timezone(Choice): + """Timezone choice field""" + + implements(ITimezone) + + def __init__(self, **kw): + if 'vocabulary' in kw: + kw.pop('vocabulary') + if 'default' not in kw: + kw['default'] = u'GMT' + super(Timezone, self).__init__(vocabulary='ZTFY timezones', **kw) diff -r 14084f98ac50 -r 393fad193b6b ztfy/utils/timezone/utility.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ztfy/utils/timezone/utility.py Fri Mar 05 00:16:27 2010 +0100 @@ -0,0 +1,39 @@ +### -*- coding: utf-8 -*- #################################################### +############################################################################## +# +# Copyright (c) 2008-2010 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## + +__docformat__ = "restructuredtext" + +# import standard packages +from persistent import Persistent + +# import Zope3 interfaces + +# import local interfaces +from interfaces import IServerTimezone + +# import Zope3 packages +from zope.interface import implements +from zope.schema.fieldproperty import FieldProperty + +# import local packages + +from ztfy.utils import _ + + +class ServerTimezoneUtility(Persistent): + + implements(IServerTimezone) + + timezone = FieldProperty(IServerTimezone['timezone'])