Welcome, guest | Sign In | My Account | Store | Cart

timehook is a simple module that allows you to modify the time clock without having to modify the system clock.

Python, 92 lines
 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class Clock(object):
    # Get a private copy of the time module
    _time = __import__('time')

    def __init__(self, rate=1):
        self.rate = rate
        
        # Initialize the real and virtual current time
        self.time = self.start = self._time.time()


    def getCurrentTimestamp(self):
        # Compute the elapsed real time
        now = self._time.time() 
        d =  now - self.time

        # Update the real and virtual current time
        self.time = now
        self.start = self.start + d * self.rate

        return self.start

    def setCurrentTime(self, timestamp):
        self.start = self._time.mktime(timestamp.timetuple())


class TimeHook(object):
    # Get a private copy of the time module
    _time = __import__('time')

    def __init__(self, clock):
        """Install the hook, using the given Clock implementation to
        obtain the current time.
        """

        import sys

        self.clock = clock
        sys.modules['time'] = self


    def asctime(self, t=None):
        if t is None:
            t = self.localtime()

        return self._time.asctime(t)

    def ctime(self, secs=None):
        if secs is None:
            secs = self.time()
        
        return self._time.ctime(secs)

    def gmtime(self, secs=None):
        if secs is None:
            secs = self.time()

        return self._time.gmtime(secs)

    def localtime(self, secs=None):
        if secs is None:
            secs = self.time()

        return self._time.localtime(secs)

    def time(self):
        return self.clock.getCurrentTimestamp()

    def __getattr__(self, name):
        return getattr(self._time, name)


if __name__ == '__main__':
    # Install the hook
    clock = Clock(rate=60*60)
    TimeHook(clock)


    # We need to import the time module after the initialization of
    # the hook
    import time
    import datetime


    clock.setCurrentTime(datetime.datetime(1999, 12, 31, 22))
    for i in range(4):
        print datetime.datetime.fromtimestamp(time.time())
        print datetime.datetime.now() # TODO does not works
        print datetime.date.today()
        print '-' * 26
        
        time.sleep(1)

With timehook it is possible to alter the time in a Python script, as an axample by moving the current time in the past or increment the clock rate.

Know bugs and problems: - datetime.datetime.now() is not influenced by the time hook.