"""Simple full-screen clock.""" import time from PIL import Image, ImageDraw from screens.base import Screen from screens.render_utils import load_font, text_width LINE_GAP = 0.04 # fraction of min(width, height) between the time and date lines WEEKDAYS_DE = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] MONTHS_DE = [ "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember", ] class ClockScreen(Screen): id = "clock" display_name = "Clock" poll_interval = 30 def check(self, settings: dict): return time.strftime("%H:%M") def render(self, state, settings: dict, resolution: tuple[int, int]) -> Image.Image: width, height = resolution image = Image.new("RGB", (width, height), "white") draw = ImageDraw.Draw(image) now = time.localtime() time_str = state or time.strftime("%H:%M", now) date_str = f"{WEEKDAYS_DE[now.tm_wday]}, {now.tm_mday}. {MONTHS_DE[now.tm_mon - 1]}" scale = min(width, height) time_font = load_font(size=int(scale * 0.32), bold=True) date_font = load_font(size=int(scale * 0.06)) gap = int(scale * LINE_GAP) time_left, time_top, time_right, time_bottom = draw.textbbox((0, 0), time_str, font=time_font) time_h = time_bottom - time_top date_left, date_top, date_right, date_bottom = draw.textbbox((0, 0), date_str, font=date_font) date_h = date_bottom - date_top block_h = time_h + gap + date_h block_top = (height - block_h) / 2 tw = time_right - time_left draw.text(((width - tw) / 2, block_top - time_top), time_str, font=time_font, fill="black") dw = date_right - date_left draw.text(((width - dw) / 2, block_top + time_h + gap - date_top), date_str, font=date_font, fill=(90, 90, 90)) return image