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

27 lines
762 B
Text
Raw Normal View History

2022-11-18 00:39:09 -08:00
#!/bin/bash
# Usage: sleep-until <time>
2022-06-01 02:27:46 -07:00
# Ex: sleep-until 3:00 pm
DRY="${DRY:-false}"
2022-06-01 02:27:46 -07:00
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
2022-06-01 02:27:46 -07:00
SLEEP_TIME="$(( $(date -f - +%s- <<< "$*"$'\nnow') 0 ))"
if [ "$SLEEP_TIME" -gt 0 ]; then
if ! "$DRY"; then
2023-04-07 04:46:12 -07:00
echo "Sleeping until: $(date --date="$*") (${SLEEP_TIME}s)"
sleep "$SLEEP_TIME"
else
echo "$SLEEP_TIME"
fi
2022-06-01 02:27:46 -07:00
else
echo "Error: input date '$*' is in the past" >&2
echo "Sadly, you cannot sleep to a time in the past" >&2
exit 1
2022-06-01 02:27:46 -07:00
fi
else
echo "Error: input date '$*' is invalid" >&2
echo "Please input a valid date" >&2
exit 1
2022-06-01 02:27:46 -07:00
fi