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