[Zope3-checkins] CVS: Zope3/src/datetime/tests - test_datetime.py:1.1.2.4

Tim Peters tim.one@comcast.net
Wed, 25 Dec 2002 01:51:49 -0500


Update of /cvs-repository/Zope3/src/datetime/tests
In directory cvs.zope.org:/tmp/cvs-serv24006/src/datetime/tests

Modified Files:
      Tag: NameGeddon-branch
	test_datetime.py 
Log Message:
Implemented datetime.astimezone() and datetimetz.astimezone().  Cleared
some XXX comments.


=== Zope3/src/datetime/tests/test_datetime.py 1.1.2.3 => 1.1.2.4 ===
--- Zope3/src/datetime/tests/test_datetime.py:1.1.2.3	Tue Dec 24 21:19:56 2002
+++ Zope3/src/datetime/tests/test_datetime.py	Wed Dec 25 01:51:49 2002
@@ -1301,6 +1301,21 @@
         base = cls(2000, 2, 29)
         self.assertRaises(ValueError, base.replace, year=2001)
 
+    def test_astimezone(self):
+        # Pretty boring for a datetime!  datetimetz is more interesting here.
+        dt = self.theclass.now()
+        f = FixedOffset(44, "")
+        for dtz in dt.astimezone(f), dt.astimezone(tz=f):
+            self.failUnless(isinstance(dtz, datetimetz))
+            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
+
 
 class TestTime(unittest.TestCase):
 
@@ -2315,6 +2330,44 @@
         # Out of bounds.
         base = cls(2000, 2, 29)
         self.assertRaises(ValueError, base.replace, year=2001)
+
+    def test_more_astimezone(self):
+        # The inherited test_astimezone covered some trivial and error cases.
+        fnone = FixedOffset(None, "None")
+        f44m = FixedOffset(44, "44")
+        fm5h = FixedOffset(-timedelta(hours=5), "m300")
+
+        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.
+        x = dt.astimezone(dt.tzinfo)
+        self.failUnless(x.tzinfo is f44m)
+        self.assertEqual(x.date(), dt.date())
+        self.assertEqual(x.time(), dt.time())
+
+        # Replacing with different tzinfo does adjust.
+        got = dt.astimezone(fm5h)
+        self.failUnless(got.tzinfo is fm5h)
+        self.assertEqual(got.utcoffset(), timedelta(hours=-5))
+        expected = dt - dt.utcoffset()  # in effect, convert to UTC
+        expected += fm5h.utcoffset(dt)  # and from there to local time
+        expected = expected.replace(tzinfo=fm5h) # and attach new tzinfo
+        self.assertEqual(got.date(), expected.date())
+        self.assertEqual(got.time(), expected.time())
+        self.assertEqual(got.timetz(), expected.timetz())
+        self.failUnless(got.tzinfo is expected.tzinfo)
+        self.assertEqual(got, expected)
+
 
 def test_suite():
     allsuites = [unittest.makeSuite(klass, 'test')