]> piware.de Git - bin.git/commitdiff
daytime-term: Check sun rise/set instead of hardcoded time
authorMartin Pitt <martin@piware.de>
Mon, 10 Mar 2025 06:01:14 +0000 (07:01 +0100)
committerMartin Pitt <martin@piware.de>
Mon, 10 Mar 2025 06:01:14 +0000 (07:01 +0100)
daytime-term

index 9d7c888c99b72906e3e891feb504b8831c05c86f..51f9b827bc2d5fe84546bbf0146070b1a5e0dc02 100755 (executable)
@@ -1,4 +1,5 @@
 #!/bin/sh
+DATA=$HOME/.cache/sunrise.json
 set -eu
 
 if [ "${1:-}" = "night" ]; then
@@ -6,8 +7,17 @@ if [ "${1:-}" = "night" ]; then
     night=1
 elif [ "${1:-}" = "day" ]; then
     night=
-elif [ `date +%k` -ge 20 ] || [ `date +%k` -lt 7 ]; then
-    night=1
+else
+    # get daylight times at most once a day
+    if [ ! -e "$DATA" ] || [ $(( `date +%s` - `stat -c %Y $DATA` )) -gt 86400 ]; then
+        curl --silent --show-error -o "$DATA" 'https://api.sunrise-sunset.org/json?lat=48.22&lng=10.54&formatted=0'
+    fi
+    sunrise=$(jq -r .results.sunrise < ~/.cache/sunrise.json)
+    sunset=$(jq -r .results.sunset < ~/.cache/sunrise.json)
+    now=$(date '+%H%M')
+    if [ $now -le $(date -d "$sunrise" '+%H%M') ] || [ $now -ge $(date -d "$sunset" '+%H%M') ]; then
+        night=1
+    fi
 fi
 
 if [ -n "${night:-}" ]; then