[Checkins] SVN: zc.i18n/trunk/src/zc/i18n/ fixed handling of daylight savings in date.normalize, this should actually be fixed in pytz

Bernd Dorn bernd.dorn at fhv.at
Sun Apr 30 10:04:18 EDT 2006


Log message for revision 67763:
  fixed handling of daylight savings in date.normalize, this should actually be fixed in pytz

Changed:
  U   zc.i18n/trunk/src/zc/i18n/date.py
  U   zc.i18n/trunk/src/zc/i18n/tests.py

-=-
Modified: zc.i18n/trunk/src/zc/i18n/date.py
===================================================================
--- zc.i18n/trunk/src/zc/i18n/date.py	2006-04-30 13:56:45 UTC (rev 67762)
+++ zc.i18n/trunk/src/zc/i18n/date.py	2006-04-30 14:04:17 UTC (rev 67763)
@@ -31,8 +31,45 @@
         'dateTime', 'medium')
     return formatter.format(dt)
 
+def normalize(request, 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)

Modified: zc.i18n/trunk/src/zc/i18n/tests.py
===================================================================
--- zc.i18n/trunk/src/zc/i18n/tests.py	2006-04-30 13:56:45 UTC (rev 67762)
+++ zc.i18n/trunk/src/zc/i18n/tests.py	2006-04-30 14:04:17 UTC (rev 67763)
@@ -4,6 +4,7 @@
 def test_suite():
     return unittest.TestSuite((
         doctest.DocFileSuite('duration.txt'),
+        doctest.DocTestSuite('zc.i18n.date')
         ))
 
 if __name__ == '__main__':



More information about the Checkins mailing list