annotate Lib/test/test_strftime.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 7f13d6904e14
children d1398ff01a07 70e115c34f97
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 os
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
8 import re
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
9 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
10 import time
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
11 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
12
4697
1f368fb3e1f7 Added test_strftime (Skip Montanaro).
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
45814
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
14 # 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
15 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
16 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
17 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
18 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
19
45814
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
20 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
21 """
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
22 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
23 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
24 """
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
25 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
26 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
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 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
30 return new_text
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
31
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
32 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
33
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
34 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
35 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
36
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
37 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
38 # 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
39 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
40 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
41
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
42 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
43 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
44
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
45 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
46
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
47 try:
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
48 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
49 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
50 except AttributeError:
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
51 self.tz = ''
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
52
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
53 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
54 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
55 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
56
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
57 self.now = now
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
58
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
59 def setUp(self):
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
60 try:
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
61 import java
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
62 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
63 except ImportError:
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
64 import locale
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
65 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
66
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
67 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
68 now = time.time()
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
69 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
70 self.strftest1(now)
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
71 self.strftest2(now)
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
72
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
73 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
74 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
75 (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
76
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
77 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
78 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
79 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
80 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
81 self.strftest1(arg)
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
82 self.strftest2(arg)
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
83
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
84 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
85 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
86 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
87 now = self.now
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
88 # 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
89 # escaped in escapestr()
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
90 expectations = (
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_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
92 ('%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
93 ('%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
94 ('%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
95 # %c see below
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
96 ('%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
97 ('%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
98 ('%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
99 ('%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
100 ('%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
101 ('%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
102 ('%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
103 ('%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
104 ('%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
105 '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
106 ('%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
107 ('%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
108 '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
109 # %x see below
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
110 ('%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
111 ('%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
112 ('%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
113 # %Z see below
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
114 ('%%', '%', 'single percent sign'),
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
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
117 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
118 # 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
119 try:
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
120 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
121 except ValueError, error:
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
122 print "Standard '%s' format gaver error:" % (e[0], error)
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 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
125 continue
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
126 if 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
127 print "Does not support standard '%s' format (%s)" % \
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
128 (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
129 else:
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
130 print "Conflict for %s (%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
131 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
132
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
133 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
134 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
135 now = self.now
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
136
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
137 nonstandard_expectations = (
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
138 # 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
139 ('%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
140 ('%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
141 '%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
142 ('%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
143
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
144 # 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
145 ('%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
146 ('%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
147 ('%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
148 ('%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
149 ('%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
150 ('%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
151 '%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
152 ('%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
153 ('%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
154 ('%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
155 ('%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
156 ('%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
157 '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
158 )
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
159
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
160 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
161 try:
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
162 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
163 except ValueError, result:
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
164 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
165 (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
166 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
167 print msg
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
168 continue
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
169
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
170 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
171 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
172 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
173 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
174 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
175 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
176 (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
177 else:
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
178 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
179 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
180 (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
181 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
182
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
183 def test_main():
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
184 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
185
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
186 if __name__ == '__main__':
7f13d6904e14 Convert test_strftime, test_getargs, and test_pep247 to use unittest.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
187 test_main()