blob: 0f00b1af52d24689153bc803f94fbe757f78b42f [file] [log] [blame]
Barry Warsaw04f357c2002-07-23 19:04:11 +00001from test import test_support
Barry Warsawb0c22321996-12-06 23:30:07 +00002import time
Fred Drakebc561982001-05-22 17:02:02 +00003import unittest
Senthil Kumaran792eb5d2011-04-06 14:27:47 +08004import sys
Barry Warsawb0c22321996-12-06 23:30:07 +00005
Barry Warsawb0c22321996-12-06 23:30:07 +00006
Fred Drakebc561982001-05-22 17:02:02 +00007class TimeTestCase(unittest.TestCase):
Barry Warsawb0c22321996-12-06 23:30:07 +00008
Fred Drakebc561982001-05-22 17:02:02 +00009 def setUp(self):
10 self.t = time.time()
Barry Warsawb0c22321996-12-06 23:30:07 +000011
Fred Drakebc561982001-05-22 17:02:02 +000012 def test_data_attributes(self):
13 time.altzone
14 time.daylight
15 time.timezone
16 time.tzname
Barry Warsawb0c22321996-12-06 23:30:07 +000017
Fred Drakebc561982001-05-22 17:02:02 +000018 def test_clock(self):
19 time.clock()
Barry Warsawb0c22321996-12-06 23:30:07 +000020
Fred Drakebc561982001-05-22 17:02:02 +000021 def test_conversions(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000022 self.assertTrue(time.ctime(self.t)
Fred Drakebc561982001-05-22 17:02:02 +000023 == time.asctime(time.localtime(self.t)))
Benjamin Peterson5c8da862009-06-30 22:57:08 +000024 self.assertTrue(long(time.mktime(time.localtime(self.t)))
Fred Drakebc561982001-05-22 17:02:02 +000025 == long(self.t))
26
27 def test_sleep(self):
28 time.sleep(1.2)
29
30 def test_strftime(self):
31 tt = time.gmtime(self.t)
32 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
33 'j', 'm', 'M', 'p', 'S',
34 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
35 format = ' %' + directive
36 try:
37 time.strftime(format, tt)
38 except ValueError:
39 self.fail('conversion specifier: %r failed.' % format)
40
Senthil Kumaran792eb5d2011-04-06 14:27:47 +080041 # Issue #10762: Guard against invalid/non-supported format string
42 # so that Python don't crash (Windows crashes when the format string
43 # input to [w]strftime is not kosher.
44 if sys.platform.startswith('win'):
45 with self.assertRaises(ValueError):
46 time.strftime('%f')
47
Brett Cannond1080a32004-03-02 04:38:10 +000048 def test_strftime_bounds_checking(self):
49 # Make sure that strftime() checks the bounds of the various parts
Brett Cannoncaebe222006-07-18 04:41:36 +000050 #of the time tuple (0 is valid for *all* values).
Brett Cannond1080a32004-03-02 04:38:10 +000051
Brett Cannoncaebe222006-07-18 04:41:36 +000052 # Check year [1900, max(int)]
Brett Cannond1080a32004-03-02 04:38:10 +000053 self.assertRaises(ValueError, time.strftime, '',
54 (1899, 1, 1, 0, 0, 0, 0, 1, -1))
55 if time.accept2dyear:
56 self.assertRaises(ValueError, time.strftime, '',
57 (-1, 1, 1, 0, 0, 0, 0, 1, -1))
58 self.assertRaises(ValueError, time.strftime, '',
59 (100, 1, 1, 0, 0, 0, 0, 1, -1))
Brett Cannoncaebe222006-07-18 04:41:36 +000060 # Check month [1, 12] + zero support
Brett Cannond1080a32004-03-02 04:38:10 +000061 self.assertRaises(ValueError, time.strftime, '',
Brett Cannoncaebe222006-07-18 04:41:36 +000062 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000063 self.assertRaises(ValueError, time.strftime, '',
64 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Brett Cannoncaebe222006-07-18 04:41:36 +000065 # Check day of month [1, 31] + zero support
Brett Cannond1080a32004-03-02 04:38:10 +000066 self.assertRaises(ValueError, time.strftime, '',
Brett Cannoncaebe222006-07-18 04:41:36 +000067 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000068 self.assertRaises(ValueError, time.strftime, '',
69 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Brett Cannoncaebe222006-07-18 04:41:36 +000070 # Check hour [0, 23]
Brett Cannond1080a32004-03-02 04:38:10 +000071 self.assertRaises(ValueError, time.strftime, '',
72 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
73 self.assertRaises(ValueError, time.strftime, '',
74 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Brett Cannoncaebe222006-07-18 04:41:36 +000075 # Check minute [0, 59]
Brett Cannond1080a32004-03-02 04:38:10 +000076 self.assertRaises(ValueError, time.strftime, '',
77 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
78 self.assertRaises(ValueError, time.strftime, '',
79 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Brett Cannoncaebe222006-07-18 04:41:36 +000080 # Check second [0, 61]
Brett Cannond1080a32004-03-02 04:38:10 +000081 self.assertRaises(ValueError, time.strftime, '',
82 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
83 # C99 only requires allowing for one leap second, but Python's docs say
84 # allow two leap seconds (0..61)
85 self.assertRaises(ValueError, time.strftime, '',
86 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
87 # No check for upper-bound day of week;
88 # value forced into range by a ``% 7`` calculation.
89 # Start check at -2 since gettmarg() increments value before taking
90 # modulo.
91 self.assertRaises(ValueError, time.strftime, '',
92 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Brett Cannoncaebe222006-07-18 04:41:36 +000093 # Check day of the year [1, 366] + zero support
Brett Cannond1080a32004-03-02 04:38:10 +000094 self.assertRaises(ValueError, time.strftime, '',
Brett Cannoncaebe222006-07-18 04:41:36 +000095 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000096 self.assertRaises(ValueError, time.strftime, '',
97 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000098
Brett Cannoncaebe222006-07-18 04:41:36 +000099 def test_default_values_for_zero(self):
100 # Make sure that using all zeros uses the proper default values.
101 # No test for daylight savings since strftime() does not change output
102 # based on its value.
103 expected = "2000 01 01 00 00 00 1 001"
104 result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
Ezio Melotti2623a372010-11-21 13:34:58 +0000105 self.assertEqual(expected, result)
Brett Cannoncaebe222006-07-18 04:41:36 +0000106
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000107 def test_strptime(self):
Brett Cannone0426012006-09-23 19:53:20 +0000108 # Should be able to go round-trip from strftime to strptime without
109 # throwing an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000110 tt = time.gmtime(self.t)
111 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
112 'j', 'm', 'M', 'p', 'S',
113 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
Brett Cannone0426012006-09-23 19:53:20 +0000114 format = '%' + directive
115 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000116 try:
Brett Cannone0426012006-09-23 19:53:20 +0000117 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000118 except ValueError:
Brett Cannone0426012006-09-23 19:53:20 +0000119 self.fail("conversion specifier %r failed with '%s' input." %
120 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000121
Fred Drakebc561982001-05-22 17:02:02 +0000122 def test_asctime(self):
123 time.asctime(time.gmtime(self.t))
124 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolsky8009e8e2011-01-02 23:23:54 +0000125 self.assertRaises(TypeError, time.asctime, ())
126 # XXX: Posix compiant asctime should refuse to convert
127 # year > 9999, but Linux implementation does not.
128 # self.assertRaises(ValueError, time.asctime,
129 # (12345, 1, 0, 0, 0, 0, 0, 0, 0))
130 # XXX: For now, just make sure we don't have a crash:
131 try:
Alexander Belopolsky70645a42011-01-03 17:52:03 +0000132 time.asctime((12345, 1, 1, 0, 0, 0, 0, 1, 0))
Alexander Belopolsky8009e8e2011-01-02 23:23:54 +0000133 except ValueError:
134 pass
Fred Drakebc561982001-05-22 17:02:02 +0000135
R. David Murrayfe3b4372010-12-14 01:28:59 +0000136 @unittest.skipIf(not hasattr(time, "tzset"),
137 "time module has no attribute tzset")
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000138 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000139
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000140 from os import environ
141
Tim Peters0eadaac2003-04-24 16:02:54 +0000142 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000143 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000144 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000145
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000146 # These formats are correct for 2002, and possibly future years
147 # This format is the 'standard' as documented at:
148 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
149 # They are also documented in the tzset(3) man page on most Unix
150 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000151 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000152 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
153 utc='UTC+0'
154
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000155 org_TZ = environ.get('TZ',None)
156 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000157 # Make sure we can switch to UTC time and results are correct
158 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000159 # Note that altzone is undefined in UTC, as there is no DST
160 environ['TZ'] = eastern
161 time.tzset()
162 environ['TZ'] = utc
163 time.tzset()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000164 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000165 time.gmtime(xmas2002), time.localtime(xmas2002)
166 )
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000167 self.assertEqual(time.daylight, 0)
168 self.assertEqual(time.timezone, 0)
169 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000170
171 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000172 environ['TZ'] = eastern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000173 time.tzset()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000174 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
175 self.assertEqual(time.tzname, ('EST', 'EDT'))
176 self.assertEqual(len(time.tzname), 2)
177 self.assertEqual(time.daylight, 1)
178 self.assertEqual(time.timezone, 18000)
179 self.assertEqual(time.altzone, 14400)
180 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
181 self.assertEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000182
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000183 # Now go to the southern hemisphere.
184 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000185 time.tzset()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000186 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
187 self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0]))
188 self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
189 self.assertEqual(len(time.tzname), 2)
190 self.assertEqual(time.daylight, 1)
191 self.assertEqual(time.timezone, -36000)
192 self.assertEqual(time.altzone, -39600)
193 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000194
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000195 finally:
196 # Repair TZ environment variable in case any other tests
197 # rely on it.
198 if org_TZ is not None:
199 environ['TZ'] = org_TZ
200 elif environ.has_key('TZ'):
201 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000202 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000203
Tim Peters1b6f7a92004-06-20 02:50:16 +0000204 def test_insane_timestamps(self):
205 # It's possible that some platform maps time_t to double,
206 # and that this test will fail there. This test should
207 # exempt such platforms (provided they return reasonable
208 # results!).
209 for func in time.ctime, time.gmtime, time.localtime:
210 for unreasonable in -1e200, 1e200:
211 self.assertRaises(ValueError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000212
Fred Drakef901abd2004-08-03 17:58:55 +0000213 def test_ctime_without_arg(self):
214 # Not sure how to check the values, since the clock could tick
215 # at any time. Make sure these are at least accepted and
216 # don't raise errors.
217 time.ctime()
218 time.ctime(None)
219
220 def test_gmtime_without_arg(self):
Neal Norwitzde7f5022006-06-15 05:55:49 +0000221 gt0 = time.gmtime()
222 gt1 = time.gmtime(None)
223 t0 = time.mktime(gt0)
224 t1 = time.mktime(gt1)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000225 self.assertTrue(0 <= (t1-t0) < 0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000226
227 def test_localtime_without_arg(self):
Neal Norwitzde7f5022006-06-15 05:55:49 +0000228 lt0 = time.localtime()
229 lt1 = time.localtime(None)
230 t0 = time.mktime(lt0)
231 t1 = time.mktime(lt1)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000232 self.assertTrue(0 <= (t1-t0) < 0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000233
Alexander Belopolsky6233b362011-02-15 15:51:17 +0000234 def test_mktime(self):
235 # Issue #1726687
236 for t in (-2, -1, 0, 1):
237 try:
238 tt = time.localtime(t)
239 except (OverflowError, ValueError):
240 pass
Alexander Belopolskyf9ad7d42011-02-15 16:01:11 +0000241 else:
242 self.assertEqual(time.mktime(tt), t)
Alexander Belopolsky6233b362011-02-15 15:51:17 +0000243
244
Fred Drake2e2be372001-09-20 21:33:42 +0000245def test_main():
246 test_support.run_unittest(TimeTestCase)
247
248
249if __name__ == "__main__":
250 test_main()