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 52class LocaltimeTests(unittest.TestCase): 53 54 def test_localtime_is_tz_aware_daylight_true(self): 55 test.support.patch(self, time, 'daylight', True) 56 t = utils.localtime() 57 self.assertIsNotNone(t.tzinfo) 58 59 def test_localtime_is_tz_aware_daylight_false(self): 60 test.support.patch(self, time, 'daylight', False) 61 t = utils.localtime() 62 self.assertIsNotNone(t.tzinfo) 63 64 def test_localtime_daylight_true_dst_false(self): 65 test.support.patch(self, time, 'daylight', True) 66 t0 = datetime.datetime(2012, 3, 12, 1, 1) 67 t1 = utils.localtime(t0, isdst=-1) 68 t2 = utils.localtime(t1) 69 self.assertEqual(t1, t2) 70 71 def test_localtime_daylight_false_dst_false(self): 72 test.support.patch(self, time, 'daylight', False) 73 t0 = datetime.datetime(2012, 3, 12, 1, 1) 74 t1 = utils.localtime(t0, isdst=-1) 75 t2 = utils.localtime(t1) 76 self.assertEqual(t1, t2) 77 78 @test.support.run_with_tz('Europe/Minsk') 79 def test_localtime_daylight_true_dst_true(self): 80 test.support.patch(self, time, 'daylight', True) 81 t0 = datetime.datetime(2012, 3, 12, 1, 1) 82 t1 = utils.localtime(t0, isdst=1) 83 t2 = utils.localtime(t1) 84 self.assertEqual(t1, t2) 85 86 @test.support.run_with_tz('Europe/Minsk') 87 def test_localtime_daylight_false_dst_true(self): 88 test.support.patch(self, time, 'daylight', False) 89 t0 = datetime.datetime(2012, 3, 12, 1, 1) 90 t1 = utils.localtime(t0, isdst=1) 91 t2 = utils.localtime(t1) 92 self.assertEqual(t1, t2) 93 94 @test.support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0') 95 def test_localtime_epoch_utc_daylight_true(self): 96 test.support.patch(self, time, 'daylight', True) 97 t0 = datetime.datetime(1990, 1, 1, tzinfo = datetime.timezone.utc) 98 t1 = utils.localtime(t0) 99 t2 = t0 - datetime.timedelta(hours=5) 100 t2 = t2.replace(tzinfo = datetime.timezone(datetime.timedelta(hours=-5))) 101 self.assertEqual(t1, t2) 102 103 @test.support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0') 104 def test_localtime_epoch_utc_daylight_false(self): 105 test.support.patch(self, time, 'daylight', False) 106 t0 = datetime.datetime(1990, 1, 1, tzinfo = datetime.timezone.utc) 107 t1 = utils.localtime(t0) 108 t2 = t0 - datetime.timedelta(hours=5) 109 t2 = t2.replace(tzinfo = datetime.timezone(datetime.timedelta(hours=-5))) 110 self.assertEqual(t1, t2) 111 112 def test_localtime_epoch_notz_daylight_true(self): 113 test.support.patch(self, time, 'daylight', True) 114 t0 = datetime.datetime(1990, 1, 1) 115 t1 = utils.localtime(t0) 116 t2 = utils.localtime(t0.replace(tzinfo=None)) 117 self.assertEqual(t1, t2) 118 119 def test_localtime_epoch_notz_daylight_false(self): 120 test.support.patch(self, time, 'daylight', False) 121 t0 = datetime.datetime(1990, 1, 1) 122 t1 = utils.localtime(t0) 123 t2 = utils.localtime(t0.replace(tzinfo=None)) 124 self.assertEqual(t1, t2) 125 126 # XXX: Need a more robust test for Olson's tzdata 127 @unittest.skipIf(sys.platform.startswith('win'), 128 "Windows does not use Olson's TZ database") 129 @unittest.skipUnless(os.path.exists('/usr/share/zoneinfo') or 130 os.path.exists('/usr/lib/zoneinfo'), 131 "Can't find the Olson's TZ database") 132 @test.support.run_with_tz('Europe/Kiev') 133 def test_variable_tzname(self): 134 t0 = datetime.datetime(1984, 1, 1, tzinfo=datetime.timezone.utc) 135 t1 = utils.localtime(t0) 136 self.assertEqual(t1.tzname(), 'MSK') 137 t0 = datetime.datetime(1994, 1, 1, tzinfo=datetime.timezone.utc) 138 t1 = utils.localtime(t0) 139 self.assertEqual(t1.tzname(), 'EET') 140 141# Issue #24836: The timezone files are out of date (pre 2011k) 142# on Mac OS X Snow Leopard. 143@test.support.requires_mac_ver(10, 7) 144class FormatDateTests(unittest.TestCase): 145 146 @test.support.run_with_tz('Europe/Minsk') 147 def test_formatdate(self): 148 timeval = time.mktime((2011, 12, 1, 18, 0, 0, 4, 335, 0)) 149 string = utils.formatdate(timeval, localtime=False, usegmt=False) 150 self.assertEqual(string, 'Thu, 01 Dec 2011 15:00:00 -0000') 151 string = utils.formatdate(timeval, localtime=False, usegmt=True) 152 self.assertEqual(string, 'Thu, 01 Dec 2011 15:00:00 GMT') 153 154 @test.support.run_with_tz('Europe/Minsk') 155 def test_formatdate_with_localtime(self): 156 timeval = time.mktime((2011, 1, 1, 18, 0, 0, 6, 1, 0)) 157 string = utils.formatdate(timeval, localtime=True) 158 self.assertEqual(string, 'Sat, 01 Jan 2011 18:00:00 +0200') 159 # Minsk moved from +0200 (with DST) to +0300 (without DST) in 2011 160 timeval = time.mktime((2011, 12, 1, 18, 0, 0, 4, 335, 0)) 161 string = utils.formatdate(timeval, localtime=True) 162 self.assertEqual(string, 'Thu, 01 Dec 2011 18:00:00 +0300') 163 164if __name__ == '__main__': 165 unittest.main() 166