fix: fail on explicitly past dates, rather than pushing forwards to always tomorrow
This commit is contained in:
parent
97e5dcbac4
commit
c11aefb963
2 changed files with 17 additions and 2 deletions
14
nag
14
nag
|
|
@ -556,8 +556,8 @@ _format_time() {
|
||||||
#
|
#
|
||||||
# Description:
|
# Description:
|
||||||
# Parse a human-readable time string via `date -d` and print a unix
|
# 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
|
# timestamp. If the time is earlier today, roll forward to tomorrow.
|
||||||
# the same time-of-day tomorrow.
|
# If the date is explicitly in the past (a previous day), error.
|
||||||
_parse_time() {
|
_parse_time() {
|
||||||
local _time_str="${1:-}"
|
local _time_str="${1:-}"
|
||||||
[[ -n "${_time_str}" ]] || _exit_1 printf "No time specified.\\n"
|
[[ -n "${_time_str}" ]] || _exit_1 printf "No time specified.\\n"
|
||||||
|
|
@ -571,6 +571,16 @@ _parse_time() {
|
||||||
|
|
||||||
if [[ "${_timestamp}" -le "${_now}" ]]
|
if [[ "${_timestamp}" -le "${_now}" ]]
|
||||||
then
|
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
|
local _time_of_day
|
||||||
_time_of_day="$(date -d "@${_timestamp}" +%H:%M:%S)"
|
_time_of_day="$(date -d "@${_timestamp}" +%H:%M:%S)"
|
||||||
_timestamp="$(date -d "tomorrow ${_time_of_day}" +%s)" ||
|
_timestamp="$(date -d "tomorrow ${_time_of_day}" +%s)" ||
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,11 @@ load test_helper
|
||||||
[ "${status}" -eq 1 ]
|
[ "${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" {
|
@test "at without message fails" {
|
||||||
run_nag at "tomorrow 3pm"
|
run_nag at "tomorrow 3pm"
|
||||||
[ "${status}" -eq 1 ]
|
[ "${status}" -eq 1 ]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue