File: clock.py

package info (click to toggle)
miro 1.2.3-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 60,356 kB
  • ctags: 15,099
  • sloc: cpp: 58,491; python: 40,363; ansic: 796; xml: 265; sh: 197; makefile: 167
file content (39 lines) | stat: -rw-r--r-- 1,071 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Originally taken from BitTornado written by John Hoffman
# see portable/BitTorrent/LICENSE.txt for license information.
#
# Rewritten by Nick Nassar <nassar@pculture dotorg> to be thread safe.

from time import *
import sys
import threading

_MAXFORWARD = 100
_FUDGE = 1

class RelativeTime:
    def __init__(self):
        self.time = time()
        self.offset = 0
        self.lock = threading.Lock()

    def get_time(self):
        self.lock.acquire()
        try:
            t = time() + self.offset
            if t < self.time or t > self.time + _MAXFORWARD:
#                 print "FUDGE"
#                 print "t:           %s" % t
#                 print "self.time:   %s" % self.time
#                 print "self.offset: %s" % self.offset
                self.time += _FUDGE
                self.offset += self.time - t
                return self.time
            self.time = t
        finally:
            self.lock.release()
        return t

if sys.platform != 'win32':
    _RTIME = RelativeTime()
    def clock():
        return _RTIME.get_time()