test_strftime.py 6.8 KB

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