[Checkins] SVN: zc.i18n/trunk/src/zc/i18n/date.py Fix from Ignas Mikalajunas:

Adam Groszer adamg at fw.hu
Tue Jul 25 09:59:57 EDT 2006


Log message for revision 69254:
  Fix from Ignas Mikalajunas:
  I looked into your problem with timezones, and here is the best
  solution i could come up with (it is mostly right, i just don't know how (whether) you want to handle non existent ambiguous time). Pytz pays attention to dt when calculating dst, look at documentation of DstTzInfo.localize also http://pytz.sourceforge.net/.
  
  Tuesday, July 25, 2006, 3:41:28 PM, Gary Poster wrote:
  For the record, by the way, I think raising an error with an  
  ambiguous time is ok for now; ideally we'd come up with some better  way to handle it in the UI (give the user options?  not sure) but  since the solution is not obvious, making it an error seems  
  reasonable for now.

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

-=-
Modified: zc.i18n/trunk/src/zc/i18n/date.py
===================================================================
--- zc.i18n/trunk/src/zc/i18n/date.py	2006-07-25 12:44:00 UTC (rev 69253)
+++ zc.i18n/trunk/src/zc/i18n/date.py	2006-07-25 13:59:56 UTC (rev 69254)
@@ -35,13 +35,13 @@
     """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
-    >>> requestTZ = pytz.timezone('Europe/Vienna')
     >>> from zope.interface.common.idatetime import ITZInfo
     >>> from zope.publisher.interfaces.browser import IBrowserRequest
     >>> from zope.publisher.browser import TestRequest
+    >>> requestTZ = pytz.timezone('Europe/Vienna')
     >>> @interface.implementer(ITZInfo)
     ... @component.adapter(IBrowserRequest)
     ... def tzinfo(request):
@@ -51,49 +51,51 @@
     >>> 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)    
+    >>> dt = datetime.datetime(2006,2,1,12)
+
     >>> normalize(request,dt)
     datetime.datetime(2006, 2, 1, 11, 0, tzinfo=<UTC>)
 
-    An explicit UTC tzinfo is not modified.
-    >>> dt = datetime.datetime(2006, 5, 1, 10, tzinfo=pytz.UTC)
-    >>> normalize(request, dt)
-    datetime.datetime(2006, 5, 1, 10, 0, tzinfo=<UTC>)
+    Normalizing UTC to UTC should work also
+    >>> dt = datetime.datetime(2006,5,1,12,tzinfo=pytz.UTC)
+    >>> normalize(request,dt)
+    datetime.datetime(2006, 5, 1, 12, 0, tzinfo=<UTC>)
 
-    Other explicit tzinfo values are honored, ignoring the ITZInfo adapter,
-    converting them to UTC.
-
-    >>> dt = datetime.datetime(2006, 5, 1, 12, tzinfo=pytz.timezone(
-    ...     'US/Eastern'))
-    >>> normalize(request, dt)
-    datetime.datetime(2006, 5, 1, 16, 0, tzinfo=<UTC>)
-    
-    Normalizing UTC to UTC should work if dt has no tzinfo and ITZInfo is UTC
+    This way too UTC to UTC
     >>> requestTZ = pytz.UTC
     >>> dt = datetime.datetime(2006,5,1,12)
     >>> normalize(request,dt)
     datetime.datetime(2006, 5, 1, 12, 0, tzinfo=<UTC>)
+
+    Just so you would know that these are possible -
+
+    The time that does not exist (defaulting to is_dst=False will raise an
+    index error in this case):
+
+    >>> requestTZ = pytz.timezone('Europe/Vilnius')
+    >>> dt = datetime.datetime(2006,3,26,3,30)
+    >>> normalize(request,dt)
+    Traceback (most recent call last):
+    ...
+    AmbiguousTimeError: 2006-03-26 03:30:00
+
+    An ambiguous time:
+
+    >>> dt = datetime.datetime(2006,10,29,3,30)
+    >>> normalize(request,dt)
+    Traceback (most recent call last):
+    ...
+    AmbiguousTimeError: 2006-10-29 03:30:00
+
     """
 
     if dt.tzinfo is None:
         tzinfo = ITZInfo(request)
-    else:
-        tzinfo = dt.tzinfo
-    
-    if tzinfo.utcoffset(None) == datetime.timedelta(0):
-        #the following fails if tzinfo == UTC, so skip it
-        #anyway converting a datetime from UTC to UTC is meaningless
-        #datetime.timedelta(0) is a better match for UTC
-        return dt.replace(tzinfo=pytz.UTC)
-    
-    # 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)
+        dt = tzinfo.localize(dt, is_dst=None)
+
+    return dt.astimezone(pytz.utc)
\ No newline at end of file



More information about the Checkins mailing list