dotfiles/.config/shell/bin/sleep-until

26 lines
762 B
Bash
Executable file

#!/bin/bash
# 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
if ! "$DRY"; then
echo "Sleeping until: $(date --date="$*") (${SLEEP_TIME}s)"
sleep "$SLEEP_TIME"
else
echo "$SLEEP_TIME"
fi
else
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 "Error: input date '$*' is invalid" >&2
echo "Please input a valid date" >&2
exit 1
fi