Skip to content

Instantly share code, notes, and snippets.

@edvardm
Created December 18, 2012 19:41
Show Gist options
  • Save edvardm/4331229 to your computer and use it in GitHub Desktop.
Save edvardm/4331229 to your computer and use it in GitHub Desktop.
Given time in days, current weight and weight to lose, create a calendar with daily target weights
t0 = Time.local(2012, 12, 19)
time_in_days = 180
cur_w = 90.7
w_delta = 6*2.0
DAY = 86400
target_date = t0 + time_in_days*DAY
target_weight = cur_w - w_delta
daily_loss = w_delta / time_in_days
d = t0
w = cur_w
def year_month(d)
d.strftime('-=-= %02m/%Y =-=-')
end
def adjusted_wday(d)
d.wday == 0 ? 6 : d.wday - 1
end
def last_day?(d)
d.month != (d + DAY).month
end
epsilon = 0.01
while d < target_date
if d.day == 1 || d == t0
puts
puts
puts year_month(d)
print ' '*9*adjusted_wday(d)
end
w -= daily_loss
fmt = (w.to_i - w.round(1)).abs <= epsilon ? '%4d' : '%.1f'
ds, ws = [d.strftime('%-2d'), fmt % w]
out = ["%3s %4s" % [ds, ws]]
out << (!d.sunday? || last_day?(d) ? " " : "\n")
print out.join
d += DAY
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment