1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """C{timer INTERVAL}
19
20 Generates a sequence of timestamps, separated in time by the specified C{INTERVAL}
21 (approximately). The C{INTERVAL} format is::
22
23 HH:MM:SS
24
25 where C{HH} is hours, C{MM} is minutes, C{SS} is seconds. C{HH:} and
26 C{HH:MM:} may be omitted.
27
28 B{Examples}::
29
30 INTERVAL meaning
31 -----------------------------
32 5 5 seconds
33 1:30 1 minute, 30 seconds
34 1:00:00 1 hour
35
36 The output timestamp is a tuple, matching what is returned by
37 C{time.gmtime()} and C{time.localtime()}: (year, month, day of month,
38 hour, minute, second, day of week, day of year, dst).
39
40 Notes:
41 - month is 1-based (January = 1, February = 2, ...)
42 - day of month is 1-based.
43 - second can go as high as 61 due to leap-seconds.
44 - day of week is 0-based, Monday = 0.
45 - day of year is 1-based.
46 - dst is 1 if Daylight Savings Time is in effect, 0 otherwise.
47 """
48
49 import threading
50 import time
51
52 import osh.core
53
54
57
58
60 """Generates a sequence of timestamps, separated in time by the
61 specified C{interval} (approximately). The C{interval} format is
62 C{HH:MM:SS}, where C{HH} is hours, C{MM} is minutes, C{SS} is
63 seconds. C{HH:} and C{HH:MM:} may be omitted.
64 """
65 return _Timer().process_args(interval)
66
67 -class _Timer(osh.core.Generator):
68
69 _metronome = None
70 _lock = None
71 _now = None
72 _done = False
73
74
75
78
79
80
81
84
86 args = self.args()
87 if args.has_next():
88 interval = self.parse_interval(args.next_string())
89 if args.has_next():
90 self.usage()
91 self._metronome = _Metronome(interval, self)
92 self._lock = threading.Condition()
93 else:
94 self.usage()
95
96
97
98
100
101
102
103
104
105 self._metronome.start()
106 while not self._done:
107 self._lock.acquire()
108 while self._now is None:
109
110
111
112 self._lock.wait(1.0)
113 now = self._now
114 self._now = None
115 self._lock.release()
116 self.send(now)
117
118
119
121 colon1 = interval.find(':')
122 colon2 = -1
123 if colon1 > 0:
124 colon2 = interval.find(':', colon1 + 1)
125
126 if colon1 < 0:
127
128 interval = '0:0:' + interval
129 elif colon2 < 0:
130
131 interval = '0:' + interval
132 colon1 = interval.find(':')
133 colon2 = interval.find(':', colon1 + 1)
134 HH = int(interval[:colon1])
135 MM = int(interval[colon1 + 1 : colon2])
136 SS = int(interval[colon2 + 1 :])
137 return HH * 3600 + MM * 60 + SS
138
140 self._lock.acquire()
141 self._now = time.localtime()
142 self._lock.notifyAll()
143 self._lock.release()
144
146
147 _interval = None
148 _timer = None
149
151 threading.Thread.__init__(self)
152 self._interval = interval
153 self._timer = timer
154 self.setDaemon(True)
155
157 while True:
158 self._timer.register_tick()
159 time.sleep(self._interval)
160