From 949e1baacde2f0b5b40b7f28ba0c105f17c4301c Mon Sep 17 00:00:00 2001 From: Hyperling Date: Mon, 2 Nov 2026 07:36:49 -0700 Subject: [PATCH] Fix false positives when time was not getting successfully retrieved. --- sunset-alarm | 66 +++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/sunset-alarm b/sunset-alarm index 68c1562..6db2518 100755 --- a/sunset-alarm +++ b/sunset-alarm @@ -1,18 +1,14 @@ #!/bin/sh -# sunset-alarm -- Alert before or after sunset +# sunset-alarm -- Alert before or after sunset. # Displays a persistent alert when current time is within # (sunset minus specified minutes) of midnight. ############################################################################### # Defaults ############################################################################### -LATLONG="" -SUNSET_TIME="" -MINUTES="" -SUNSET_EPOCH=0 -LAST_DATE="" -PROMPT="sunset-alarm" +declare LATLONG="" SUNSET_TIME="" MINUTES="" LAST_DATE="" +typeset -i SUNSET_EPOCH=0 ############################################################################### # Usage @@ -56,7 +52,7 @@ while [ $# -gt 0 ]; do esac done -# Validate inputs +# Validate inputs. if [ -z "$MINUTES" ]; then echo "ERROR: -m MINUTES is required." >&2 usage @@ -106,24 +102,26 @@ fetch_sunset_time() { #REQUEST="https://api.open-meteo.com/v1/forecast?latitude=${LAT}&longitude=${LON}&timezone=auto&daily=sunset&forecast_days=1" REQUEST="https://api.open-meteo.com/v1/forecast?latitude=${LAT}&longitude=${LON}&timezone=auto&daily=sunset&start_date=${TODAY}&end_date=${TODAY}" RESPONSE=$(curl -s "$REQUEST") + status="$?" + echo "Curl command completed with a status of $status." - if [ -z "$RESPONSE" ]; then + if [[ -z "$RESPONSE" ]]; then echo "ERROR: Did not receive response. $REQUEST" >&2 return 1 fi - # Extract sunset timestamp like "2025-06-15T19:32:00" + # Extract sunset timestamp like "2025-06-15T19:32:00". SUNSET_RAW=$(echo "$RESPONSE" | jq -r '.daily.sunset[0]') - if [ -z "$SUNSET_RAW" ]; then + if [[ -z "$SUNSET_RAW" ]]; then echo "ERROR: Could not parse time. $RESPONSE" >&2 return 1 fi - # Convert to epoch + # Convert to epoch. SUNSET_EPOCH=$(date -d "$(echo "$SUNSET_RAW" | cut -dT -f1) $(echo "$SUNSET_RAW" | cut -dT -f2)" +%s 2>/dev/null) - if [ -z "$SUNSET_EPOCH" ]; then + if [[ -z "$SUNSET_EPOCH" ]]; then echo "ERROR: Could not calculate epoch. $SUNSET_RAW" >&2 return 1 fi @@ -166,53 +164,63 @@ while true; do TODAY=$(date +%Y-%m-%d) CURRENT_TIME=$(date +%s) - # Check if we need to fetch new sunset time for a new day - if [ "$LAST_DATE" != "$TODAY" ]; then + # Check if we need to fetch new sunset time for a new day. + if [[ "$LAST_DATE" != "$TODAY" \ + || "$SUNSET_EPOCH" == 0 || -z "$SUNSET_TIME" ]] + then LAST_DATE="$TODAY" - if [ -n "$LATLONG" ]; then - # Need internet to get sunset time + if [[ -n "$LATLONG" ]]; then + # Need internet to get sunset time. if ! check_internet; then - echo "No internet connection. Retrying in 5 minutes." >&2 + echo "ERROR: No internet connection. Retrying in 5 minutes." >&2 sleep 300 continue fi fetch_sunset_time || { - echo "Failed fetching sunset time. Retrying in 5 minutes." >&2 + echo "ERROR: Failed fetching sunset time. Retrying in 5 minutes." >&2 sleep 300 continue } + + if [[ -z "$SUNSET_TIME" ]]; then + echo "ERROR: Sunset time is still null. Retrying in 5 minutes." >&2 + sleep 300 + continue + fi else - # User provided sunset time directly, convert to epoch + # User provided sunset time directly, convert to epoch. SUNSET_EPOCH=$(date -d "${TODAY} ${SUNSET_TIME}" +%s 2>/dev/null) - if [ -z "$SUNSET_EPOCH" ]; then - echo "ERROR: could not parse time '${SUNSET_TIME}'." >&2 + if [[ -z "$SUNSET_EPOCH" ]]; then + echo "ERROR: Could not parse time '${SUNSET_TIME}'." >&2 sleep 60 continue fi fi fi - # Calculate alert window start (sunset minus specified minutes) + # Calculate alert window start (sunset minus specified minutes). ALERT_WINDOW_START=$(( SUNSET_EPOCH - (MINUTES * 60) )) # Calculate midnight of the current day MIDNIGHT=$(date -d "${TODAY} +1 day" +%s) - # Alert while current time is within the window (from window start until midnight) - if [ "$CURRENT_TIME" -ge "$ALERT_WINDOW_START" ] && \ - [ "$CURRENT_TIME" -lt "$MIDNIGHT" ]; then - echo "Alert time has been reached, show message." + # Alert while current time is within the window (from window start until midnight). + if [[ -n "$SUNSET_TIME" && "$SUNSET_EPOCH" -ge 0 \ + && "$CURRENT_TIME" -ge "$ALERT_WINDOW_START" \ + && "$CURRENT_TIME" -lt "$MIDNIGHT" ]] + then + echo "Alert time has been reached for $SUNSET_TIME, show message." show_alert echo "Alert has been dismissed, sleeping for 5 minutes." sleep 290 echo "Waking back up from notification dismissal." fi - # Reset when we've passed midnight so we fetch new time tomorrow - if [ "$CURRENT_TIME" -ge "$MIDNIGHT" ]; then + # Reset when we've passed midnight so we fetch new time tomorrow. + if [[ "$CURRENT_TIME" -ge "$MIDNIGHT" ]]; then LAST_DATE="" fi