45 lines
1.1 KiB
Python
Executable File
45 lines
1.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import datetime
|
|
import os
|
|
import json
|
|
from time import sleep
|
|
import sys
|
|
|
|
def get_time():
|
|
try:
|
|
with open(f'{os.environ["HOME"]}/.timer', 'rb') as timestamp_file:
|
|
return datetime.datetime.fromtimestamp(int.from_bytes(timestamp_file.read(8), 'little'))
|
|
except Exception as exc:
|
|
return None
|
|
|
|
while True:
|
|
now = datetime.datetime.now()
|
|
end = get_time()
|
|
if end is None:
|
|
print(json.dumps({
|
|
'hours': '00',
|
|
'minutes': '00',
|
|
'seconds': '00',
|
|
'future': False,
|
|
'timer': False
|
|
}), flush=True)
|
|
sleep(0.5)
|
|
continue
|
|
delta = int((now - get_time()).total_seconds())
|
|
future = False
|
|
if delta < 0:
|
|
future = True
|
|
delta = -delta
|
|
hours = int(delta / 3600)
|
|
minutes = int((delta % 3600) / 60)
|
|
seconds = delta % 60
|
|
print(json.dumps({
|
|
'hours': hours,
|
|
'minutes': f'{minutes:02}',
|
|
'seconds': f'{seconds:02}',
|
|
'future': future,
|
|
'timer': True
|
|
}), flush=True)
|
|
sleep(0.5)
|