blob: a66b54ac976618ab0547991272b5d3aae45fa8ba [file] [log] [blame]
Barry Warsaw4d895fa2002-09-23 22:46:49 +00001"""PyUnit testing against strptime"""
Guido van Rossum00efe7e2002-07-19 17:04:46 +00002
Guido van Rossum00efe7e2002-07-19 17:04:46 +00003import unittest
4import time
5import locale
6import re
Guido van Rossuma5ce2e82002-08-22 19:57:50 +00007from test import test_support
Guido van Rossum00efe7e2002-07-19 17:04:46 +00008
9import _strptime
10
Guido van Rossum00efe7e2002-07-19 17:04:46 +000011class LocaleTime_Tests(unittest.TestCase):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +000012 """Tests for _strptime.LocaleTime."""
Guido van Rossum00efe7e2002-07-19 17:04:46 +000013
14 def setUp(self):
15 """Create time tuple based on current time."""
16 self.time_tuple = time.localtime()
17 self.LT_ins = _strptime.LocaleTime()
18
Martin v. Löwise16e01f2002-11-27 08:30:25 +000019 def compare_against_time(self, testing, directive, tuple_position,
20 error_msg):
Tim Peters469cdad2002-08-08 20:19:19 +000021 """Helper method that tests testing against directive based on the
Guido van Rossum00efe7e2002-07-19 17:04:46 +000022 tuple_position of time_tuple. Uses error_msg as error message.
23
24 """
25 strftime_output = time.strftime(directive, self.time_tuple)
26 comparison = testing[self.time_tuple[tuple_position]]
Martin v. Löwise16e01f2002-11-27 08:30:25 +000027 self.failUnless(strftime_output in testing, "%s: not found in tuple" %
28 error_msg)
29 self.failUnless(comparison == strftime_output,
30 "%s: position within tuple incorrect; %s != %s" %
31 (error_msg, comparison, strftime_output))
Tim Peters469cdad2002-08-08 20:19:19 +000032
Guido van Rossum00efe7e2002-07-19 17:04:46 +000033 def test_weekday(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +000034 # Make sure that full and abbreviated weekday names are correct in
35 # both string and position with tuple
Martin v. Löwise16e01f2002-11-27 08:30:25 +000036 self.compare_against_time(self.LT_ins.f_weekday, '%A', 6,
37 "Testing of full weekday name failed")
38 self.compare_against_time(self.LT_ins.a_weekday, '%a', 6,
39 "Testing of abbreviated weekday name failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +000040
41 def test_month(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +000042 # Test full and abbreviated month names; both string and position
43 # within the tuple
Martin v. Löwise16e01f2002-11-27 08:30:25 +000044 self.compare_against_time(self.LT_ins.f_month, '%B', 1,
45 "Testing against full month name failed")
46 self.compare_against_time(self.LT_ins.a_month, '%b', 1,
47 "Testing against abbreviated month name failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +000048
49 def test_am_pm(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +000050 # Make sure AM/PM representation done properly
Guido van Rossum00efe7e2002-07-19 17:04:46 +000051 strftime_output = time.strftime("%p", self.time_tuple)
Martin v. Löwise16e01f2002-11-27 08:30:25 +000052 self.failUnless(strftime_output in self.LT_ins.am_pm,
53 "AM/PM representation not in tuple")
Guido van Rossum00efe7e2002-07-19 17:04:46 +000054 if self.time_tuple[3] < 12: position = 0
55 else: position = 1
Martin v. Löwise16e01f2002-11-27 08:30:25 +000056 self.failUnless(strftime_output == self.LT_ins.am_pm[position],
57 "AM/PM representation in the wrong position within the tuple")
Guido van Rossum00efe7e2002-07-19 17:04:46 +000058
59 def test_timezone(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +000060 # Make sure timezone is correct
Guido van Rossum00efe7e2002-07-19 17:04:46 +000061 if time.strftime("%Z", self.time_tuple):
Martin v. Löwise16e01f2002-11-27 08:30:25 +000062 self.compare_against_time(self.LT_ins.timezone, '%Z', 8,
63 "Testing against timezone failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +000064
65 def test_date_time(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +000066 # Check that LC_date_time, LC_date, and LC_time are correct
Barry Warsaw4d895fa2002-09-23 22:46:49 +000067 # the magic date is used so as to not have issues with %c when day of
68 # the month is a single digit and has a leading space. This is not an
69 # issue since strptime still parses it correctly. The problem is
70 # testing these directives for correctness by comparing strftime
71 # output.
72 magic_date = (1999, 3, 17, 22, 44, 55, 2, 76, 0)
73 strftime_output = time.strftime("%c", magic_date)
Martin v. Löwise16e01f2002-11-27 08:30:25 +000074 self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date_time,
75 magic_date),
76 "LC_date_time incorrect")
Barry Warsaw4d895fa2002-09-23 22:46:49 +000077 strftime_output = time.strftime("%x", magic_date)
Martin v. Löwise16e01f2002-11-27 08:30:25 +000078 self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date,
79 magic_date),
80 "LC_date incorrect")
Barry Warsaw4d895fa2002-09-23 22:46:49 +000081 strftime_output = time.strftime("%X", magic_date)
Martin v. Löwise16e01f2002-11-27 08:30:25 +000082 self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_time,
83 magic_date),
84 "LC_time incorrect")
Barry Warsaw4d895fa2002-09-23 22:46:49 +000085 LT = _strptime.LocaleTime(am_pm=('',''))
86 self.failUnless(LT.LC_time, "LocaleTime's LC directives cannot handle "
87 "empty strings")
Guido van Rossum00efe7e2002-07-19 17:04:46 +000088
89 def test_lang(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +000090 # Make sure lang is set
Martin v. Löwise16e01f2002-11-27 08:30:25 +000091 self.failUnless(self.LT_ins.lang in (locale.getdefaultlocale()[0],
92 locale.getlocale(locale.LC_TIME),
93 ''),
94 "Setting of lang failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +000095
96 def test_by_hand_input(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +000097 # Test passed-in initialization value checks
Martin v. Löwise16e01f2002-11-27 08:30:25 +000098 self.failUnless(_strptime.LocaleTime(f_weekday=range(7)),
99 "Argument size check for f_weekday failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000100 self.assertRaises(TypeError, _strptime.LocaleTime, f_weekday=range(8))
101 self.assertRaises(TypeError, _strptime.LocaleTime, f_weekday=range(6))
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000102 self.failUnless(_strptime.LocaleTime(a_weekday=range(7)),
103 "Argument size check for a_weekday failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000104 self.assertRaises(TypeError, _strptime.LocaleTime, a_weekday=range(8))
105 self.assertRaises(TypeError, _strptime.LocaleTime, a_weekday=range(6))
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000106 self.failUnless(_strptime.LocaleTime(f_month=range(12)),
107 "Argument size check for f_month failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000108 self.assertRaises(TypeError, _strptime.LocaleTime, f_month=range(11))
109 self.assertRaises(TypeError, _strptime.LocaleTime, f_month=range(13))
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000110 self.failUnless(len(_strptime.LocaleTime(f_month=range(12)).f_month) == 13,
111 "dummy value for f_month not added")
112 self.failUnless(_strptime.LocaleTime(a_month=range(12)),
113 "Argument size check for a_month failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000114 self.assertRaises(TypeError, _strptime.LocaleTime, a_month=range(11))
115 self.assertRaises(TypeError, _strptime.LocaleTime, a_month=range(13))
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000116 self.failUnless(len(_strptime.LocaleTime(a_month=range(12)).a_month) == 13,
117 "dummy value for a_month not added")
118 self.failUnless(_strptime.LocaleTime(am_pm=range(2)),
119 "Argument size check for am_pm failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000120 self.assertRaises(TypeError, _strptime.LocaleTime, am_pm=range(1))
121 self.assertRaises(TypeError, _strptime.LocaleTime, am_pm=range(3))
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000122 self.failUnless(_strptime.LocaleTime(timezone=range(2)),
123 "Argument size check for timezone failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000124 self.assertRaises(TypeError, _strptime.LocaleTime, timezone=range(1))
125 self.assertRaises(TypeError, _strptime.LocaleTime, timezone=range(3))
126
127class TimeRETests(unittest.TestCase):
128 """Tests for TimeRE."""
129
130 def setUp(self):
131 """Construct generic TimeRE object."""
132 self.time_re = _strptime.TimeRE()
133 self.locale_time = _strptime.LocaleTime()
134
135 def test_getitem(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000136 # Make sure that __getitem__ works properly
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000137 self.failUnless(self.time_re['m'],
138 "Fetching 'm' directive (built-in) failed")
139 self.failUnless(self.time_re['b'],
140 "Fetching 'b' directive (built w/ __tupleToRE) failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000141 for name in self.locale_time.a_month:
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000142 self.failUnless(self.time_re['b'].find(name) != -1,
143 "Not all abbreviated month names in regex")
144 self.failUnless(self.time_re['c'],
145 "Fetching 'c' directive (built w/ format) failed")
146 self.failUnless(self.time_re['c'].find('%') == -1,
147 "Conversion of 'c' directive failed; '%' found")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000148 self.assertRaises(KeyError, self.time_re.__getitem__, '1')
149
150 def test_pattern(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000151 # Test TimeRE.pattern
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000152 pattern_string = self.time_re.pattern(r"%a %A %d")
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000153 self.failUnless(pattern_string.find(self.locale_time.a_weekday[2]) != -1,
154 "did not find abbreviated weekday in pattern string '%s'" %
155 pattern_string)
156 self.failUnless(pattern_string.find(self.locale_time.f_weekday[4]) != -1,
157 "did not find full weekday in pattern string '%s'" %
158 pattern_string)
159 self.failUnless(pattern_string.find(self.time_re['d']) != -1,
160 "did not find 'd' directive pattern string '%s'" %
161 pattern_string)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000162
163 def test_compile(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000164 # Check that compiled regex is correct
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000165 found = self.time_re.compile(r"%A").match(self.locale_time.f_weekday[6])
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000166 self.failUnless(found and found.group('A') == self.locale_time.f_weekday[6],
167 "re object for '%A' failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000168 compiled = self.time_re.compile(r"%a %b")
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000169 found = compiled.match("%s %s" % (self.locale_time.a_weekday[4],
170 self.locale_time.a_month[4]))
Tim Peters469cdad2002-08-08 20:19:19 +0000171 self.failUnless(found,
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000172 "Match failed with '%s' regex and '%s' string" %
173 (compiled.pattern, "%s %s" % (self.locale_time.a_weekday[4],
174 self.locale_time.a_month[4])))
175 self.failUnless(found.group('a') == self.locale_time.a_weekday[4] and
176 found.group('b') == self.locale_time.a_month[4],
177 "re object couldn't find the abbreviated weekday month in "
178 "'%s' using '%s'; group 'a' = '%s', group 'b' = %s'" %
179 (found.string, found.re.pattern, found.group('a'),
180 found.group('b')))
181 for directive in ('a','A','b','B','c','d','H','I','j','m','M','p','S',
182 'U','w','W','x','X','y','Y','Z','%'):
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000183 compiled = self.time_re.compile("%%%s"% directive)
184 found = compiled.match(time.strftime("%%%s" % directive))
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000185 self.failUnless(found, "Matching failed on '%s' using '%s' regex" %
186 (time.strftime("%%%s" % directive),
187 compiled.pattern))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000188
189class StrptimeTests(unittest.TestCase):
190 """Tests for _strptime.strptime."""
191
192 def setUp(self):
193 """Create testing time tuple."""
194 self.time_tuple = time.gmtime()
195
196 def test_TypeError(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000197 # Make sure ValueError is raised when match fails
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000198 self.assertRaises(ValueError, _strptime.strptime, data_string="%d",
199 format="%A")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000200
201 def test_returning_RE(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000202 # Make sure that an re can be returned
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000203 strp_output = _strptime.strptime(False, "%Y")
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000204 self.failUnless(isinstance(strp_output, type(re.compile(''))),
205 "re object not returned correctly")
206 self.failUnless(_strptime.strptime("1999", strp_output),
207 "Use of re object failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000208 bad_locale_time = _strptime.LocaleTime(lang="gibberish")
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000209 self.assertRaises(TypeError, _strptime.strptime, data_string='1999',
210 format=strp_output, locale_time=bad_locale_time)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000211
212 def helper(self, directive, position):
213 """Helper fxn in testing."""
214 strf_output = time.strftime("%%%s" % directive, self.time_tuple)
215 strp_output = _strptime.strptime(strf_output, "%%%s" % directive)
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000216 self.failUnless(strp_output[position] == self.time_tuple[position],
217 "testing of '%s' directive failed; '%s' -> %s != %s" %
218 (directive, strf_output, strp_output[position],
219 self.time_tuple[position]))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000220
221 def test_year(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000222 # Test that the year is handled properly
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000223 for directive in ('y', 'Y'):
224 self.helper(directive, 0)
225
226 def test_month(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000227 # Test for month directives
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000228 for directive in ('B', 'b', 'm'):
229 self.helper(directive, 1)
230
231 def test_day(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000232 # Test for day directives
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000233 self.helper('d', 2)
234
235 def test_hour(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000236 # Test hour directives
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000237 self.helper('H', 3)
238 strf_output = time.strftime("%I %p", self.time_tuple)
239 strp_output = _strptime.strptime(strf_output, "%I %p")
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000240 self.failUnless(strp_output[3] == self.time_tuple[3],
241 "testing of '%%I %%p' directive failed; '%s' -> %s != %s" %
242 (strf_output, strp_output[3], self.time_tuple[3]))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000243
244 def test_minute(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000245 # Test minute directives
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000246 self.helper('M', 4)
247
248 def test_second(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000249 # Test second directives
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000250 self.helper('S', 5)
251
252 def test_weekday(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000253 # Test weekday directives
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000254 for directive in ('A', 'a', 'w'):
255 self.helper(directive,6)
256
257 def test_julian(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000258 # Test julian directives
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000259 self.helper('j', 7)
260
261 def test_timezone(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000262 # Test timezone directives.
263 # When gmtime() is used with %Z, entire result of strftime() is empty.
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000264 # Check for equal timezone names deals with bad locale info when this
265 # occurs; first found in FreeBSD 4.4 -current
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000266 time_tuple = time.localtime()
267 strf_output = time.strftime("%Z") #UTC does not have a timezone
268 strp_output = _strptime.strptime(strf_output, "%Z")
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000269 locale_time = _strptime.LocaleTime()
270 if locale_time.timezone[0] != locale_time.timezone[1]:
271 self.failUnless(strp_output[8] == time_tuple[8],
272 "timezone check failed; '%s' -> %s != %s" %
273 (strf_output, strp_output[8], time_tuple[8]))
274 else:
275 self.failUnless(strp_output[8] == -1,
276 "LocaleTime().timezone has duplicate values but "
277 "timzone value not set to -1")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000278
279 def test_date_time(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000280 # Test %c directive
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000281 for position in range(6):
282 self.helper('c', position)
283
284 def test_date(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000285 # Test %x directive
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000286 for position in range(0,3):
287 self.helper('x', position)
288
289 def test_time(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000290 # Test %X directive
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000291 for position in range(3,6):
292 self.helper('X', position)
293
294 def test_percent(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000295 # Make sure % signs are handled properly
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000296 strf_output = time.strftime("%m %% %Y", self.time_tuple)
297 strp_output = _strptime.strptime(strf_output, "%m %% %Y")
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000298 self.failUnless(strp_output[0] == self.time_tuple[0] and
299 strp_output[1] == self.time_tuple[1],
300 "handling of percent sign failed")
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000301
Barry Warsaw4d895fa2002-09-23 22:46:49 +0000302 def test_caseinsensitive(self):
303 # Should handle names case-insensitively.
304 strf_output = time.strftime("%B", self.time_tuple)
305 self.failUnless(_strptime.strptime(strf_output.upper(), "%B"),
306 "strptime does not handle ALL-CAPS names properly")
307 self.failUnless(_strptime.strptime(strf_output.lower(), "%B"),
308 "strptime does not handle lowercase names properly")
309 self.failUnless(_strptime.strptime(strf_output.capitalize(), "%B"),
310 "strptime does not handle capword names properly")
311
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000312class FxnTests(unittest.TestCase):
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000313 """Test functions that fill in info by validating result and are triggered
314 properly."""
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000315
316 def setUp(self):
317 """Create an initial time tuple."""
318 self.time_tuple = time.gmtime()
319
320 def test_julianday_result(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000321 # Test julianday
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000322 result = _strptime.julianday(self.time_tuple[0], self.time_tuple[1],
323 self.time_tuple[2])
324 self.failUnless(result == self.time_tuple[7],
325 "julianday failed; %s != %s" %
326 (result, self.time_tuple[7]))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000327
328 def test_julianday_trigger(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000329 # Make sure julianday is called
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000330 strf_output = time.strftime("%Y-%m-%d", self.time_tuple)
331 strp_output = _strptime.strptime(strf_output, "%Y-%m-%d")
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000332 self.failUnless(strp_output[7] == self.time_tuple[7],
333 "strptime did not trigger julianday(); %s != %s" %
334 (strp_output[7], self.time_tuple[7]))
Tim Peters469cdad2002-08-08 20:19:19 +0000335
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000336 def test_gregorian_result(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000337 # Test gregorian
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000338 result = _strptime.gregorian(self.time_tuple[7], self.time_tuple[0])
339 comparison = [self.time_tuple[0], self.time_tuple[1], self.time_tuple[2]]
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000340 self.failUnless(result == comparison,
341 "gregorian() failed; %s != %s" % (result, comparison))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000342
343 def test_gregorian_trigger(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000344 # Test that gregorian() is triggered
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000345 strf_output = time.strftime("%j %Y", self.time_tuple)
346 strp_output = _strptime.strptime(strf_output, "%j %Y")
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000347 self.failUnless(strp_output[1] == self.time_tuple[1] and
348 strp_output[2] == self.time_tuple[2],
349 "gregorian() not triggered; month -- %s != %s, "
350 "day -- %s != %s" %
351 (strp_output[1], self.time_tuple[1], strp_output[2],
352 self.time_tuple[2]))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000353
354 def test_dayofweek_result(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000355 # Test dayofweek
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000356 result = _strptime.dayofweek(self.time_tuple[0], self.time_tuple[1],
357 self.time_tuple[2])
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000358 comparison = self.time_tuple[6]
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000359 self.failUnless(result == comparison,
360 "dayofweek() failed; %s != %s" % (result, comparison))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000361
362 def test_dayofweek_trigger(self):
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000363 # Make sure dayofweek() gets triggered
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000364 strf_output = time.strftime("%Y-%m-%d", self.time_tuple)
365 strp_output = _strptime.strptime(strf_output, "%Y-%m-%d")
Martin v. Löwise16e01f2002-11-27 08:30:25 +0000366 self.failUnless(strp_output[6] == self.time_tuple[6],
367 "triggering of dayofweek() failed; %s != %s" %
368 (strp_output[6], self.time_tuple[6]))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000369
370
Barry Warsaw375e0ee2002-08-29 15:25:04 +0000371class Strptime12AMPMTests(unittest.TestCase):
372 """Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
373
374 def test_twelve_noon_midnight(self):
375 eq = self.assertEqual
376 eq(time.strptime('12 PM', '%I %p')[3], 12)
377 eq(time.strptime('12 AM', '%I %p')[3], 0)
378 eq(_strptime.strptime('12 PM', '%I %p')[3], 12)
379 eq(_strptime.strptime('12 AM', '%I %p')[3], 0)
380
381
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000382def test_main():
383 suite = unittest.TestSuite()
384 suite.addTest(unittest.makeSuite(LocaleTime_Tests))
385 suite.addTest(unittest.makeSuite(TimeRETests))
386 suite.addTest(unittest.makeSuite(StrptimeTests))
387 suite.addTest(unittest.makeSuite(FxnTests))
Barry Warsaw375e0ee2002-08-29 15:25:04 +0000388 suite.addTest(unittest.makeSuite(Strptime12AMPMTests))
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000389 test_support.run_suite(suite)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000390
391
392if __name__ == '__main__':
Guido van Rossuma5ce2e82002-08-22 19:57:50 +0000393 test_main()