sleep-until: better errors, add DRY mode for dry run

This commit is contained in:
PowerUser64 2023-04-07 03:03:47 -07:00
parent b31e1a64a0
commit 1bd8ce3744

View file

@ -2,16 +2,23 @@
# Usage: sleep-until <time>
# Ex: sleep-until 3:00 pm
DRY="${DRY:-false}"
if [ -n "$1" ] && date --date="$*" > /dev/null 2>&1; then
# Calculate the time in seconds to sleep by subtracting the future time from the current time
SLEEP_TIME="$(( $(date -f - +%s- <<< "$*"$'\nnow') 0 ))"
if [ "$SLEEP_TIME" -gt 0 ]; then
echo "Sleeping until: $(date --date="$*") (${SLEEP_TIME}s)"
sleep "$SLEEP_TIME"
if ! "$DRY"; then
sleep "$SLEEP_TIME"
fi
else
echo "Sadly, you cannot sleep to a time in the past"
echo "Error: input date '$*' is in the past" >&2
echo "Sadly, you cannot sleep to a time in the past" >&2
exit 1
fi
else
echo "Please input a valid date"
echo "Error: input date '$*' is invalid" >&2
echo "Please input a valid date" >&2
exit 1
fi