Skip to content

Instantly share code, notes, and snippets.

@alganet
Created February 7, 2020 21:00
Show Gist options
  • Save alganet/f5bfac29f5a78b811ec1e10ad96393e2 to your computer and use it in GitHub Desktop.
Save alganet/f5bfac29f5a78b811ec1e10ad96393e2 to your computer and use it in GitHub Desktop.
hours.sh
#!/usr/bin/env bash
calculate_debt_hours_per_day () {
total_debt="${1:-}"
hours_debt="${1%:*}"
minutes_debt="${1#*:}"
payment_days="${2:-}"
if test -z "$hours_debt"
then
echo "Usage: bash hours.sh [HOURS_TO_PAY] [PAYMENT_DAYS]"
echo "Example: bash hours.sh 10:20 10"
return
fi
seconds_debt="$(echo "($hours_debt * 3600) + ($minutes_debt * 60)" | bc)"
seconds_per_day="$(echo "$seconds_debt / $payment_days" | bc)"
hours_per_day="$(echo "$seconds_per_day / 3600" | bc)"
minutes_per_day="$(echo "$seconds_per_day % 3600 / 60" | bc)"
time_per_day="$(printf '%02d:%02d\n' "$hours_per_day" "$minutes_per_day")"
echo -e "To pay for \033[1m$total_debt\033[0m negative hours you'll need to work an extra \033[1m$time_per_day\033[0m for \033[1m$payment_days\033[0m days."
}
calculate_debt_hours_per_day "${@:-}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment