src/ztfy/utils/timezone/__init__.py
branchZTK-1.1
changeset 148 d3668ecd9137
parent 73 96079b5bdc1f
child 214 91a1aa280326
equal deleted inserted replaced
147:044dc196ec8a 148:d3668ecd9137
       
     1 ### -*- coding: utf-8 -*- ####################################################
       
     2 ##############################################################################
       
     3 #
       
     4 # Copyright (c) 2008-2010 Thierry Florac <tflorac AT ulthar.net>
       
     5 # All Rights Reserved.
       
     6 #
       
     7 # This software is subject to the provisions of the Zope Public License,
       
     8 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
    10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    12 # FOR A PARTICULAR PURPOSE.
       
    13 #
       
    14 ##############################################################################
       
    15 
       
    16 __docformat__ = "restructuredtext"
       
    17 
       
    18 # import standard packages
       
    19 import pytz
       
    20 
       
    21 # import Zope3 interfaces
       
    22 from zope.interface.common.idatetime import ITZInfo
       
    23 from zope.publisher.interfaces.browser import IBrowserRequest
       
    24 
       
    25 # import local interfaces
       
    26 from ztfy.utils.timezone.interfaces import IServerTimezone
       
    27 
       
    28 # import Zope3 packages
       
    29 from zope.component import adapter, queryUtility
       
    30 from zope.interface import implementer
       
    31 
       
    32 # import local packages
       
    33 
       
    34 
       
    35 GMT = pytz.timezone('GMT')
       
    36 _tz = pytz.timezone('Europe/Paris')
       
    37 tz = _tz
       
    38 
       
    39 @implementer(ITZInfo)
       
    40 @adapter(IBrowserRequest)
       
    41 def tzinfo(request=None):
       
    42     util = queryUtility(IServerTimezone)
       
    43     if util is not None:
       
    44         return pytz.timezone(util.timezone)
       
    45     return GMT
       
    46 
       
    47 
       
    48 def tztime(value):
       
    49     if not value:
       
    50         return None
       
    51     if not value.tzinfo:
       
    52         value = GMT.localize(value)
       
    53     return value.astimezone(tzinfo())
       
    54 
       
    55 
       
    56 def gmtime(value):
       
    57     if not value:
       
    58         return None
       
    59     if not value.tzinfo:
       
    60         value = GMT.localize(value)
       
    61     return value.astimezone(GMT)