ztfy/utils/timezone/__init__.py
changeset 37 393fad193b6b
child 71 2512d7fe2cda
equal deleted inserted replaced
36:14084f98ac50 37:393fad193b6b
       
     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 interfaces import IServerTimezone
       
    27 
       
    28 # import Zope3 packages
       
    29 from zope.app import zapi
       
    30 from zope.component import adapter
       
    31 from zope.interface import implementer
       
    32 
       
    33 # import local packages
       
    34 
       
    35 
       
    36 GMT = pytz.timezone('GMT')
       
    37 _tz = pytz.timezone('Europe/Paris')
       
    38 tz = _tz
       
    39 
       
    40 @implementer(ITZInfo)
       
    41 @adapter(IBrowserRequest)
       
    42 def tzinfo(request=None):
       
    43     util = zapi.queryUtility(IServerTimezone)
       
    44     if util is not None:
       
    45         return pytz.timezone(util.timezone)
       
    46     return GMT
       
    47 
       
    48 
       
    49 def tztime(value):
       
    50     if not value:
       
    51         return None
       
    52     if not value.tzinfo:
       
    53         value = GMT.localize(value)
       
    54     return value.astimezone(tzinfo())
       
    55 
       
    56 
       
    57 def gmtime(value):
       
    58     if not value:
       
    59         return None
       
    60     if not value.tzinfo:
       
    61         value = GMT.localize(value)
       
    62     return value.astimezone(GMT)