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:
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)
And the test that drove it:
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)
new_date > original_date,
"new date should be after the original date"
"new date should be a Monday since we gave a Friday"
Please feel free to embarrass me with your improvements in the comments.