[Zope3-checkins] CVS: Zope3/src/datetime/tests - test_datetime.py:1.20
Tim Peters
tim.one@comcast.net
Tue, 21 Jan 2003 16:30:18 -0500
Update of /cvs-repository/Zope3/src/datetime/tests
In directory cvs.zope.org:/tmp/cvs-serv11591/src/datetime/tests
Modified Files:
test_datetime.py
Log Message:
Refactoring of, and new rules for, dt.astimezone(tz).
dt must be aware now, and tz.utcoffset() and tz.dst() must not return None.
The old dt.astimezone(None) no longer works to change an aware datetime
into a naive datetime; use dt.replace(tzinfo=None) instead.
The tzinfo base class now supplies a new fromutc(self, dt) method, and
datetime.astimezone(tz) invokes tz.fromutc(). The default implementation
of fromutc() reproduces the same results as the old astimezone()
implementation, but tzinfo subclasses can override fromutc() if the
default implementation isn't strong enough to get the correct results
in all cases (for example, this may be necessary if a tzinfo subclass
models a time zone whose "standard offset" (wrt UTC) changed in some
year(s), or in some variations of double-daylight time -- the creativity
of time zone politics can't be captured in a single default implementation).
=== Zope3/src/datetime/tests/test_datetime.py 1.19 => 1.20 ===
--- Zope3/src/datetime/tests/test_datetime.py:1.19 Mon Jan 20 17:13:04 2003
+++ Zope3/src/datetime/tests/test_datetime.py Tue Jan 21 16:30:15 2003
@@ -1311,20 +1311,27 @@
self.assertRaises(ValueError, base.replace, year=2001)
def test_astimezone(self):
- # Pretty boring! The TZ test is more interesting here.
+ # Pretty boring! The TZ test is more interesting here. astimezone()
+ # simply can't be applied to a naive object.
dt = self.theclass.now()
f = FixedOffset(44, "")
- for dtz in dt.astimezone(f), dt.astimezone(tz=f):
- self.failUnless(isinstance(dtz, datetime))
- self.assertEqual(dt.date(), dtz.date())
- self.assertEqual(dt.time(), dtz.time())
- self.failUnless(dtz.tzinfo is f)
- self.assertEqual(dtz.utcoffset(), timedelta(minutes=44))
-
self.assertRaises(TypeError, dt.astimezone) # not enough args
self.assertRaises(TypeError, dt.astimezone, f, f) # too many args
self.assertRaises(TypeError, dt.astimezone, dt) # arg wrong type
+ self.assertRaises(ValueError, dt.astimezone, f) # naive
+ self.assertRaises(ValueError, dt.astimezone, tz=f) # naive
+ class Bogus(tzinfo):
+ def utcoffset(self, dt): return None
+ def dst(self, dt): return timedelta(0)
+ bog = Bogus()
+ self.assertRaises(ValueError, dt.astimezone, bog) # naive
+
+ class AlsoBogus(tzinfo):
+ def utcoffset(self, dt): return timedelta(0)
+ def dst(self, dt): return None
+ alsobog = AlsoBogus()
+ self.assertRaises(ValueError, dt.astimezone, alsobog) # also naive
class TestTime(unittest.TestCase):
@@ -2441,17 +2448,11 @@
dt = self.theclass.now(tzinfo=f44m)
self.failUnless(dt.tzinfo is f44m)
- # Replacing with degenerate tzinfo doesn't do any adjustment.
- for x in dt.astimezone(fnone), dt.astimezone(tz=fnone):
- self.failUnless(x.tzinfo is fnone)
- self.assertEqual(x.date(), dt.date())
- self.assertEqual(x.time(), dt.time())
- # Ditt with None tz.
- x = dt.astimezone(tz=None)
- self.failUnless(x.tzinfo is None)
- self.assertEqual(x.date(), dt.date())
- self.assertEqual(x.time(), dt.time())
- # Ditto replacing with same tzinfo.
+ # Replacing with degenerate tzinfo raises an exception.
+ self.assertRaises(ValueError, dt.astimezone, fnone)
+ # Ditto with None tz.
+ self.assertRaises(TypeError, dt.astimezone, None)
+ # Replacing with same tzinfo makes no change.
x = dt.astimezone(dt.tzinfo)
self.failUnless(x.tzinfo is f44m)
self.assertEqual(x.date(), dt.date())
@@ -2601,7 +2602,7 @@
# Can't compare naive to aware objects, so strip the timezone from
# dt first.
- if start <= dt.astimezone(None) < end:
+ if start <= dt.replace(tzinfo=None) < end:
return HOUR
else:
return ZERO
@@ -2628,8 +2629,6 @@
# Conversion to our own timezone is always an identity.
self.assertEqual(dt.astimezone(tz), dt)
- # Conversion to None is always the same as stripping tzinfo.
- self.assertEqual(dt.astimezone(None), dt.replace(tzinfo=None))
asutc = dt.astimezone(utc)
there_and_back = asutc.astimezone(tz)
@@ -2682,8 +2681,11 @@
# Conversion to our own timezone is always an identity.
self.assertEqual(dt.astimezone(tz), dt)
- # Conversion to None is always the same as stripping tzinfo.
- self.assertEqual(dt.astimezone(None), dt.replace(tzinfo=None))
+
+ # Converting to UTC and back is an identity too.
+ asutc = dt.astimezone(utc)
+ there_and_back = asutc.astimezone(tz)
+ self.assertEqual(dt, there_and_back)
def convert_between_tz_and_utc(self, tz, utc):
dston = self.dston.replace(tzinfo=tz)
@@ -2735,7 +2737,7 @@
# 22:00 on day before daylight starts.
fourback = self.dston - timedelta(hours=4)
ninewest = FixedOffset(-9*60, "-0900", 0)
- fourback = fourback.astimezone(ninewest)
+ fourback = fourback.replace(tzinfo=ninewest)
# 22:00-0900 is 7:00 UTC == 2:00 EST == 3:00 DST. Since it's "after
# 2", we should get the 3 spelling.
# If we plug 22:00 the day before into Eastern, it "looks like std
@@ -2744,17 +2746,17 @@
# local clock jumps from 1 to 3). The point here is to make sure we
# get the 3 spelling.
expected = self.dston.replace(hour=3)
- got = fourback.astimezone(Eastern).astimezone(None)
+ got = fourback.astimezone(Eastern).replace(tzinfo=None)
self.assertEqual(expected, got)
# Similar, but map to 6:00 UTC == 1:00 EST == 2:00 DST. In that
# case we want the 1:00 spelling.
- sixutc = self.dston.replace(hour=6).astimezone(utc_real)
+ sixutc = self.dston.replace(hour=6, tzinfo=utc_real)
# Now 6:00 "looks like daylight", so the offset wrt Eastern is -4,
# and adding -4-0 == -4 gives the 2:00 spelling. We want the 1:00 EST
# spelling.
expected = self.dston.replace(hour=1)
- got = sixutc.astimezone(Eastern).astimezone(None)
+ got = sixutc.astimezone(Eastern).replace(tzinfo=None)
self.assertEqual(expected, got)
# Now on the day DST ends, we want "repeat an hour" behavior.