/Lib/test/test_strftime.py

http://unladen-swallow.googlecode.com/ · Python · 187 lines · 145 code · 25 blank · 17 comment · 35 complexity · 054d3d433183df64172ca444622834da MD5 · raw file

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