1
from test import support
6
class TimeTestCase(unittest.TestCase):
11
def test_data_attributes(self):
20
def test_conversions(self):
21
self.assert_(time.ctime(self.t)
22
== time.asctime(time.localtime(self.t)))
23
self.assert_(int(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))
90
# Check daylight savings flag [-1, 1]
91
self.assertRaises(ValueError, time.strftime, '',
92
(1900, 1, 1, 0, 0, 0, 0, 1, -2))
93
self.assertRaises(ValueError, time.strftime, '',
94
(1900, 1, 1, 0, 0, 0, 0, 1, 2))
96
def test_default_values_for_zero(self):
97
# Make sure that using all zeros uses the proper default values.
98
# No test for daylight savings since strftime() does not change output
100
expected = "2000 01 01 00 00 00 1 001"
101
result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
102
self.assertEquals(expected, result)
104
def test_strptime(self):
105
# Should be able to go round-trip from strftime to strptime without
106
# throwing an exception.
107
tt = time.gmtime(self.t)
108
for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
109
'j', 'm', 'M', 'p', 'S',
110
'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
111
format = '%' + directive
112
strf_output = time.strftime(format, tt)
114
time.strptime(strf_output, format)
116
self.fail("conversion specifier %r failed with '%s' input." %
117
(format, strf_output))
119
def test_asctime(self):
120
time.asctime(time.gmtime(self.t))
121
self.assertRaises(TypeError, time.asctime, 0)
123
def test_tzset(self):
124
if not hasattr(time, "tzset"):
125
return # Can't test this; don't want the test suite to fail
127
from os import environ
129
# Epoch time of midnight Dec 25th 2002. Never DST in northern
131
xmas2002 = 1040774400.0
133
# These formats are correct for 2002, and possibly future years
134
# This format is the 'standard' as documented at:
135
# http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
136
# They are also documented in the tzset(3) man page on most Unix
138
eastern = 'EST+05EDT,M4.1.0,M10.5.0'
139
victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
142
org_TZ = environ.get('TZ',None)
144
# Make sure we can switch to UTC time and results are correct
145
# Note that unknown timezones default to UTC.
146
# Note that altzone is undefined in UTC, as there is no DST
147
environ['TZ'] = eastern
151
self.failUnlessEqual(
152
time.gmtime(xmas2002), time.localtime(xmas2002)
154
self.failUnlessEqual(time.daylight, 0)
155
self.failUnlessEqual(time.timezone, 0)
156
self.failUnlessEqual(time.localtime(xmas2002).tm_isdst, 0)
158
# Make sure we can switch to US/Eastern
159
environ['TZ'] = eastern
161
self.failIfEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
162
self.failUnlessEqual(time.tzname, ('EST', 'EDT'))
163
self.failUnlessEqual(len(time.tzname), 2)
164
self.failUnlessEqual(time.daylight, 1)
165
self.failUnlessEqual(time.timezone, 18000)
166
self.failUnlessEqual(time.altzone, 14400)
167
self.failUnlessEqual(time.localtime(xmas2002).tm_isdst, 0)
168
self.failUnlessEqual(len(time.tzname), 2)
170
# Now go to the southern hemisphere.
171
environ['TZ'] = victoria
173
self.failIfEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
174
self.failUnless(time.tzname[0] == 'AEST', str(time.tzname[0]))
175
self.failUnless(time.tzname[1] == 'AEDT', str(time.tzname[1]))
176
self.failUnlessEqual(len(time.tzname), 2)
177
self.failUnlessEqual(time.daylight, 1)
178
self.failUnlessEqual(time.timezone, -36000)
179
self.failUnlessEqual(time.altzone, -39600)
180
self.failUnlessEqual(time.localtime(xmas2002).tm_isdst, 1)
183
# Repair TZ environment variable in case any other tests
185
if org_TZ is not None:
186
environ['TZ'] = org_TZ
187
elif 'TZ' in environ:
191
def test_insane_timestamps(self):
192
# It's possible that some platform maps time_t to double,
193
# and that this test will fail there. This test should
194
# exempt such platforms (provided they return reasonable
196
for func in time.ctime, time.gmtime, time.localtime:
197
for unreasonable in -1e200, 1e200:
198
self.assertRaises(ValueError, func, unreasonable)
200
def test_ctime_without_arg(self):
201
# Not sure how to check the values, since the clock could tick
202
# at any time. Make sure these are at least accepted and
203
# don't raise errors.
207
def test_gmtime_without_arg(self):
209
gt1 = time.gmtime(None)
210
t0 = time.mktime(gt0)
211
t1 = time.mktime(gt1)
212
self.assert_(0 <= (t1-t0) < 0.2)
214
def test_localtime_without_arg(self):
215
lt0 = time.localtime()
216
lt1 = time.localtime(None)
217
t0 = time.mktime(lt0)
218
t1 = time.mktime(lt1)
219
self.assert_(0 <= (t1-t0) < 0.2)
222
support.run_unittest(TimeTestCase)
225
if __name__ == "__main__":