aboutsummaryrefslogtreecommitdiffstats
path: root/hyprsunset_qt/sun.py
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-07-17 10:22:40 +0200
committerDanilo M. <danix@danix.xyz>2026-07-17 10:22:40 +0200
commit1b949b536dee4039625218ed7de0634a94e7fa86 (patch)
tree9b19d8caf365dc6d0d97b08bf989b58bc2d6a03d /hyprsunset_qt/sun.py
parentfa74bf36bd5cce6e86c9f38f71284d8d429cc5d6 (diff)
downloadhyprsunset-qt-1b949b536dee4039625218ed7de0634a94e7fa86.tar.gz
hyprsunset-qt-1b949b536dee4039625218ed7de0634a94e7fa86.zip
feat: sun UTC-to-local conversion and cache
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'hyprsunset_qt/sun.py')
-rw-r--r--hyprsunset_qt/sun.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/hyprsunset_qt/sun.py b/hyprsunset_qt/sun.py
new file mode 100644
index 0000000..bc611f9
--- /dev/null
+++ b/hyprsunset_qt/sun.py
@@ -0,0 +1,31 @@
+"""Geolocation + sunrise/sunset lookup with a manual-refresh JSON cache."""
+
+from __future__ import annotations
+
+import json
+from datetime import datetime, tzinfo
+from pathlib import Path
+from typing import Optional
+
+
+def to_local_hm(iso_utc: str, tz: Optional[tzinfo] = None) -> str:
+ """Convert an ISO-8601 UTC timestamp to local HH:MM."""
+ dt = datetime.fromisoformat(iso_utc)
+ local = dt.astimezone(tz) # tz=None -> system local zone
+ return local.strftime("%H:%M")
+
+
+def read_cache(path: Path) -> Optional[dict]:
+ path = Path(path)
+ if not path.exists():
+ return None
+ try:
+ return json.loads(path.read_text())
+ except (json.JSONDecodeError, OSError):
+ return None
+
+
+def write_cache(payload: dict, path: Path) -> None:
+ path = Path(path)
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(json.dumps(payload, indent=2))