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