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