Skip to content
This repository has been archived by the owner on Dec 18, 2020. It is now read-only.

Commit

Permalink
fixed handling of daylight savings in date.normalize, this should act…
Browse files Browse the repository at this point in the history
…ually be fixed in pytz
  • Loading branch information
dobe committed Apr 30, 2006
1 parent db14737 commit f875d51
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/zc/i18n/date.py
Expand Up @@ -31,8 +31,45 @@ def format(request, dt=None):
'dateTime', 'medium')
return formatter.format(dt)


def normalize(request, dt):

"""this method normalizes datetime instances by converting them to
utc, daylight saving times are also taken into account. This
method requires an adapter to get the tzinfo from the request.
>>> from zope import component, interface
>>> import pytz
>>> from zope.interface.common.idatetime import ITZInfo
>>> from zope.publisher.interfaces.browser import IBrowserRequest
>>> from zope.publisher.browser import TestRequest
>>> @interface.implementer(ITZInfo)
... @component.adapter(IBrowserRequest)
... def tzinfo(request):
... return pytz.timezone('Europe/Vienna')
>>> component.provideAdapter(tzinfo)
>>> dt = datetime.datetime(2006,5,1,12)
>>> request = TestRequest()
The Vienna timezone has a 2 hour offset to utc at this date.
>>> normalize(request,dt)
datetime.datetime(2006, 5, 1, 10, 0, tzinfo=<UTC>)
At this date the timezone has only a one hour offset.
>>> dt = datetime.datetime(2006,2,1,12)
>>> normalize(request,dt)
datetime.datetime(2006, 2, 1, 11, 0, tzinfo=<UTC>)
"""


if dt.tzinfo is None:
dt = dt.replace(tzinfo=ITZInfo(request))
tzinfo = ITZInfo(request)
else:
tzinfo = dt.tzinfo

# we have to do this because, pytz does currently not take the
# datetime argument into account in its dst() and utcoffset()
# methods

tzu = tzinfo.fromutc(dt).tzinfo
dt = dt.replace(tzinfo=tzu)
return dt.astimezone(pytz.utc)
1 change: 1 addition & 0 deletions src/zc/i18n/tests.py
Expand Up @@ -4,6 +4,7 @@
def test_suite():
return unittest.TestSuite((
doctest.DocFileSuite('duration.txt'),
doctest.DocTestSuite('zc.i18n.date')
))

if __name__ == '__main__':
Expand Down

0 comments on commit f875d51

Please sign in to comment.