PageRenderTime 244ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Tests/modules/system_related/time_test.py

#
Python | 158 lines | 99 code | 36 blank | 23 comment | 5 complexity | 6aaa1d96ce3d5cb2035da59771a7a3cd MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. from iptest.assert_util import *
  16. import time
  17. def test_strftime():
  18. t = time.localtime()
  19. x = time.strftime('%x %X', t)
  20. Assert(len(x) > 3)
  21. x1 = time.strftime('%x', t)
  22. x2 = time.strftime('%X', t)
  23. Assert(len(x1) > 1)
  24. Assert(len(x2) > 1)
  25. AreEqual(x, x1 + ' ' + x2)
  26. t = time.gmtime()
  27. AreEqual(time.strftime('%c', t), time.strftime('%x %X', t))
  28. AreEqual(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.mktime((1994,11,15,12,3,10,0,0,-1)))), "1994-11-15 12:03:10")
  29. tests = [ # user reported scenario modified to use all DST values
  30. ((2003, 5, 2, 0, 0, 0, 4, 122, 0), '2003-05-02 00:00:00'),
  31. ((2003, 5, 2, 0, 0, 0, 4, 122, 1), '2003-05-02 00:00:00'),
  32. ((2003, 5, 2, 0, 0, 0, 4, 122, -1), '2003-05-02 00:00:00'),
  33. # DST in 2003 starts 3/9/2003 and ends 11/2/2003, test interesting dates
  34. ((2003, 3, 8, 0, 0, 0, 4, 122, 0), '2003-03-08 00:00:00'),
  35. ((2003, 3, 8, 0, 0, 0, 4, 122, 1), '2003-03-08 00:00:00'),
  36. ((2003, 3, 8, 0, 0, 0, 4, 122, -1), '2003-03-08 00:00:00'),
  37. ((2003, 3, 9, 0, 0, 0, 4, 122, 0), '2003-03-09 00:00:00'),
  38. ((2003, 3, 9, 0, 0, 0, 4, 122, 1), '2003-03-09 00:00:00'),
  39. ((2003, 3, 9, 0, 0, 0, 4, 122, -1), '2003-03-09 00:00:00'),
  40. ((2003, 3, 10, 0, 0, 0, 4, 122, 0), '2003-03-10 00:00:00'),
  41. ((2003, 3, 10, 0, 0, 0, 4, 122, 1), '2003-03-10 00:00:00'),
  42. ((2003, 3, 10, 0, 0, 0, 4, 122, -1), '2003-03-10 00:00:00'),
  43. ((2003, 11, 1, 0, 0, 0, 4, 122, 0), '2003-11-01 00:00:00'),
  44. ((2003, 11, 1, 0, 0, 0, 4, 122, 1), '2003-11-01 00:00:00'),
  45. ((2003, 11, 1, 0, 0, 0, 4, 122, -1), '2003-11-01 00:00:00'),
  46. ((2003, 11, 2, 0, 0, 0, 4, 122, 0), '2003-11-02 00:00:00'),
  47. ((2003, 11, 2, 0, 0, 0, 4, 122, 1), '2003-11-02 00:00:00'),
  48. ((2003, 11, 2, 0, 0, 0, 4, 122, -1), '2003-11-02 00:00:00'),
  49. ((2003, 11, 3, 0, 0, 0, 4, 122, 0), '2003-11-03 00:00:00'),
  50. ((2003, 11, 3, 0, 0, 0, 4, 122, 1), '2003-11-03 00:00:00'),
  51. ((2003, 11, 3, 0, 0, 0, 4, 122, -1), '2003-11-03 00:00:00'),
  52. ]
  53. for dateTime, result in tests:
  54. AreEqual(time.strftime('%Y-%m-%d %H:%M:%S',time.struct_time(dateTime)), result)
  55. def test_strptime():
  56. import time
  57. d = time.strptime("July 3, 2006 At 0724 GMT", "%B %d, %Y At %H%M GMT")
  58. AreEqual(d[0], 2006)
  59. AreEqual(d[1], 7)
  60. AreEqual(d[2], 3)
  61. AreEqual(d[3], 7)
  62. AreEqual(d[4], 24)
  63. AreEqual(d[5], 0)
  64. AreEqual(d[6], 0)
  65. AreEqual(d[7], 184)
  66. #CodePlex Work Item 2557
  67. AreEqual((2006, 7, 3, 7, 24, 0, 0, 184, -1), time.strptime("%07/03/06 07:24:00", "%%%c"))
  68. AreEqual((1900, 6, 1, 0, 0, 0, 4, 152, -1), time.strptime("%6", "%%%m"))
  69. AreEqual((1942, 1, 1, 0, 0, 0, 3, 1, -1), time.strptime("%1942", "%%%Y"))
  70. AreEqual((1900, 1, 6, 0, 0, 0, 5, 6, -1), time.strptime("%6", "%%%d"))
  71. AreEqual((1900, 7, 9, 19, 30, 0, 4, 190, -1), time.strptime('Fri, July 9 7:30 PM', '%a, %B %d %I:%M %p'))
  72. # CPY & IPY differ on daylight savings time for this parse
  73. AssertError(ValueError, time.strptime, "July 3, 2006 At 0724 GMT", "%B %x, %Y At %H%M GMT")
  74. #Skip under silverlight because we cannot determine whether the AMD processor bug applies
  75. #here or not.
  76. @skip("silverlight win32")
  77. def test_sleep():
  78. #The QueryPerformanceCounter() system call is broken in XP and 2K3 (see
  79. #http://channel9.msdn.com/ShowPost.aspx?PostID=156175) for
  80. #certain AMD64 multi-proc machines.
  81. #This means that randomly y can end up being less than x.
  82. if get_environ_variable("PROCESSOR_REVISION")=="0508" and get_environ_variable("PROCESSOR_LEVEL")=="15":
  83. print "Bailing test_sleep for certain AMD64 machines!"
  84. return
  85. sleep_time = 5
  86. safe_deviation = 0.20
  87. x = time.clock()
  88. time.sleep(sleep_time)
  89. y = time.clock()
  90. print
  91. print "x is", x
  92. print "y is", y
  93. if y>x:
  94. Assert(y-x > sleep_time*(1-(safe_deviation/2)))
  95. Assert(y-x < sleep_time*(1+safe_deviation)) # make sure we're close...
  96. def test_dst():
  97. if is_silverlight:
  98. print "Dev10 524020"
  99. return
  100. AreEqual(time.altzone, time.timezone+[3600,-3600][time.daylight])
  101. t = time.time()
  102. AreEqual(time.mktime(time.gmtime(t))-time.mktime(time.localtime(t)), time.timezone)
  103. def test_tzname():
  104. AreEqual(type(time.tzname), tuple)
  105. AreEqual(len(time.tzname), 2)
  106. def test_struct_time():
  107. AreEqual(time.struct_time("123456789"), ('1', '2', '3', '4', '5', '6', '7', '8', '9'))
  108. class Exc(Exception): pass
  109. class C:
  110. def __getitem__(self, i): raise Exc
  111. def __len__(self): return 9
  112. AssertError(Exc, time.struct_time, C())
  113. def test_gmtime():
  114. AreEqual(time.gmtime(1015758000.0), (2002, 3, 10, 11, 0, 0, 6, 69, 0))
  115. AreEqual(time.gmtime(0), (1970, 1, 1, 0, 0, 0, 3, 1, 0))
  116. def test_localtime():
  117. AreEqual(time.mktime(time.localtime(0)), 0)
  118. def test_asctime():
  119. AreEqual(time.asctime((2009, 9, 4, 14, 57, 11, 4, 247, 0)), 'Fri Sep 04 14:57:11 2009')
  120. AreEqual(time.asctime((2009, 9, 4, 14, 57, 11, 4, 247, 1)), 'Fri Sep 04 14:57:11 2009')
  121. AreEqual(time.asctime((2009, 9, 4, 14, 57, 11, 4, 247, -1)), 'Fri Sep 04 14:57:11 2009')
  122. run_test(__name__)