Package DebTorrent :: Module clock
[hide private]
[frames] | no frames]

Source Code for Module DebTorrent.clock

 1  # Written by John Hoffman 
 2  # Modified by Cameron Dale 
 3  # see LICENSE.txt for license information 
 4  # 
 5  # $Id: clock.py 71 2007-05-29 05:58:07Z camrdale-guest $ 
 6   
 7  """Implement wall clock time for Unix systems. 
 8   
 9  This module implements a clock() function that will return a non-decreasing 
10  time, regardless of the system it is called on. This is necessary for Unix  
11  systems, whose clock() function instead returns the current processor time. 
12   
13  @type _MAXFORWARD: C{int} 
14  @var _MAXFORWARD: the maximum number of seconds to allow the clock 
15      to move forward 
16  @type _FUDGE: C{int} 
17  @var _FUDGE: the fudged time change to use if the clock moved forward more 
18      than L{_MAXFORWARD}, or if the clock moved back 
19  @type _RTIME: L{RelativeTime} 
20  @var _RTIME: the RelativeTime instance to use 
21   
22  """ 
23   
24  from time import * 
25  import sys 
26   
27  _MAXFORWARD = 100 
28  _FUDGE = 1 
29   
30 -class RelativeTime:
31 """Calculate relative time on Unix systems. 32 33 @type time: C{float} 34 @ivar time: the last time value measured 35 @type offset: C{float} 36 @ivar offset: the offset to use from the current time values due to 37 any changes made in the clock while the program was running 38 39 """ 40
41 - def __init__(self):
42 """Initialize the time values.""" 43 self.time = time() 44 self.offset = 0
45
46 - def get_time(self):
47 """Calculate a non-decreasing time. 48 49 Uses the time() function to calculate non-decreasing time values. 50 Checks to make sure the time values are non-decreasing, and also 51 don't change by more than L{_MAXFORWARD} seconds within a reading. 52 These could occur if the system clock was changed during the running 53 of the program. 54 55 @rtype: C{float} 56 @return: the current time 57 58 """ 59 60 t = time() + self.offset 61 if t < self.time or t > self.time + _MAXFORWARD: 62 self.time += _FUDGE 63 self.offset += self.time - t 64 return self.time 65 self.time = t 66 return t
67 68 if sys.platform != 'win32': 69 _RTIME = RelativeTime()
70 - def clock():
71 """Override the clock() function for Unix systems. 72 73 This function will return a non-decreasing measure of the current 74 time. This is only used on Unix systems. On Windows systems, the 75 clock() function from the C{time} module will be used. 76 77 @rtype: C{float} 78 @return: the relative time from the L{RelativeTime} instance 79 80 """ 81 return _RTIME.get_time()
82