summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2002-12-25 07:40:55 (GMT)
committerTim Peters <tim.peters@gmail.com>2002-12-25 07:40:55 (GMT)
commit80475bb4d21d1e5ddbb9eb0042adb1113052b38a (patch)
treea76d25afddde0b216df8832174d3866d008f5e9b /Lib
parent6578dc925ff0a7953ae50c7ac41a81b0bd54762e (diff)
downloadcpython-80475bb4d21d1e5ddbb9eb0042adb1113052b38a.zip
cpython-80475bb4d21d1e5ddbb9eb0042adb1113052b38a.tar.gz
cpython-80475bb4d21d1e5ddbb9eb0042adb1113052b38a.tar.bz2
Implemented datetime.astimezone() and datetimetz.astimezone().
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_datetime.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py
index 25c7716..de0d17a 100644
--- a/Lib/test/test_datetime.py
+++ b/Lib/test/test_datetime.py
@@ -1295,6 +1295,21 @@ class TestDateTime(TestDate):
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):
@@ -2308,6 +2323,44 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase):
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')
for klass in (TestModule,