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