From c11aefb9635dc18ab87d00c59a669c4f223f3f1e Mon Sep 17 00:00:00 2001 From: lew Date: Wed, 1 Apr 2026 22:39:11 +0100 Subject: [PATCH] fix: fail on explicitly past dates, rather than pushing forwards to always tomorrow --- nag | 14 ++++++++++++-- test/nag.bats | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/nag b/nag index c4c5755..1b9ee4c 100755 --- a/nag +++ b/nag @@ -556,8 +556,8 @@ _format_time() { # # Description: # Parse a human-readable time string via `date -d` and print a unix -# timestamp. If the resulting time is in the past, roll forward to -# the same time-of-day tomorrow. +# timestamp. If the time is earlier today, roll forward to tomorrow. +# If the date is explicitly in the past (a previous day), error. _parse_time() { local _time_str="${1:-}" [[ -n "${_time_str}" ]] || _exit_1 printf "No time specified.\\n" @@ -571,6 +571,16 @@ _parse_time() { if [[ "${_timestamp}" -le "${_now}" ]] then + local _parsed_date _today + _parsed_date="$(date -d "@${_timestamp}" +%Y-%m-%d)" + _today="$(date +%Y-%m-%d)" + + if [[ "${_parsed_date}" != "${_today}" ]] + then + _exit_1 printf "Time is in the past: %s.\\n" "${_time_str}" + fi + + # Today but already passed: roll to tomorrow same time. local _time_of_day _time_of_day="$(date -d "@${_timestamp}" +%H:%M:%S)" _timestamp="$(date -d "tomorrow ${_time_of_day}" +%s)" || diff --git a/test/nag.bats b/test/nag.bats index 45ee66c..c22d172 100644 --- a/test/nag.bats +++ b/test/nag.bats @@ -71,6 +71,11 @@ load test_helper [ "${status}" -eq 1 ] } +@test "at with explicit past date fails" { + run_nag at "yesterday 3pm" "too late" + [ "${status}" -eq 1 ] +} + @test "at without message fails" { run_nag at "tomorrow 3pm" [ "${status}" -eq 1 ]