home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / clock.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  1.0 KB  |  40 lines

  1. # Originally taken from BitTornado written by John Hoffman
  2. # see portable/BitTorrent/LICENSE.txt for license information.
  3. #
  4. # Rewritten by Nick Nassar <nassar@pculture dotorg> to be thread safe.
  5.  
  6. from time import *
  7. import sys
  8. import threading
  9.  
  10. _MAXFORWARD = 100
  11. _FUDGE = 1
  12.  
  13. class RelativeTime:
  14.     def __init__(self):
  15.         self.time = time()
  16.         self.offset = 0
  17.         self.lock = threading.Lock()
  18.  
  19.     def get_time(self):
  20.         self.lock.acquire()
  21.         try:
  22.             t = time() + self.offset
  23.             if t < self.time or t > self.time + _MAXFORWARD:
  24. #                 print "FUDGE"
  25. #                 print "t:           %s" % t
  26. #                 print "self.time:   %s" % self.time
  27. #                 print "self.offset: %s" % self.offset
  28.                 self.time += _FUDGE
  29.                 self.offset += self.time - t
  30.                 return self.time
  31.             self.time = t
  32.         finally:
  33.             self.lock.release()
  34.         return t
  35.  
  36. if sys.platform != 'win32':
  37.     _RTIME = RelativeTime()
  38.     def clock():
  39.         return _RTIME.get_time()
  40.