[Zope-Checkins] SVN: Zope/branches-regebro-timezone-142148/lib/python/DateTime/ Experimental new timezone support.

Lennart Regebro regebro at gmail.com
Sun Mar 9 12:36:43 EDT 2008


Log message for revision 84555:
  Experimental new timezone support.
  

Changed:
  U   Zope/branches-regebro-timezone-142148/lib/python/DateTime/DateTime.py
  U   Zope/branches-regebro-timezone-142148/lib/python/DateTime/pytz.txt

-=-
Modified: Zope/branches-regebro-timezone-142148/lib/python/DateTime/DateTime.py
===================================================================
--- Zope/branches-regebro-timezone-142148/lib/python/DateTime/DateTime.py	2008-03-09 15:50:59 UTC (rev 84554)
+++ Zope/branches-regebro-timezone-142148/lib/python/DateTime/DateTime.py	2008-03-09 16:36:42 UTC (rev 84555)
@@ -21,6 +21,8 @@
 from interfaces import IDateTime
 from interfaces import DateTimeError, SyntaxError, DateError, TimeError
 from zope.interface import implements
+import os
+import pytz
 from pytz_support import PytzCache
 _cache = PytzCache
 
@@ -103,36 +105,45 @@
 
 
 def _findLocalTimeZoneName(isDST):
-    try:
-        from time import tzname
-    except:
-        tzname=('UNKNOWN','UNKNOWN')
+    # Old way was via time.tzname. That is a bad idea, as it has the same
+    # name for US/Eastern and Australia/Sydney, for example. New way: pytz.
+    TZ = os.environ.get('TZ', '/etc/timezone')
+    if TZ[0] == '/':
+        # A unix timezone file.
+        TZ = open(TZ).readline().strip()
+    tz = pytz.timezone(TZ)
+    return tz.zone
+    
+    #try:
+        #from time import tzname
+    #except:
+        #tzname=('UNKNOWN','UNKNOWN')
 
-    if not pytime.daylight:
-        # Daylight savings does not occur in this time zone.
-        isDST = 0
-    try:
-        # Get the name of the current time zone depending
-        # on DST.
-        _localzone = _cache._zmap[tzname[isDST].lower()]
-    except:
-        try:
-            # Generate a GMT-offset zone name.
-            if isDST:
-                localzone = pytime.altzone
-            else:
-                localzone = pytime.timezone
-            offset=(-localzone/(60*60.0))
-            majorOffset=int(offset)
-            if majorOffset != 0 :
-                minorOffset=abs(int((offset % majorOffset) * 60.0))
-            else: minorOffset = 0
-            m=majorOffset >= 0 and '+' or ''
-            lz='%s%0.02d%0.02d' % (m, majorOffset, minorOffset)
-            _localzone = _cache._zmap[('GMT%s' % lz).lower()]
-        except:
-            _localzone = ''
-    return _localzone
+    #if not pytime.daylight:
+        ## Daylight savings does not occur in this time zone.
+        #isDST = 0
+    #try:
+        ## Get the name of the current time zone depending
+        ## on DST.
+        #_localzone = _cache._zmap[tzname[isDST].lower()]
+    #except:
+        #try:
+            ## Generate a GMT-offset zone name.
+            #if isDST:
+                #localzone = pytime.altzone
+            #else:
+                #localzone = pytime.timezone
+            #offset=(-localzone/(60*60.0))
+            #majorOffset=int(offset)
+            #if majorOffset != 0 :
+                #minorOffset=abs(int((offset % majorOffset) * 60.0))
+            #else: minorOffset = 0
+            #m=majorOffset >= 0 and '+' or ''
+            #lz='%s%0.02d%0.02d' % (m, majorOffset, minorOffset)
+            #_localzone = _cache._zmap[('GMT%s' % lz).lower()]
+        #except:
+            #_localzone = ''
+    #return _localzone
 
 # Some utility functions for calculating dates:
 

Modified: Zope/branches-regebro-timezone-142148/lib/python/DateTime/pytz.txt
===================================================================
--- Zope/branches-regebro-timezone-142148/lib/python/DateTime/pytz.txt	2008-03-09 15:50:59 UTC (rev 84554)
+++ Zope/branches-regebro-timezone-142148/lib/python/DateTime/pytz.txt	2008-03-09 16:36:42 UTC (rev 84555)
@@ -199,12 +199,11 @@
 file (defaults to /etc/localtime). This is a bit tricky to test, so first of
 all we need to test that switching timezones works:
 
-    >>> import wingdbstub
     >>> import os
     >>> os.environ['TZ'] = 'Australia/Sydney'
     >>> import time
     >>> time.tzset()
     >>> DateTime._settz()
     >>> DT = DateTime(2008, 1, 1, 0, 0)
-    >>> DT.tzoffset()
-    36000
+    >>> DT._localzone0
+    'Australia/Sydney'



More information about the Zope-Checkins mailing list