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