ยปCore Development>Code coverage>Lib/test/test_strftime.py

Python code coverage for Lib/test/test_strftime.py

#countcontent
1n/a"""
2n/aUnittest for time.strftime
3n/a"""
4n/a
5n/aimport calendar
6n/aimport sys
7n/aimport re
8n/afrom test import support
9n/aimport time
10n/aimport unittest
11n/a
12n/a
13n/a# helper functions
14n/adef fixasctime(s):
15n/a if s[8] == ' ':
16n/a s = s[:8] + '0' + s[9:]
17n/a return s
18n/a
19n/adef escapestr(text, ampm):
20n/a """
21n/a Escape text to deal with possible locale values that have regex
22n/a syntax while allowing regex syntax used for comparison.
23n/a """
24n/a new_text = re.escape(text)
25n/a new_text = new_text.replace(re.escape(ampm), ampm)
26n/a new_text = new_text.replace(r'\%', '%')
27n/a new_text = new_text.replace(r'\:', ':')
28n/a new_text = new_text.replace(r'\?', '?')
29n/a return new_text
30n/a
31n/a
32n/aclass StrftimeTest(unittest.TestCase):
33n/a
34n/a def _update_variables(self, now):
35n/a # we must update the local variables on every cycle
36n/a self.gmt = time.gmtime(now)
37n/a now = time.localtime(now)
38n/a
39n/a if now[3] < 12: self.ampm='(AM|am)'
40n/a else: self.ampm='(PM|pm)'
41n/a
42n/a self.jan1 = time.localtime(time.mktime((now[0], 1, 1, 0, 0, 0, 0, 1, 0)))
43n/a
44n/a try:
45n/a if now[8]: self.tz = time.tzname[1]
46n/a else: self.tz = time.tzname[0]
47n/a except AttributeError:
48n/a self.tz = ''
49n/a
50n/a if now[3] > 12: self.clock12 = now[3] - 12
51n/a elif now[3] > 0: self.clock12 = now[3]
52n/a else: self.clock12 = 12
53n/a
54n/a self.now = now
55n/a
56n/a def setUp(self):
57n/a try:
58n/a import java
59n/a java.util.Locale.setDefault(java.util.Locale.US)
60n/a except ImportError:
61n/a import locale
62n/a locale.setlocale(locale.LC_TIME, 'C')
63n/a
64n/a def test_strftime(self):
65n/a now = time.time()
66n/a self._update_variables(now)
67n/a self.strftest1(now)
68n/a self.strftest2(now)
69n/a
70n/a if support.verbose:
71n/a print("Strftime test, platform: %s, Python version: %s" % \
72n/a (sys.platform, sys.version.split()[0]))
73n/a
74n/a for j in range(-5, 5):
75n/a for i in range(25):
76n/a arg = now + (i+j*100)*23*3603
77n/a self._update_variables(arg)
78n/a self.strftest1(arg)
79n/a self.strftest2(arg)
80n/a
81n/a def strftest1(self, now):
82n/a if support.verbose:
83n/a print("strftime test for", time.ctime(now))
84n/a now = self.now
85n/a # Make sure any characters that could be taken as regex syntax is
86n/a # escaped in escapestr()
87n/a expectations = (
88n/a ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'),
89n/a ('%A', calendar.day_name[now[6]], 'full weekday name'),
90n/a ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'),
91n/a ('%B', calendar.month_name[now[1]], 'full month name'),
92n/a # %c see below
93n/a ('%d', '%02d' % now[2], 'day of month as number (00-31)'),
94n/a ('%H', '%02d' % now[3], 'hour (00-23)'),
95n/a ('%I', '%02d' % self.clock12, 'hour (01-12)'),
96n/a ('%j', '%03d' % now[7], 'julian day (001-366)'),
97n/a ('%m', '%02d' % now[1], 'month as number (01-12)'),
98n/a ('%M', '%02d' % now[4], 'minute, (00-59)'),
99n/a ('%p', self.ampm, 'AM or PM as appropriate'),
100n/a ('%S', '%02d' % now[5], 'seconds of current time (00-60)'),
101n/a ('%U', '%02d' % ((now[7] + self.jan1[6])//7),
102n/a 'week number of the year (Sun 1st)'),
103n/a ('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'),
104n/a ('%W', '%02d' % ((now[7] + (self.jan1[6] - 1)%7)//7),
105n/a 'week number of the year (Mon 1st)'),
106n/a # %x see below
107n/a ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
108n/a ('%y', '%02d' % (now[0]%100), 'year without century'),
109n/a ('%Y', '%d' % now[0], 'year with century'),
110n/a # %Z see below
111n/a ('%%', '%', 'single percent sign'),
112n/a )
113n/a
114n/a for e in expectations:
115n/a # musn't raise a value error
116n/a try:
117n/a result = time.strftime(e[0], now)
118n/a except ValueError as error:
119n/a self.fail("strftime '%s' format gave error: %s" % (e[0], error))
120n/a if re.match(escapestr(e[1], self.ampm), result):
121n/a continue
122n/a if not result or result[0] == '%':
123n/a self.fail("strftime does not support standard '%s' format (%s)"
124n/a % (e[0], e[2]))
125n/a else:
126n/a self.fail("Conflict for %s (%s): expected %s, but got %s"
127n/a % (e[0], e[2], e[1], result))
128n/a
129n/a def strftest2(self, now):
130n/a nowsecs = str(int(now))[:-1]
131n/a now = self.now
132n/a
133n/a nonstandard_expectations = (
134n/a # These are standard but don't have predictable output
135n/a ('%c', fixasctime(time.asctime(now)), 'near-asctime() format'),
136n/a ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)),
137n/a '%m/%d/%y %H:%M:%S'),
138n/a ('%Z', '%s' % self.tz, 'time zone name'),
139n/a
140n/a # These are some platform specific extensions
141n/a ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'),
142n/a ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'),
143n/a ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'),
144n/a ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'),
145n/a ('%n', '\n', 'newline character'),
146n/a ('%r', '%02d:%02d:%02d %s' % (self.clock12, now[4], now[5], self.ampm),
147n/a '%I:%M:%S %p'),
148n/a ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'),
149n/a ('%s', nowsecs, 'seconds since the Epoch in UCT'),
150n/a ('%t', '\t', 'tab character'),
151n/a ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
152n/a ('%3y', '%03d' % (now[0]%100),
153n/a 'year without century rendered using fieldwidth'),
154n/a )
155n/a
156n/a
157n/a for e in nonstandard_expectations:
158n/a try:
159n/a result = time.strftime(e[0], now)
160n/a except ValueError as result:
161n/a msg = "Error for nonstandard '%s' format (%s): %s" % \
162n/a (e[0], e[2], str(result))
163n/a if support.verbose:
164n/a print(msg)
165n/a continue
166n/a if re.match(escapestr(e[1], self.ampm), result):
167n/a if support.verbose:
168n/a print("Supports nonstandard '%s' format (%s)" % (e[0], e[2]))
169n/a elif not result or result[0] == '%':
170n/a if support.verbose:
171n/a print("Does not appear to support '%s' format (%s)" % \
172n/a (e[0], e[2]))
173n/a else:
174n/a if support.verbose:
175n/a print("Conflict for nonstandard '%s' format (%s):" % \
176n/a (e[0], e[2]))
177n/a print(" Expected %s, but got %s" % (e[1], result))
178n/a
179n/a
180n/aclass Y1900Tests(unittest.TestCase):
181n/a """A limitation of the MS C runtime library is that it crashes if
182n/a a date before 1900 is passed with a format string containing "%y"
183n/a """
184n/a
185n/a def test_y_before_1900(self):
186n/a # Issue #13674, #19634
187n/a t = (1899, 1, 1, 0, 0, 0, 0, 0, 0)
188n/a if (sys.platform == "win32"
189n/a or sys.platform.startswith(("aix", "sunos", "solaris"))):
190n/a with self.assertRaises(ValueError):
191n/a time.strftime("%y", t)
192n/a else:
193n/a self.assertEqual(time.strftime("%y", t), "99")
194n/a
195n/a def test_y_1900(self):
196n/a self.assertEqual(
197n/a time.strftime("%y", (1900, 1, 1, 0, 0, 0, 0, 0, 0)), "00")
198n/a
199n/a def test_y_after_1900(self):
200n/a self.assertEqual(
201n/a time.strftime("%y", (2013, 1, 1, 0, 0, 0, 0, 0, 0)), "13")
202n/a
203n/aif __name__ == '__main__':
204n/a unittest.main()