1import datetime 2from email import utils 3import test.support 4import time 5import unittest 6import sys 7import os.path 8 9class DateTimeTests(unittest.TestCase): 10 11 datestring = 'Sun, 23 Sep 2001 20:10:55' 12 dateargs = (2001, 9, 23, 20, 10, 55) 13 offsetstring = ' -0700' 14 utcoffset = datetime.timedelta(hours=-7) 15 tz = datetime.timezone(utcoffset) 16 naive_dt = datetime.datetime(*dateargs) 17 aware_dt = datetime.datetime(*dateargs, tzinfo=tz) 18 19 def test_naive_datetime(self): 20 self.assertEqual(utils.format_datetime(self.naive_dt), 21 self.datestring + ' -0000') 22 23 def test_aware_datetime(self): 24 self.assertEqual(utils.format_datetime(self.aware_dt), 25 self.datestring + self.offsetstring) 26 27 def test_usegmt(self): 28 utc_dt = datetime.datetime(*self.dateargs, 29 tzinfo=datetime.timezone.utc) 30 self.assertEqual(utils.format_datetime(utc_dt, usegmt=True), 31 self.datestring + ' GMT') 32 33 def test_usegmt_with_naive_datetime_raises(self): 34 with self.assertRaises(ValueError): 35 utils.format_datetime(self.naive_dt, usegmt=True) 36 37 def test_usegmt_with_non_utc_datetime_raises(self): 38 with self.assertRaises(ValueError): 39 utils.format_datetime(self.aware_dt, usegmt=True) 40 41 def test_parsedate_to_datetime(self): 42 self.assertEqual( 43 utils.parsedate_to_datetime(self.datestring + self.offsetstring), 44 self.aware_dt) 45 46 def test_parsedate_to_datetime_naive(self): 47 self.assertEqual( 48 utils.parsedate_to_datetime(self.datestring + ' -0000'), 49 self.naive_dt) 50 51 def test_parsedate_to_datetime_with_invalid_raises_valueerror(self): 52 invalid_dates = ['', 53 '0', 54 'A Complete Waste of Time' 55 'Tue, 06 Jun 2017 27:39:33 +0600', 56 'Tue, 06 Jun 2017 07:39:33 +2600', 57 'Tue, 06 Jun 2017 27:39:33'] 58 for dtstr in invalid_dates: 59 with self.subTest(dtstr=dtstr): 60 self.assertRaises(ValueError, utils.parsedate_to_datetime, dtstr) 61 62class LocaltimeTests(unittest.TestCase): 63 64 def test_localtime_is_tz_aware_daylight_true(self): 65 test.support.patch(self, time, 'daylight', True) 66 t = utils.localtime() 67 self.assertIsNotNone(t.tzinfo) 68 69 def test_localtime_is_tz_aware_daylight_false(self): 70 test.support.patch(self, time, 'daylight', False) 71 t = utils.localtime() 72 self.assertIsNotNone(t.tzinfo) 73 74 def test_localtime_daylight_true_dst_false(self): 75 test.support.patch(self, time, 'daylight', True) 76 t0 = datetime.datetime(2012, 3, 12, 1, 1) 77 t1 = utils.localtime(t0, isdst=-1) 78 t2 = utils.localtime(t1) 79 self.assertEqual(t1, t2) 80 81 def test_localtime_daylight_false_dst_false(self): 82 test.support.patch(self, time, 'daylight', False) 83 t0 = datetime.datetime(2012, 3, 12, 1, 1) 84 t1 = utils.localtime(t0, isdst=-1) 85 t2 = utils.localtime(t1) 86 self.assertEqual(t1, t2) 87 88 @test.support.run_with_tz('Europe/Minsk') 89 def test_localtime_daylight_true_dst_true(self): 90 test.support.patch(self, time, 'daylight', True) 91 t0 = datetime.datetime(2012, 3, 12, 1, 1) 92 t1 = utils.localtime(t0, isdst=1) 93 t2 = utils.localtime(t1) 94 self.assertEqual(t1, t2) 95 96 @test.support.run_with_tz('Europe/Minsk') 97 def test_localtime_daylight_false_dst_true(self): 98 test.support.patch(self, time, 'daylight', False) 99 t0 = datetime.datetime(2012, 3, 12, 1, 1) 100 t1 = utils.localtime(t0, isdst=1) 101 t2 = utils.localtime(t1) 102 self.assertEqual(t1, t2) 103 104 @test.support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0') 105 def test_localtime_epoch_utc_daylight_true(self): 106 test.support.patch(self, time, 'daylight', True) 107 t0 = datetime.datetime(1990, 1, 1, tzinfo = datetime.timezone.utc) 108 t1 = utils.localtime(t0) 109 t2 = t0 - datetime.timedelta(hours=5) 110 t2 = t2.replace(tzinfo = datetime.timezone(datetime.timedelta(hours=-5))) 111 self.assertEqual(t1, t2) 112 113 @test.support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0') 114 def test_localtime_epoch_utc_daylight_false(self): 115 test.support.patch(self, time, 'daylight', False) 116 t0 = datetime.datetime(1990, 1, 1, tzinfo = datetime.timezone.utc) 117 t1 = utils.localtime(t0) 118 t2 = t0 - datetime.timedelta(hours=5) 119 t2 = t2.replace(tzinfo = datetime.timezone(datetime.timedelta(hours=-5))) 120 self.assertEqual(t1, t2) 121 122 def test_localtime_epoch_notz_daylight_true(self): 123 test.support.patch(self, time, 'daylight', True) 124 t0 = datetime.datetime(1990, 1, 1) 125 t1 = utils.localtime(t0) 126 t2 = utils.localtime(t0.replace(tzinfo=None)) 127 self.assertEqual(t1, t2) 128 129 def test_localtime_epoch_notz_daylight_false(self): 130 test.support.patch(self, time, 'daylight', False) 131 t0 = datetime.datetime(1990, 1, 1) 132 t1 = utils.localtime(t0) 133 t2 = utils.localtime(t0.replace(tzinfo=None)) 134 self.assertEqual(t1, t2) 135 136 # XXX: Need a more robust test for Olson's tzdata 137 @unittest.skipIf(sys.platform.startswith('win'), 138 "Windows does not use Olson's TZ database") 139 @unittest.skipUnless(os.path.exists('/usr/share/zoneinfo') or 140 os.path.exists('/usr/lib/zoneinfo'), 141 "Can't find the Olson's TZ database") 142 @test.support.run_with_tz('Europe/Kiev') 143 def test_variable_tzname(self): 144 t0 = datetime.datetime(1984, 1, 1, tzinfo=datetime.timezone.utc) 145 t1 = utils.localtime(t0) 146 self.assertEqual(t1.tzname(), 'MSK') 147 t0 = datetime.datetime(1994, 1, 1, tzinfo=datetime.timezone.utc) 148 t1 = utils.localtime(t0) 149 self.assertEqual(t1.tzname(), 'EET') 150 151# Issue #24836: The timezone files are out of date (pre 2011k) 152# on Mac OS X Snow Leopard. 153@test.support.requires_mac_ver(10, 7) 154class FormatDateTests(unittest.TestCase): 155 156 @test.support.run_with_tz('Europe/Minsk') 157 def test_formatdate(self): 158 timeval = time.mktime((2011, 12, 1, 18, 0, 0, 4, 335, 0)) 159 string = utils.formatdate(timeval, localtime=False, usegmt=False) 160 self.assertEqual(string, 'Thu, 01 Dec 2011 15:00:00 -0000') 161 string = utils.formatdate(timeval, localtime=False, usegmt=True) 162 self.assertEqual(string, 'Thu, 01 Dec 2011 15:00:00 GMT') 163 164 @test.support.run_with_tz('Europe/Minsk') 165 def test_formatdate_with_localtime(self): 166 timeval = time.mktime((2011, 1, 1, 18, 0, 0, 6, 1, 0)) 167 string = utils.formatdate(timeval, localtime=True) 168 self.assertEqual(string, 'Sat, 01 Jan 2011 18:00:00 +0200') 169 # Minsk moved from +0200 (with DST) to +0300 (without DST) in 2011 170 timeval = time.mktime((2011, 12, 1, 18, 0, 0, 4, 335, 0)) 171 string = utils.formatdate(timeval, localtime=True) 172 self.assertEqual(string, 'Thu, 01 Dec 2011 18:00:00 +0300') 173 174if __name__ == '__main__': 175 unittest.main() 176