formated code and added delta hours

This commit is contained in:
Alexander Nigl 2025-04-06 17:40:58 +02:00
parent 549a71a99b
commit 051f04f7b2
2 changed files with 37 additions and 20 deletions

View File

@ -1,14 +1,14 @@
from setuptools import setup, find_packages from setuptools import setup
setup( setup(
name="timer", name="timer",
version="0.1", version="0.2",
author="Alexander Nigl", author="Alexander Nigl",
py_modules=["timer"], py_modules=["timer"],
install_requires=[ install_requires=[
"click", "click",
], ],
entry_points = { entry_points={
'console_scripts': ['timer=timer:main'], "console_scripts": ["timer=timer:main"],
} },
) )

View File

@ -4,30 +4,47 @@ from tkinter import Tk, CENTER, Label
import click import click
import typing as t import typing as t
DELTASECONDS = "+%Ss"
DELTAMINUTES = "+%Mm"
DELTAHOURS = "+%Hh"
HOURS = "%H:%M"
DATETIME_FORMAT = [
"%Y-%m-%d",
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%d %H:%M:%S",
"%Y-%m-%d %H:%M",
HOURS,
DELTAHOURS,
DELTAMINUTES,
DELTASECONDS,
]
class DateTime(click.DateTime): class DateTime(click.DateTime):
def _try_to_convert_date(self, value: t.Any, format: str) -> t.Optional[datetime]: def _try_to_convert_date(self, value: t.Any, format: str) -> t.Optional[datetime]:
try: try:
if format == SECONDS and value[-1] == "s": if format == DELTASECONDS and value[-1] == "s":
return datetime.datetime.now() + datetime.timedelta(seconds=int(value[1:-1])) return datetime.datetime.now() + datetime.timedelta(
if format == MINUTES and value[-1] == "m": seconds=int(value[1:-1])
return datetime.datetime.now() + datetime.timedelta(minutes=int(value[1:-1])) )
if format == DELTAMINUTES and value[-1] == "m":
return datetime.datetime.now() + datetime.timedelta(
minutes=int(value[1:-1])
)
if format == DELTAHOURS and value[-1] == "h":
return datetime.datetime.now() + datetime.timedelta(
hours=int(value[1:-1])
)
if format == HOURS: if format == HOURS:
return datetime.datetime.combine( return datetime.datetime.combine(
datetime.datetime.now().date(), datetime.datetime.now().date(),
datetime.datetime.strptime(value, format).time() datetime.datetime.strptime(value, format).time(),
) )
return datetime.datetime.strptime(value, format) return datetime.datetime.strptime(value, format)
except ValueError: except ValueError:
return None return None
SECONDS = '+%Ms'
MINUTES = '+%Mm'
HOURS = '%H:%M'
DATETIME_FORMAT = ['%Y-%m-%d', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M', HOURS, MINUTES, SECONDS]
@click.command() @click.command()
@click.argument("enddate", type=DateTime(formats=DATETIME_FORMAT)) @click.argument("enddate", type=DateTime(formats=DATETIME_FORMAT))
def main(enddate): def main(enddate):
@ -36,7 +53,7 @@ def main(enddate):
root.configure(background="#000") root.configure(background="#000")
tlbl = Label(root, text="", font=("Arial", -100), fg="#fff", bg="#000") tlbl = Label(root, text="", font=("Arial", -100), fg="#fff", bg="#000")
tlbl.pack() tlbl.pack()
tlbl.place(relx=.5, rely=.5, anchor=CENTER) tlbl.place(relx=0.5, rely=0.5, anchor=CENTER)
def deltaStr(t): def deltaStr(t):
days, remainder = divmod(t.total_seconds(), 60 * 60 * 24) days, remainder = divmod(t.total_seconds(), 60 * 60 * 24)
@ -52,7 +69,7 @@ def main(enddate):
def resize(_): def resize(_):
ratio = root.winfo_width() / 1920 ratio = root.winfo_width() / 1920
tlbl.configure(font=('Chicago', int(ratio * -200))) tlbl.configure(font=("Chicago", int(ratio * -200)))
def update(): def update():
delta = enddate - datetime.datetime.now() delta = enddate - datetime.datetime.now()
@ -66,7 +83,7 @@ def main(enddate):
root.bind("<Configure>", resize) root.bind("<Configure>", resize)
root.bind("<Escape>", lambda x: root.destroy()) root.bind("<Escape>", lambda x: root.destroy())
root.after(1, update) root.after(1, update)
root.wm_attributes('-type', 'splash') root.wm_attributes("-type", "splash")
root.mainloop() root.mainloop()