PageRenderTime 40ms CodeModel.GetById 16ms app.highlight 21ms 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 | 6 complexity | 6aaa1d96ce3d5cb2035da59771a7a3cd MD5 | raw file
  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
 16from iptest.assert_util import *
 17
 18import time
 19
 20def test_strftime():
 21    t = time.localtime()
 22    x = time.strftime('%x %X', t)
 23    
 24    Assert(len(x) > 3)
 25    
 26    x1 = time.strftime('%x', t)
 27    x2 = time.strftime('%X', t)
 28    
 29    Assert(len(x1) > 1)
 30    Assert(len(x2) > 1)
 31    
 32    AreEqual(x, x1 + ' ' + x2)
 33    
 34    t = time.gmtime()
 35    AreEqual(time.strftime('%c', t), time.strftime('%x %X', t))
 36
 37    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")
 38
 39    tests = [  # user reported scenario modified to use all DST values
 40              ((2003, 5, 2, 0, 0, 0, 4, 122, 0), '2003-05-02 00:00:00'), 
 41              ((2003, 5, 2, 0, 0, 0, 4, 122, 1), '2003-05-02 00:00:00'), 
 42              ((2003, 5, 2, 0, 0, 0, 4, 122, -1), '2003-05-02 00:00:00'),
 43              # DST in 2003 starts 3/9/2003 and ends 11/2/2003, test interesting dates
 44              ((2003, 3, 8, 0, 0, 0, 4, 122, 0), '2003-03-08 00:00:00'),
 45              ((2003, 3, 8, 0, 0, 0, 4, 122, 1), '2003-03-08 00:00:00'),
 46              ((2003, 3, 8, 0, 0, 0, 4, 122, -1), '2003-03-08 00:00:00'), 
 47              
 48              ((2003, 3, 9, 0, 0, 0, 4, 122, 0),  '2003-03-09 00:00:00'),
 49              ((2003, 3, 9, 0, 0, 0, 4, 122, 1),  '2003-03-09 00:00:00'),
 50              ((2003, 3, 9, 0, 0, 0, 4, 122, -1),  '2003-03-09 00:00:00'),
 51              
 52              ((2003, 3, 10, 0, 0, 0, 4, 122, 0),   '2003-03-10 00:00:00'),
 53              ((2003, 3, 10, 0, 0, 0, 4, 122, 1),   '2003-03-10 00:00:00'),
 54              ((2003, 3, 10, 0, 0, 0, 4, 122, -1),   '2003-03-10 00:00:00'),
 55
 56              ((2003, 11, 1, 0, 0, 0, 4, 122, 0), '2003-11-01 00:00:00'),
 57              ((2003, 11, 1, 0, 0, 0, 4, 122, 1), '2003-11-01 00:00:00'),
 58              ((2003, 11, 1, 0, 0, 0, 4, 122, -1), '2003-11-01 00:00:00'), 
 59              
 60              ((2003, 11, 2, 0, 0, 0, 4, 122, 0),  '2003-11-02 00:00:00'),
 61              ((2003, 11, 2, 0, 0, 0, 4, 122, 1),  '2003-11-02 00:00:00'),
 62              ((2003, 11, 2, 0, 0, 0, 4, 122, -1),  '2003-11-02 00:00:00'),
 63              
 64              ((2003, 11, 3, 0, 0, 0, 4, 122, 0),   '2003-11-03 00:00:00'),
 65              ((2003, 11, 3, 0, 0, 0, 4, 122, 1),   '2003-11-03 00:00:00'),
 66              ((2003, 11, 3, 0, 0, 0, 4, 122, -1),   '2003-11-03 00:00:00'),
 67            ]                                        
 68
 69    for dateTime, result in tests:
 70        AreEqual(time.strftime('%Y-%m-%d %H:%M:%S',time.struct_time(dateTime)), result)
 71
 72def test_strptime():
 73    import time
 74    d = time.strptime("July 3, 2006 At 0724 GMT", "%B %d, %Y At %H%M GMT")
 75    AreEqual(d[0], 2006)
 76    AreEqual(d[1], 7)
 77    AreEqual(d[2], 3)
 78    AreEqual(d[3], 7)
 79    AreEqual(d[4], 24)
 80    AreEqual(d[5], 0)
 81    AreEqual(d[6], 0)
 82    AreEqual(d[7], 184)
 83    
 84    #CodePlex Work Item 2557
 85    AreEqual((2006, 7, 3, 7, 24, 0, 0, 184, -1), time.strptime("%07/03/06 07:24:00", "%%%c"))
 86    AreEqual((1900, 6, 1, 0, 0, 0, 4, 152, -1), time.strptime("%6", "%%%m"))
 87    AreEqual((1942, 1, 1, 0, 0, 0, 3, 1, -1), time.strptime("%1942", "%%%Y"))
 88    AreEqual((1900, 1, 6, 0, 0, 0, 5, 6, -1), time.strptime("%6", "%%%d"))
 89        
 90    AreEqual((1900, 7, 9, 19, 30, 0, 4, 190, -1), time.strptime('Fri, July 9 7:30 PM', '%a, %B %d %I:%M %p'))
 91    # CPY & IPY differ on daylight savings time for this parse
 92        
 93    AssertError(ValueError, time.strptime, "July 3, 2006 At 0724 GMT", "%B %x, %Y At %H%M GMT")
 94    
 95#Skip under silverlight because we cannot determine whether the AMD processor bug applies
 96#here or not.
 97@skip("silverlight win32")
 98def test_sleep():
 99    #The QueryPerformanceCounter() system  call is broken in XP and 2K3 (see
100    #http://channel9.msdn.com/ShowPost.aspx?PostID=156175) for
101    #certain AMD64 multi-proc machines.
102    #This means that randomly y can end up being less than x.
103    if get_environ_variable("PROCESSOR_REVISION")=="0508" and get_environ_variable("PROCESSOR_LEVEL")=="15":
104        print "Bailing test_sleep for certain AMD64 machines!"
105        return
106        
107    sleep_time = 5
108    safe_deviation = 0.20
109
110    x = time.clock()
111    time.sleep(sleep_time)
112    y = time.clock()
113    
114    print
115    print "x is", x
116    print "y is", y
117    
118    if y>x:
119        Assert(y-x > sleep_time*(1-(safe_deviation/2)))
120        Assert(y-x < sleep_time*(1+safe_deviation))  # make sure we're close...
121
122def test_dst():
123    if is_silverlight:
124        print "Dev10 524020"
125        return
126    AreEqual(time.altzone, time.timezone+[3600,-3600][time.daylight])
127    t = time.time()
128    AreEqual(time.mktime(time.gmtime(t))-time.mktime(time.localtime(t)), time.timezone)
129
130def test_tzname():
131    AreEqual(type(time.tzname), tuple)
132    AreEqual(len(time.tzname), 2)
133
134def test_struct_time():
135    AreEqual(time.struct_time("123456789"), ('1', '2', '3', '4', '5', '6', '7', '8', '9'))
136    
137    class Exc(Exception): pass
138
139    class C:
140        def __getitem__(self, i): raise Exc
141        def __len__(self): return 9
142    
143    AssertError(Exc, time.struct_time, C())
144
145def test_gmtime():
146    AreEqual(time.gmtime(1015758000.0), (2002, 3, 10, 11, 0, 0, 6, 69, 0))
147    AreEqual(time.gmtime(0), (1970, 1, 1, 0, 0, 0, 3, 1, 0))
148
149def test_localtime():
150    AreEqual(time.mktime(time.localtime(0)), 0)
151
152def test_asctime():
153    AreEqual(time.asctime((2009, 9, 4, 14, 57, 11, 4, 247, 0)), 'Fri Sep 04 14:57:11 2009')
154    AreEqual(time.asctime((2009, 9, 4, 14, 57, 11, 4, 247, 1)), 'Fri Sep 04 14:57:11 2009')
155    AreEqual(time.asctime((2009, 9, 4, 14, 57, 11, 4, 247, -1)), 'Fri Sep 04 14:57:11 2009')
156
157
158run_test(__name__)