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