-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpause.py
More file actions
64 lines (50 loc) · 1.58 KB
/
pause.py
File metadata and controls
64 lines (50 loc) · 1.58 KB
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
from datetime import datetime
import time as pytime
from time import sleep
# Below is modified from python pause module, fixed an error
def until(time):
"""
Pause your program until a specific end time.
'time' is either a valid datetime object or unix timestamp in seconds (i.e. seconds since Unix epoch)
"""
end = time
# Convert datetime to unix timestamp
if type(time) is datetime:
end = time.timestamp() # float(time.strftime('%S.%f'))
# Type check
if type(end) not in [int, float]:
raise Exception(
'The time parameter is not a number or datetime object')
# Now we wait
while True:
now = pytime.time()
diff = end - now
#
# Time is up!
#
if diff <= 0:
break
#
# Let's try to tune the precision, as we get closer to the end time
#
# Sleep by 0.001, when we're within 0.1 seconds
if diff <= 0.1:
sleep(0.001)
# Sleep by 0.01, when we're within 0.5 seconds
elif diff <= 0.5:
sleep(0.01)
# Sleep by 0.1, when we're within 1.5 seconds
elif diff <= 1.5:
sleep(0.1)
# Otherwise sleep by 1 second
else:
sleep(1)
def main():
dt = datetime(2018, 6, 5, 15, 27, 10, 0)
dx = datetime.now()
print("Seconds until epoch of wait time " + str(dt.timestamp()))
print("Waiting until: " + str(dt) + ". It is currently: " + str(dx))
until(dt)
print("Done! It is now " + str(dt))
if __name__ == '__main__':
main()