57 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
# This script prints the time remaining for the current timer
# as stored in the file ~/.timer as a binary unix timestamp.
import time
import datetime
# Get the time stored in the file ~/.timer
try:
with open("/home/ezri/.timer", "br") as f:
timer = f.read()
except FileNotFoundError:
print("It's Time!", flush=True)
exit()
# Convert the binary unix timestamp to a datetime object
timer = datetime.datetime.fromtimestamp(int.from_bytes(timer, "big"))
while True:
# Get the current time
now = datetime.datetime.now()
# Calculate the time remaining
remaining = timer - now
# If the timer has already expired, delete the file and exit
if remaining.total_seconds() <= 0:
import os
os.remove("/home/ezri/.timer")
print("Timer expired.")
# Print the time remaining in the format "D days, HH:MM:SS"
if remaining.days == 0:
print(
"{} hours {:02d}:{:02d}".format(
remaining.seconds // 3600,
remaining.seconds % 3600 // 60,
remaining.seconds % 60,
),
flush=True,
)
else:
print(
"{} days, {:02d}:{:02d}:{:02d}".format(
remaining.days,
remaining.seconds // 3600,
remaining.seconds % 3600 // 60,
remaining.seconds % 60,
),
flush=True,
)
# Wait half a second before printing the time again
time.sleep(1)