~ubuntu-branches/ubuntu/natty/miro/natty

« back to all changes in this revision

Viewing changes to portable/clock.py

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2011-01-22 02:46:33 UTC
  • mfrom: (1.4.10 upstream) (1.7.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110122024633-kjme8u93y2il5nmf
Tags: 3.5.1-1ubuntu1
* Merge from debian.  Remaining ubuntu changes:
  - Use python 2.7 instead of python 2.6
  - Relax dependency on python-dbus to >= 0.83.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Permission is hereby granted, free of charge, to any person
2
 
# obtaining a copy of this software and associated documentation files
3
 
# (the "Software"), to deal in the Software without restriction,
4
 
# including without limitation the rights to use, copy, modify, merge,
5
 
# publish, distribute, sublicense, and/or sell copies of the Software,
6
 
# and to permit persons to whom the Software is furnished to do so,
7
 
# subject to the following conditions:
8
 
9
 
# The above copyright notice and this permission notice shall be
10
 
# included in all copies or substantial portions of the Software.
11
 
12
 
# The Software is provided "AS IS", without warranty of any kind,
13
 
# express or implied, including but not limited to the warranties of
14
 
# merchantability,  fitness for a particular purpose and
15
 
# noninfringement. In no event shall the  authors or copyright holders
16
 
# be liable for any claim, damages or other liability, whether in an
17
 
# action of contract, tort or otherwise, arising from, out of or in
18
 
# connection with the Software or the use or other dealings in the
19
 
# Software.
20
 
#
21
 
# Copyright (C) 2002 John Hoffman
22
 
#               2006-2010 Participatory Culture Foundation
23
 
 
24
 
from time import time, clock
25
 
import sys
26
 
import threading
27
 
 
28
 
_MAXFORWARD = 100
29
 
_FUDGE = 1
30
 
 
31
 
class RelativeTime:
32
 
    def __init__(self):
33
 
        self.time = time()
34
 
        self.offset = 0
35
 
        self.lock = threading.Lock()
36
 
 
37
 
    def get_time(self):
38
 
        self.lock.acquire()
39
 
        try:
40
 
            t = time() + self.offset
41
 
            if t < self.time or t > self.time + _MAXFORWARD:
42
 
#                 print "FUDGE"
43
 
#                 print "t:           %s" % t
44
 
#                 print "self.time:   %s" % self.time
45
 
#                 print "self.offset: %s" % self.offset
46
 
                self.time += _FUDGE
47
 
                self.offset += self.time - t
48
 
                return self.time
49
 
            self.time = t
50
 
        finally:
51
 
            self.lock.release()
52
 
        return t
53
 
 
54
 
if sys.platform != 'win32':
55
 
    _RTIME = RelativeTime()
56
 
    def clock():
57
 
        return _RTIME.get_time()