diff options
author | R David Murray <rdmurray@bitdance.com> | 2012-05-26 03:22:59 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2012-05-26 03:22:59 (GMT) |
commit | d2d521eafdea651de075f987ca80bece033b9b40 (patch) | |
tree | ffec4997db7f5c20cb879d5eac0877154e6e34ad /Lib/test/test_email | |
parent | dcaf2ece6cbd432f8ad71ca2bebc4f79e3b97fbc (diff) | |
download | cpython-d2d521eafdea651de075f987ca80bece033b9b40.zip cpython-d2d521eafdea651de075f987ca80bece033b9b40.tar.gz cpython-d2d521eafdea651de075f987ca80bece033b9b40.tar.bz2 |
#665194: Add a localtime function to email.utils.
Without this function people would be tempted to use the other date functions
in email.utils to compute an aware localtime, and those functions are not as
good for that purpose as this code. The code is Alexander Belopolsy's from
his proposed patch for issue 9527, with a fix (and additional tests) by Brian
K. Jones.
Diffstat (limited to 'Lib/test/test_email')
-rw-r--r-- | Lib/test/test_email/test_utils.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/Lib/test/test_email/test_utils.py b/Lib/test/test_email/test_utils.py index e003a64..d9c4d70 100644 --- a/Lib/test/test_email/test_utils.py +++ b/Lib/test/test_email/test_utils.py @@ -1,5 +1,7 @@ import datetime from email import utils +import test.support +import time import unittest class DateTimeTests(unittest.TestCase): @@ -43,3 +45,74 @@ class DateTimeTests(unittest.TestCase): self.assertEqual( utils.parsedate_to_datetime(self.datestring + ' -0000'), self.naive_dt) + + +class LocaltimeTests(unittest.TestCase): + + def test_localtime_is_tz_aware_daylight_true(self): + test.support.patch(self, time, 'daylight', True) + t = utils.localtime() + self.assertIsNot(t.tzinfo, None) + + def test_localtime_is_tz_aware_daylight_false(self): + test.support.patch(self, time, 'daylight', False) + t = utils.localtime() + self.assertIsNot(t.tzinfo, None) + + def test_localtime_daylight_true_dst_false(self): + test.support.patch(self, time, 'daylight', True) + t0 = datetime.datetime(2012, 3, 12, 1, 1) + t1 = utils.localtime(t0, isdst=-1) + t2 = utils.localtime(t1) + self.assertEqual(t1, t2) + + def test_localtime_daylight_false_dst_false(self): + test.support.patch(self, time, 'daylight', False) + t0 = datetime.datetime(2012, 3, 12, 1, 1) + t1 = utils.localtime(t0, isdst=-1) + t2 = utils.localtime(t1) + self.assertEqual(t1, t2) + + def test_localtime_daylight_true_dst_true(self): + test.support.patch(self, time, 'daylight', True) + t0 = datetime.datetime(2012, 3, 12, 1, 1) + t1 = utils.localtime(t0, isdst=1) + t2 = utils.localtime(t1) + self.assertEqual(t1, t2) + + def test_localtime_daylight_false_dst_true(self): + test.support.patch(self, time, 'daylight', False) + t0 = datetime.datetime(2012, 3, 12, 1, 1) + t1 = utils.localtime(t0, isdst=1) + t2 = utils.localtime(t1) + self.assertEqual(t1, t2) + + def test_localtime_epoch_utc_daylight_true(self): + test.support.patch(self, time, 'daylight', True) + t0 = datetime.datetime(1970, 1, 1, tzinfo = datetime.timezone.utc) + t1 = utils.localtime(t0) + self.assertEqual(t0, t1) + + def test_localtime_epoch_utc_daylight_false(self): + test.support.patch(self, time, 'daylight', False) + t0 = datetime.datetime(1970, 1, 1, tzinfo = datetime.timezone.utc) + t1 = utils.localtime(t0) + self.assertEqual(t0, t1) + + def test_localtime_epoch_notz_daylight_true(self): + test.support.patch(self, time, 'daylight', True) + t0 = datetime.datetime(1970, 1, 1) + t1 = utils.localtime(t0) + t2 = utils.localtime(t0.replace(tzinfo=None)) + self.assertEqual(t1, t2) + + def test_localtime_epoch_notz_daylight_false(self): + test.support.patch(self, time, 'daylight', False) + t0 = datetime.datetime(1970, 1, 1) + t1 = utils.localtime(t0) + t2 = utils.localtime(t0.replace(tzinfo=None)) + self.assertEqual(t1, t2) + + +if __name__ == '__main__': + unittest.main() |