1 from test
import test_support
6 class TimeTestCase(unittest
.TestCase
):
11 def test_data_attributes(self
):
20 def test_conversions(self
):
21 self
.assertTrue(time
.ctime(self
.t
)
22 == time
.asctime(time
.localtime(self
.t
)))
23 self
.assertTrue(long(time
.mktime(time
.localtime(self
.t
)))
29 def test_strftime(self
):
30 tt
= time
.gmtime(self
.t
)
31 for directive
in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
32 'j', 'm', 'M', 'p', 'S',
33 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
34 format
= ' %' + directive
36 time
.strftime(format
, tt
)
38 self
.fail('conversion specifier: %r failed.' % format
)
40 def test_strftime_bounds_checking(self
):
41 # Make sure that strftime() checks the bounds of the various parts
42 #of the time tuple (0 is valid for *all* values).
44 # Check year [1900, max(int)]
45 self
.assertRaises(ValueError, time
.strftime
, '',
46 (1899, 1, 1, 0, 0, 0, 0, 1, -1))
48 self
.assertRaises(ValueError, time
.strftime
, '',
49 (-1, 1, 1, 0, 0, 0, 0, 1, -1))
50 self
.assertRaises(ValueError, time
.strftime
, '',
51 (100, 1, 1, 0, 0, 0, 0, 1, -1))
52 # Check month [1, 12] + zero support
53 self
.assertRaises(ValueError, time
.strftime
, '',
54 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
55 self
.assertRaises(ValueError, time
.strftime
, '',
56 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
57 # Check day of month [1, 31] + zero support
58 self
.assertRaises(ValueError, time
.strftime
, '',
59 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
60 self
.assertRaises(ValueError, time
.strftime
, '',
61 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
63 self
.assertRaises(ValueError, time
.strftime
, '',
64 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
65 self
.assertRaises(ValueError, time
.strftime
, '',
66 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
67 # Check minute [0, 59]
68 self
.assertRaises(ValueError, time
.strftime
, '',
69 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
70 self
.assertRaises(ValueError, time
.strftime
, '',
71 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
72 # Check second [0, 61]
73 self
.assertRaises(ValueError, time
.strftime
, '',
74 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
75 # C99 only requires allowing for one leap second, but Python's docs say
76 # allow two leap seconds (0..61)
77 self
.assertRaises(ValueError, time
.strftime
, '',
78 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
79 # No check for upper-bound day of week;
80 # value forced into range by a ``% 7`` calculation.
81 # Start check at -2 since gettmarg() increments value before taking
83 self
.assertRaises(ValueError, time
.strftime
, '',
84 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
85 # Check day of the year [1, 366] + zero support
86 self
.assertRaises(ValueError, time
.strftime
, '',
87 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
88 self
.assertRaises(ValueError, time
.strftime
, '',
89 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
91 def test_default_values_for_zero(self
):
92 # Make sure that using all zeros uses the proper default values.
93 # No test for daylight savings since strftime() does not change output
95 expected
= "2000 01 01 00 00 00 1 001"
96 result
= time
.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
97 self
.assertEquals(expected
, result
)
99 def test_strptime(self
):
100 # Should be able to go round-trip from strftime to strptime without
101 # throwing an exception.
102 tt
= time
.gmtime(self
.t
)
103 for directive
in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
104 'j', 'm', 'M', 'p', 'S',
105 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
106 format
= '%' + directive
107 strf_output
= time
.strftime(format
, tt
)
109 time
.strptime(strf_output
, format
)
111 self
.fail("conversion specifier %r failed with '%s' input." %
112 (format
, strf_output
))
114 def test_asctime(self
):
115 time
.asctime(time
.gmtime(self
.t
))
116 self
.assertRaises(TypeError, time
.asctime
, 0)
118 def test_tzset(self
):
119 if not hasattr(time
, "tzset"):
120 return # Can't test this; don't want the test suite to fail
122 from os
import environ
124 # Epoch time of midnight Dec 25th 2002. Never DST in northern
126 xmas2002
= 1040774400.0
128 # These formats are correct for 2002, and possibly future years
129 # This format is the 'standard' as documented at:
130 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
131 # They are also documented in the tzset(3) man page on most Unix
133 eastern
= 'EST+05EDT,M4.1.0,M10.5.0'
134 victoria
= 'AEST-10AEDT-11,M10.5.0,M3.5.0'
137 org_TZ
= environ
.get('TZ',None)
139 # Make sure we can switch to UTC time and results are correct
140 # Note that unknown timezones default to UTC.
141 # Note that altzone is undefined in UTC, as there is no DST
142 environ
['TZ'] = eastern
147 time
.gmtime(xmas2002
), time
.localtime(xmas2002
)
149 self
.assertEqual(time
.daylight
, 0)
150 self
.assertEqual(time
.timezone
, 0)
151 self
.assertEqual(time
.localtime(xmas2002
).tm_isdst
, 0)
153 # Make sure we can switch to US/Eastern
154 environ
['TZ'] = eastern
156 self
.assertNotEqual(time
.gmtime(xmas2002
), time
.localtime(xmas2002
))
157 self
.assertEqual(time
.tzname
, ('EST', 'EDT'))
158 self
.assertEqual(len(time
.tzname
), 2)
159 self
.assertEqual(time
.daylight
, 1)
160 self
.assertEqual(time
.timezone
, 18000)
161 self
.assertEqual(time
.altzone
, 14400)
162 self
.assertEqual(time
.localtime(xmas2002
).tm_isdst
, 0)
163 self
.assertEqual(len(time
.tzname
), 2)
165 # Now go to the southern hemisphere.
166 environ
['TZ'] = victoria
168 self
.assertNotEqual(time
.gmtime(xmas2002
), time
.localtime(xmas2002
))
169 self
.assertTrue(time
.tzname
[0] == 'AEST', str(time
.tzname
[0]))
170 self
.assertTrue(time
.tzname
[1] == 'AEDT', str(time
.tzname
[1]))
171 self
.assertEqual(len(time
.tzname
), 2)
172 self
.assertEqual(time
.daylight
, 1)
173 self
.assertEqual(time
.timezone
, -36000)
174 self
.assertEqual(time
.altzone
, -39600)
175 self
.assertEqual(time
.localtime(xmas2002
).tm_isdst
, 1)
178 # Repair TZ environment variable in case any other tests
180 if org_TZ
is not None:
181 environ
['TZ'] = org_TZ
182 elif environ
.has_key('TZ'):
186 def test_insane_timestamps(self
):
187 # It's possible that some platform maps time_t to double,
188 # and that this test will fail there. This test should
189 # exempt such platforms (provided they return reasonable
191 for func
in time
.ctime
, time
.gmtime
, time
.localtime
:
192 for unreasonable
in -1e200
, 1e200
:
193 self
.assertRaises(ValueError, func
, unreasonable
)
195 def test_ctime_without_arg(self
):
196 # Not sure how to check the values, since the clock could tick
197 # at any time. Make sure these are at least accepted and
198 # don't raise errors.
202 def test_gmtime_without_arg(self
):
204 gt1
= time
.gmtime(None)
205 t0
= time
.mktime(gt0
)
206 t1
= time
.mktime(gt1
)
207 self
.assertTrue(0 <= (t1
-t0
) < 0.2)
209 def test_localtime_without_arg(self
):
210 lt0
= time
.localtime()
211 lt1
= time
.localtime(None)
212 t0
= time
.mktime(lt0
)
213 t1
= time
.mktime(lt1
)
214 self
.assertTrue(0 <= (t1
-t0
) < 0.2)
217 test_support
.run_unittest(TimeTestCase
)
220 if __name__
== "__main__":