Skip to content

Instantly share code, notes, and snippets.

@nuex
Created October 7, 2012 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuex/3849113 to your computer and use it in GitHub Desktop.
Save nuex/3849113 to your computer and use it in GitHub Desktop.
goal-calc
#!/usr/bin/awk -f
#
# goal-calc
#
# Returns a weekly budget amount given the date of your next
# paycheck, a target date, and a target amount.
#
# USAGE
#
# If you're next paycheck will be arriving on 2011/08/29 and you have
# a bill you need to pay on April 15th, 2012 for $1,500.00:
#
# goal-calc 2011-08-29 2012-04-15 $1,500.00
#
# Will give you the following amount to save each paycheck to reach
# the $1,500.00 goal in 2012:
#
# $46.88
#
BEGIN {
day_seconds = 60*60*24
"date -d " ARGV[1] " +%s" | getline start_date
"date -d " ARGV[2]" +%s" | getline target_date
days_until = ((target_date - start_date) / day_seconds)
weeks_until = int(days_until / 7)
amount = ARGV[3]
sub(/\$/, "", amount)
sub(/\./, "", amount)
sub(/,/, "", amount)
budget_cents = amount / weeks_until
budget = budget_cents / 100
printf("$%0.2f\n", budget)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment