[Zope-Checkins]
SVN: Zope/branches-regebro-timezone-142148/lib/python/DateTime/
Changing the local timeone now is possible during testing,
so we can test the local timezone errors.
Lennart Regebro
regebro at gmail.com
Sun Mar 9 11:51:16 EDT 2008
Log message for revision 84554:
Changing the local timeone now is possible during testing, so we can test the local timezone errors.
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 12:00:54 UTC (rev 84553)
+++ Zope/branches-regebro-timezone-142148/lib/python/DateTime/DateTime.py 2008-03-09 15:50:59 UTC (rev 84554)
@@ -15,9 +15,8 @@
__version__='$Revision: 1.99 $'[11:-2]
-import re, math, DateTimeZone
-from time import time, gmtime, localtime
-from time import daylight, timezone, altzone, strftime
+import re, math
+import time as pytime
from datetime import datetime
from interfaces import IDateTime
from interfaces import DateTimeError, SyntaxError, DateError, TimeError
@@ -39,23 +38,17 @@
else:
return default_datefmt
-
-try:
- from time import tzname
-except:
- tzname=('UNKNOWN','UNKNOWN')
-
# To control rounding errors, we round system time to the nearest
# microsecond. Then delicate calculations can rely on that the
# maximum precision that needs to be preserved is known.
-_system_time = time
+_system_time = pytime.time
def time():
return round(_system_time(), 6)
# Determine machine epoch
tm=((0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334),
(0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335))
-yr,mo,dy,hr,mn,sc=gmtime(0)[:6]
+yr,mo,dy,hr,mn,sc=pytime.gmtime(0)[:6]
i=int(yr-1)
to_year =int(i*365+i/4-i/100+i/400-693960.0)
to_month=tm[yr%4==0 and (yr%100!=0 or yr%400==0)][mo]
@@ -110,7 +103,12 @@
def _findLocalTimeZoneName(isDST):
- if not daylight:
+ 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:
@@ -121,9 +119,9 @@
try:
# Generate a GMT-offset zone name.
if isDST:
- localzone = altzone
+ localzone = pytime.altzone
else:
- localzone = timezone
+ localzone = pytime.timezone
offset=(-localzone/(60*60.0))
majorOffset=int(offset)
if majorOffset != 0 :
@@ -259,7 +257,7 @@
t_int = int(t)
if isinstance(t_int, long):
raise OverflowError # Python 2.3 fix: int can return a long!
- return gmtime(t_int)
+ return pytime.gmtime(t_int)
except (ValueError, OverflowError):
raise TimeError, 'The time %f is beyond the range ' \
'of this Python implementation.' % float(t)
@@ -270,7 +268,7 @@
t_int = int(t)
if isinstance(t_int, long):
raise OverflowError # Python 2.3 fix: int can return a long!
- return localtime(t_int)
+ return pytime.localtime(t_int)
except (ValueError, OverflowError):
raise TimeError, 'The time %f is beyond the range ' \
'of this Python implementation.' % float(t)
@@ -795,13 +793,17 @@
'friday': 6, 'fri': 6,
'saturday': 7, 'sat': 7}
- _localzone0 = _findLocalTimeZoneName(0)
- _localzone1 = _findLocalTimeZoneName(1)
- _multipleZones = (_localzone0 != _localzone1)
- # For backward compatibility only:
- _isDST = localtime(time())[8]
- _localzone = _isDST and _localzone1 or _localzone0
_tzinfo = PytzCache()
+
+ @classmethod
+ def _settz(cls):
+ #import pdb;pdb.set_trace()
+ cls._localzone0 = _findLocalTimeZoneName(0)
+ cls._localzone1 = _findLocalTimeZoneName(1)
+ cls._multipleZones = (cls._localzone0 != cls._localzone1)
+ # For backward compatibility only:
+ cls._isDST = pytime.localtime(time())[8]
+ cls._localzone = cls._isDST and cls._localzone1 or cls._localzone0
def localZone(self, ltm=None):
'''Returns the time zone on the given date. The time zone
@@ -809,7 +811,7 @@
if not DateTime._multipleZones:
return DateTime._localzone0
if ltm == None:
- ltm = localtime(time())
+ ltm = pytime.localtime(time())
isDST = ltm[8]
lz = isDST and DateTime._localzone1 or DateTime._localzone0
return lz
@@ -988,7 +990,7 @@
if day is None:
# Use today's date.
- year,month,day = localtime(time())[:3]
+ year,month,day = pytime.localtime(time())[:3]
year = _correctYear(year)
if year < 1000: raise SyntaxError, st
@@ -1837,6 +1839,7 @@
out.write(self.ISO8601())
out.write('</dateTime.iso8601></value>\n')
+DateTime._settz()
class strftimeFormatter:
Modified: Zope/branches-regebro-timezone-142148/lib/python/DateTime/pytz.txt
===================================================================
--- Zope/branches-regebro-timezone-142148/lib/python/DateTime/pytz.txt 2008-03-09 12:00:54 UTC (rev 84553)
+++ Zope/branches-regebro-timezone-142148/lib/python/DateTime/pytz.txt 2008-03-09 15:50:59 UTC (rev 84554)
@@ -185,3 +185,26 @@
>>> tz.info() in ((-21600, 1, 'MDT'), (-25200, 0, 'MST'))
True
+
+Ambigous time zone names (bug #142148)
+--------------------------------------
+Some timezone abbrevieations, like EST are ambigous. This can mean both
+US/Eastern, Australia/Eastern and several timezones in south america,
+completely different places, with different offsets, and wore, different
+daylight saving rules. Figuring out the local timezone from time.tzname will
+therefore fail if you are in for example Australia/Eastern, as EST will be
+assumed to mean US/Eastern.
+
+So, on Unix the local time zone info needs to be figured out from a timezone
+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
More information about the Zope-Checkins
mailing list