I had to advance a Time
to the next weekday for a client Ruby on Rails project. I admit I dreaded writing that code, I somehow imagined awful hacks atop awful hacks.
I'm happy to report it didn't turn out quite so bad after all:
def next_weekday(original_date)
one_day = 60 * 60 * 24 # in Rails just say 1.day
weekdays = 1..5 # Monday is wday 1
result = original_date
result += one_day until result > original_date && weekdays.member?(result.wday)
result
end
And the test that drove it:
def test_next_weekday_skips_weekend
original_date = Time.at(1165606025) # a Friday
assert_equal(5, original_date.wday, "this test should give a Friday")
new_date = next_weekday(original_date)
assert(
  new_date > original_date,
  "new date should be after the original date"
)
assert_equal(
  1, new_date.wday,
  "new date should be a Monday since we gave a Friday"
)
end
Please feel free to embarrass me with your improvements in the comments.