96:de8ac6c470d8 | 97:96d5adbe0205 |
---|---|
29 # Return "dreamtime" | 29 # Return "dreamtime" |
30 def dreamtime(): | 30 def dreamtime(): |
31 return max(0, time.monotonic()-dreamtime_difference()) | 31 return max(0, time.monotonic()-dreamtime_difference()) |
32 | 32 |
33 class Time: | 33 class Time: |
34 def __init__(self, when): | |
35 self._value=when | |
36 | |
34 def realtime(self): | 37 def realtime(self): |
35 raise NotImplementedError | 38 raise NotImplementedError |
36 | 39 |
37 def monotonic(self): | 40 def monotonic(self): |
38 raise NotImplementedError | 41 raise NotImplementedError |
55 | 58 |
56 @classmethod | 59 @classmethod |
57 def after(cls, seconds): | 60 def after(cls, seconds): |
58 return cls(cls._now()+seconds) | 61 return cls(cls._now()+seconds) |
59 | 62 |
63 @classmethod | |
64 def after_other(cls, other, seconds): | |
65 if isinstance(other, cls): | |
66 return cls(other._value+seconds) | |
67 else: | |
68 return cls.from_monotonic(other.monotonic()+seconds) | |
69 | |
60 def datetime(self): | 70 def datetime(self): |
61 return datetime.datetime.fromtimestamp(self.realtime()) | 71 return datetime.datetime.fromtimestamp(self.realtime()) |
62 | 72 |
63 def seconds_to(self): | 73 def seconds_to(self): |
64 return self._value-self._now() | 74 return self._value-self._now() |
65 | 75 |
76 def isoformat(self): | |
77 return self.datetime().isoformat() | |
78 | |
66 def __lt__(self, other): | 79 def __lt__(self, other): |
67 return self.monotonic() < other.monotonic() | 80 return self.monotonic() < other.monotonic() |
68 | 81 |
69 def __gt__(self, other): | 82 def __gt__(self, other): |
70 return self.monotonic() > other.monotonic() | 83 return self.monotonic() > other.monotonic() |
77 | 90 |
78 def __eq__(self, other): | 91 def __eq__(self, other): |
79 return self.monotonic() == other.realtime() | 92 return self.monotonic() == other.realtime() |
80 | 93 |
81 class RealTime(Time): | 94 class RealTime(Time): |
82 def __init__(self, when): | |
83 self._value=when | |
84 | |
85 def realtime(self): | 95 def realtime(self): |
86 return self._value | 96 return self._value |
87 | 97 |
88 def monotonic(self): | 98 def monotonic(self): |
89 return self._value+(time.monotonic()-time.time()) | 99 return self._value+(time.monotonic()-time.time()) |
91 @staticmethod | 101 @staticmethod |
92 def _now(): | 102 def _now(): |
93 return time.time() | 103 return time.time() |
94 | 104 |
95 class MonotonicTime(Time): | 105 class MonotonicTime(Time): |
96 def __init__(self, when): | |
97 self._value=when | |
98 | |
99 def realtime(self): | 106 def realtime(self): |
100 return self._value+(time.time()-time.monotonic()) | 107 return self._value+(time.time()-time.monotonic()) |
101 | 108 |
102 def monotonic(self): | 109 def monotonic(self): |
103 return self._value | 110 return self._value |
105 @staticmethod | 112 @staticmethod |
106 def _now(): | 113 def _now(): |
107 return time.monotonic() | 114 return time.monotonic() |
108 | 115 |
109 class DreamTime(Time): | 116 class DreamTime(Time): |
110 def __init__(self, when): | |
111 self._value=when | |
112 | |
113 def realtime(self): | 117 def realtime(self): |
114 return self._value+(time.time()-dreamtime()) | 118 return self._value+(time.time()-dreamtime()) |
115 | 119 |
116 # Important: monotonic is "static" within a wakeup period | 120 # Important: monotonic is "static" within a wakeup period |
117 # and does not need to call time.monotonic(), as it gets compared | 121 # and does not need to call time.monotonic(), as it gets compared |
120 return self._value+dreamtime_difference() | 124 return self._value+dreamtime_difference() |
121 | 125 |
122 @staticmethod | 126 @staticmethod |
123 def _now(): | 127 def _now(): |
124 return dreamtime() | 128 return dreamtime() |
129 | |
125 | 130 |
126 if platform.system()=='Darwin': | 131 if platform.system()=='Darwin': |
127 | 132 |
128 import Foundation | 133 import Foundation |
129 import AppKit | 134 import AppKit |