Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_strftime.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | b36cb4602e21 |
| children |
| rev | line source |
|---|---|
|
45814
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
1 """ |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
2 Unittest for time.strftime |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
3 """ |
|
4697
1f368fb3e1f7
Added test_strftime (Skip Montanaro).
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
45814
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
5 import calendar |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
6 import sys |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
7 import re |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
8 from test import test_support |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
9 import time |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
10 import unittest |
|
6047
96fecc80a28f
Moved some non-standard format characters out of the standard list.
Guido van Rossum <guido@python.org>
parents:
5989
diff
changeset
|
11 |
|
4697
1f368fb3e1f7
Added test_strftime (Skip Montanaro).
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
45814
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
13 # helper functions |
|
5174
c4e5ef9bc361
Much more rigorous testing -- we now try many times, varying in time
Guido van Rossum <guido@python.org>
parents:
5137
diff
changeset
|
14 def fixasctime(s): |
|
c4e5ef9bc361
Much more rigorous testing -- we now try many times, varying in time
Guido van Rossum <guido@python.org>
parents:
5137
diff
changeset
|
15 if s[8] == ' ': |
|
7747
aedd2d0dcb31
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6854
diff
changeset
|
16 s = s[:8] + '0' + s[9:] |
|
5174
c4e5ef9bc361
Much more rigorous testing -- we now try many times, varying in time
Guido van Rossum <guido@python.org>
parents:
5137
diff
changeset
|
17 return s |
|
c4e5ef9bc361
Much more rigorous testing -- we now try many times, varying in time
Guido van Rossum <guido@python.org>
parents:
5137
diff
changeset
|
18 |
|
45814
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
19 def escapestr(text, ampm): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
20 """ |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
21 Escape text to deal with possible locale values that have regex |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
22 syntax while allowing regex syntax used for comparison. |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
23 """ |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
24 new_text = re.escape(text) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
25 new_text = new_text.replace(re.escape(ampm), ampm) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
26 new_text = new_text.replace('\%', '%') |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
27 new_text = new_text.replace('\:', ':') |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
28 new_text = new_text.replace('\?', '?') |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
29 return new_text |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
30 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
31 class StrftimeTest(unittest.TestCase): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
32 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
33 def __init__(self, *k, **kw): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
34 unittest.TestCase.__init__(self, *k, **kw) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
35 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
36 def _update_variables(self, now): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
37 # we must update the local variables on every cycle |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
38 self.gmt = time.gmtime(now) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
39 now = time.localtime(now) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
40 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
41 if now[3] < 12: self.ampm='(AM|am)' |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
42 else: self.ampm='(PM|pm)' |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
43 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
44 self.jan1 = time.localtime(time.mktime((now[0], 1, 1, 0, 0, 0, 0, 1, 0))) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
45 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
46 try: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
47 if now[8]: self.tz = time.tzname[1] |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
48 else: self.tz = time.tzname[0] |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
49 except AttributeError: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
50 self.tz = '' |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
51 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
52 if now[3] > 12: self.clock12 = now[3] - 12 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
53 elif now[3] > 0: self.clock12 = now[3] |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
54 else: self.clock12 = 12 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
55 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
56 self.now = now |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
57 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
58 def setUp(self): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
59 try: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
60 import java |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
61 java.util.Locale.setDefault(java.util.Locale.US) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
62 except ImportError: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
63 import locale |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
64 locale.setlocale(locale.LC_TIME, 'C') |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
65 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
66 def test_strftime(self): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
67 now = time.time() |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
68 self._update_variables(now) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
69 self.strftest1(now) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
70 self.strftest2(now) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
71 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
72 if test_support.verbose: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
73 print "Strftime test, platform: %s, Python version: %s" % \ |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
74 (sys.platform, sys.version.split()[0]) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
75 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
76 for j in range(-5, 5): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
77 for i in range(25): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
78 arg = now + (i+j*100)*23*3603 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
79 self._update_variables(arg) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
80 self.strftest1(arg) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
81 self.strftest2(arg) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
82 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
83 def strftest1(self, now): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
84 if test_support.verbose: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
85 print "strftime test for", time.ctime(now) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
86 now = self.now |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
87 # Make sure any characters that could be taken as regex syntax is |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
88 # escaped in escapestr() |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
89 expectations = ( |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
90 ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
91 ('%A', calendar.day_name[now[6]], 'full weekday name'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
92 ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
93 ('%B', calendar.month_name[now[1]], 'full month name'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
94 # %c see below |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
95 ('%d', '%02d' % now[2], 'day of month as number (00-31)'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
96 ('%H', '%02d' % now[3], 'hour (00-23)'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
97 ('%I', '%02d' % self.clock12, 'hour (01-12)'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
98 ('%j', '%03d' % now[7], 'julian day (001-366)'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
99 ('%m', '%02d' % now[1], 'month as number (01-12)'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
100 ('%M', '%02d' % now[4], 'minute, (00-59)'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
101 ('%p', self.ampm, 'AM or PM as appropriate'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
102 ('%S', '%02d' % now[5], 'seconds of current time (00-60)'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
103 ('%U', '%02d' % ((now[7] + self.jan1[6])//7), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
104 'week number of the year (Sun 1st)'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
105 ('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
106 ('%W', '%02d' % ((now[7] + (self.jan1[6] - 1)%7)//7), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
107 'week number of the year (Mon 1st)'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
108 # %x see below |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
109 ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
110 ('%y', '%02d' % (now[0]%100), 'year without century'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
111 ('%Y', '%d' % now[0], 'year with century'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
112 # %Z see below |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
113 ('%%', '%', 'single percent sign'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
114 ) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
115 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
116 for e in expectations: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
117 # musn't raise a value error |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
118 try: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
119 result = time.strftime(e[0], now) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
120 except ValueError, error: |
|
59841
d1398ff01a07
Merged revisions 77593,77702-77703,77858,77887,78113-78115,78117,78245,78385-78386,78496,78760,78771-78773,78802 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45814
diff
changeset
|
121 self.fail("strftime '%s' format gave error: %s" % (e[0], error)) |
|
45814
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
122 if re.match(escapestr(e[1], self.ampm), result): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
123 continue |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
124 if not result or result[0] == '%': |
|
59841
d1398ff01a07
Merged revisions 77593,77702-77703,77858,77887,78113-78115,78117,78245,78385-78386,78496,78760,78771-78773,78802 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45814
diff
changeset
|
125 self.fail("strftime does not support standard '%s' format (%s)" |
|
d1398ff01a07
Merged revisions 77593,77702-77703,77858,77887,78113-78115,78117,78245,78385-78386,78496,78760,78771-78773,78802 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45814
diff
changeset
|
126 % (e[0], e[2])) |
|
45814
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
127 else: |
|
59841
d1398ff01a07
Merged revisions 77593,77702-77703,77858,77887,78113-78115,78117,78245,78385-78386,78496,78760,78771-78773,78802 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45814
diff
changeset
|
128 self.fail("Conflict for %s (%s): expected %s, but got %s" |
|
d1398ff01a07
Merged revisions 77593,77702-77703,77858,77887,78113-78115,78117,78245,78385-78386,78496,78760,78771-78773,78802 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45814
diff
changeset
|
129 % (e[0], e[2], e[1], result)) |
|
45814
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
130 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
131 def strftest2(self, now): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
132 nowsecs = str(long(now))[:-1] |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
133 now = self.now |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
134 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
135 nonstandard_expectations = ( |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
136 # These are standard but don't have predictable output |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
137 ('%c', fixasctime(time.asctime(now)), 'near-asctime() format'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
138 ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
139 '%m/%d/%y %H:%M:%S'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
140 ('%Z', '%s' % self.tz, 'time zone name'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
141 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
142 # These are some platform specific extensions |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
143 ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
144 ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
145 ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
146 ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
147 ('%n', '\n', 'newline character'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
148 ('%r', '%02d:%02d:%02d %s' % (self.clock12, now[4], now[5], self.ampm), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
149 '%I:%M:%S %p'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
150 ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
151 ('%s', nowsecs, 'seconds since the Epoch in UCT'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
152 ('%t', '\t', 'tab character'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
153 ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
154 ('%3y', '%03d' % (now[0]%100), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
155 'year without century rendered using fieldwidth'), |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
156 ) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
157 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
158 for e in nonstandard_expectations: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
159 try: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
160 result = time.strftime(e[0], now) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
161 except ValueError, result: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
162 msg = "Error for nonstandard '%s' format (%s): %s" % \ |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
163 (e[0], e[2], str(result)) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
164 if test_support.verbose: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
165 print msg |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
166 continue |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
167 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
168 if re.match(escapestr(e[1], self.ampm), result): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
169 if test_support.verbose: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
170 print "Supports nonstandard '%s' format (%s)" % (e[0], e[2]) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
171 elif not result or result[0] == '%': |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
172 if test_support.verbose: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
173 print "Does not appear to support '%s' format (%s)" % \ |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
174 (e[0], e[2]) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
175 else: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
176 if test_support.verbose: |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
177 print "Conflict for nonstandard '%s' format (%s):" % \ |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
178 (e[0], e[2]) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
179 print " Expected %s, but got %s" % (e[1], result) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
180 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
181 def test_main(): |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
182 test_support.run_unittest(StrftimeTest) |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
183 |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
184 if __name__ == '__main__': |
|
7f13d6904e14
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
185 test_main() |
