PageRenderTime 75ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/cpython/Lib/test/test_time.py

https://github.com/atoun/empythoned
Python | 250 lines | 193 code | 28 blank | 29 comment | 18 complexity | 1e73dada97e7d3a5ebbd0bded6c15f01 MD5 | raw file
  1. from test import test_support
  2. import time
  3. import unittest
  4. import sys
  5. class TimeTestCase(unittest.TestCase):
  6. def setUp(self):
  7. self.t = time.time()
  8. def test_data_attributes(self):
  9. time.altzone
  10. time.daylight
  11. time.timezone
  12. time.tzname
  13. def test_clock(self):
  14. time.clock()
  15. def test_conversions(self):
  16. self.assertTrue(time.ctime(self.t)
  17. == time.asctime(time.localtime(self.t)))
  18. self.assertTrue(long(time.mktime(time.localtime(self.t)))
  19. == long(self.t))
  20. def test_sleep(self):
  21. time.sleep(1.2)
  22. def test_strftime(self):
  23. tt = time.gmtime(self.t)
  24. for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
  25. 'j', 'm', 'M', 'p', 'S',
  26. 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
  27. format = ' %' + directive
  28. try:
  29. time.strftime(format, tt)
  30. except ValueError:
  31. self.fail('conversion specifier: %r failed.' % format)
  32. # Issue #10762: Guard against invalid/non-supported format string
  33. # so that Python don't crash (Windows crashes when the format string
  34. # input to [w]strftime is not kosher.
  35. if sys.platform.startswith('win'):
  36. with self.assertRaises(ValueError):
  37. time.strftime('%f')
  38. def test_strftime_bounds_checking(self):
  39. # Make sure that strftime() checks the bounds of the various parts
  40. #of the time tuple (0 is valid for *all* values).
  41. # Check year [1900, max(int)]
  42. self.assertRaises(ValueError, time.strftime, '',
  43. (1899, 1, 1, 0, 0, 0, 0, 1, -1))
  44. if time.accept2dyear:
  45. self.assertRaises(ValueError, time.strftime, '',
  46. (-1, 1, 1, 0, 0, 0, 0, 1, -1))
  47. self.assertRaises(ValueError, time.strftime, '',
  48. (100, 1, 1, 0, 0, 0, 0, 1, -1))
  49. # Check month [1, 12] + zero support
  50. self.assertRaises(ValueError, time.strftime, '',
  51. (1900, -1, 1, 0, 0, 0, 0, 1, -1))
  52. self.assertRaises(ValueError, time.strftime, '',
  53. (1900, 13, 1, 0, 0, 0, 0, 1, -1))
  54. # Check day of month [1, 31] + zero support
  55. self.assertRaises(ValueError, time.strftime, '',
  56. (1900, 1, -1, 0, 0, 0, 0, 1, -1))
  57. self.assertRaises(ValueError, time.strftime, '',
  58. (1900, 1, 32, 0, 0, 0, 0, 1, -1))
  59. # Check hour [0, 23]
  60. self.assertRaises(ValueError, time.strftime, '',
  61. (1900, 1, 1, -1, 0, 0, 0, 1, -1))
  62. self.assertRaises(ValueError, time.strftime, '',
  63. (1900, 1, 1, 24, 0, 0, 0, 1, -1))
  64. # Check minute [0, 59]
  65. self.assertRaises(ValueError, time.strftime, '',
  66. (1900, 1, 1, 0, -1, 0, 0, 1, -1))
  67. self.assertRaises(ValueError, time.strftime, '',
  68. (1900, 1, 1, 0, 60, 0, 0, 1, -1))
  69. # Check second [0, 61]
  70. self.assertRaises(ValueError, time.strftime, '',
  71. (1900, 1, 1, 0, 0, -1, 0, 1, -1))
  72. # C99 only requires allowing for one leap second, but Python's docs say
  73. # allow two leap seconds (0..61)
  74. self.assertRaises(ValueError, time.strftime, '',
  75. (1900, 1, 1, 0, 0, 62, 0, 1, -1))
  76. # No check for upper-bound day of week;
  77. # value forced into range by a ``% 7`` calculation.
  78. # Start check at -2 since gettmarg() increments value before taking
  79. # modulo.
  80. self.assertRaises(ValueError, time.strftime, '',
  81. (1900, 1, 1, 0, 0, 0, -2, 1, -1))
  82. # Check day of the year [1, 366] + zero support
  83. self.assertRaises(ValueError, time.strftime, '',
  84. (1900, 1, 1, 0, 0, 0, 0, -1, -1))
  85. self.assertRaises(ValueError, time.strftime, '',
  86. (1900, 1, 1, 0, 0, 0, 0, 367, -1))
  87. def test_default_values_for_zero(self):
  88. # Make sure that using all zeros uses the proper default values.
  89. # No test for daylight savings since strftime() does not change output
  90. # based on its value.
  91. expected = "2000 01 01 00 00 00 1 001"
  92. result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
  93. self.assertEqual(expected, result)
  94. def test_strptime(self):
  95. # Should be able to go round-trip from strftime to strptime without
  96. # throwing an exception.
  97. tt = time.gmtime(self.t)
  98. for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
  99. 'j', 'm', 'M', 'p', 'S',
  100. 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
  101. format = '%' + directive
  102. strf_output = time.strftime(format, tt)
  103. try:
  104. time.strptime(strf_output, format)
  105. except ValueError:
  106. self.fail("conversion specifier %r failed with '%s' input." %
  107. (format, strf_output))
  108. def test_asctime(self):
  109. time.asctime(time.gmtime(self.t))
  110. self.assertRaises(TypeError, time.asctime, 0)
  111. self.assertRaises(TypeError, time.asctime, ())
  112. # XXX: Posix compiant asctime should refuse to convert
  113. # year > 9999, but Linux implementation does not.
  114. # self.assertRaises(ValueError, time.asctime,
  115. # (12345, 1, 0, 0, 0, 0, 0, 0, 0))
  116. # XXX: For now, just make sure we don't have a crash:
  117. try:
  118. time.asctime((12345, 1, 1, 0, 0, 0, 0, 1, 0))
  119. except ValueError:
  120. pass
  121. @unittest.skipIf(not hasattr(time, "tzset"),
  122. "time module has no attribute tzset")
  123. def test_tzset(self):
  124. from os import environ
  125. # Epoch time of midnight Dec 25th 2002. Never DST in northern
  126. # hemisphere.
  127. xmas2002 = 1040774400.0
  128. # These formats are correct for 2002, and possibly future years
  129. # This format is the 'standard' as documented at:
  130. # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
  131. # They are also documented in the tzset(3) man page on most Unix
  132. # systems.
  133. eastern = 'EST+05EDT,M4.1.0,M10.5.0'
  134. victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
  135. utc='UTC+0'
  136. org_TZ = environ.get('TZ',None)
  137. try:
  138. # Make sure we can switch to UTC time and results are correct
  139. # Note that unknown timezones default to UTC.
  140. # Note that altzone is undefined in UTC, as there is no DST
  141. environ['TZ'] = eastern
  142. time.tzset()
  143. environ['TZ'] = utc
  144. time.tzset()
  145. self.assertEqual(
  146. time.gmtime(xmas2002), time.localtime(xmas2002)
  147. )
  148. self.assertEqual(time.daylight, 0)
  149. self.assertEqual(time.timezone, 0)
  150. self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
  151. # Make sure we can switch to US/Eastern
  152. environ['TZ'] = eastern
  153. time.tzset()
  154. self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
  155. self.assertEqual(time.tzname, ('EST', 'EDT'))
  156. self.assertEqual(len(time.tzname), 2)
  157. self.assertEqual(time.daylight, 1)
  158. self.assertEqual(time.timezone, 18000)
  159. self.assertEqual(time.altzone, 14400)
  160. self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
  161. self.assertEqual(len(time.tzname), 2)
  162. # Now go to the southern hemisphere.
  163. environ['TZ'] = victoria
  164. time.tzset()
  165. self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
  166. self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0]))
  167. self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
  168. self.assertEqual(len(time.tzname), 2)
  169. self.assertEqual(time.daylight, 1)
  170. self.assertEqual(time.timezone, -36000)
  171. self.assertEqual(time.altzone, -39600)
  172. self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
  173. finally:
  174. # Repair TZ environment variable in case any other tests
  175. # rely on it.
  176. if org_TZ is not None:
  177. environ['TZ'] = org_TZ
  178. elif environ.has_key('TZ'):
  179. del environ['TZ']
  180. time.tzset()
  181. def test_insane_timestamps(self):
  182. # It's possible that some platform maps time_t to double,
  183. # and that this test will fail there. This test should
  184. # exempt such platforms (provided they return reasonable
  185. # results!).
  186. for func in time.ctime, time.gmtime, time.localtime:
  187. for unreasonable in -1e200, 1e200:
  188. self.assertRaises(ValueError, func, unreasonable)
  189. def test_ctime_without_arg(self):
  190. # Not sure how to check the values, since the clock could tick
  191. # at any time. Make sure these are at least accepted and
  192. # don't raise errors.
  193. time.ctime()
  194. time.ctime(None)
  195. def test_gmtime_without_arg(self):
  196. gt0 = time.gmtime()
  197. gt1 = time.gmtime(None)
  198. t0 = time.mktime(gt0)
  199. t1 = time.mktime(gt1)
  200. self.assertTrue(0 <= (t1-t0) < 0.2)
  201. def test_localtime_without_arg(self):
  202. lt0 = time.localtime()
  203. lt1 = time.localtime(None)
  204. t0 = time.mktime(lt0)
  205. t1 = time.mktime(lt1)
  206. self.assertTrue(0 <= (t1-t0) < 0.2)
  207. def test_mktime(self):
  208. # Issue #1726687
  209. for t in (-2, -1, 0, 1):
  210. try:
  211. tt = time.localtime(t)
  212. except (OverflowError, ValueError):
  213. pass
  214. else:
  215. self.assertEqual(time.mktime(tt), t)
  216. def test_main():
  217. test_support.run_unittest(TimeTestCase)
  218. if __name__ == "__main__":
  219. test_main()