From: "Kroeger Simon (ext)" Date: 2005-07-29T20:23:07+09:00 Subject: AW: add_weekdays method > Thanks for the alternative implementation Todd. I'm still > running with > my original but I've changed it ever so slightly, as it's now in the > Time class rather than Date, and therefore I'm first creating a new > DateTime and then adding days to it. > > def add_weekdays(days) > new_date = DateTime.new(self.year, self.month, self.day, > self.hour, > self.min, self.sec) > while days > 0 > new_date += 1 > days -= 1 unless (new_date.wday == 6 || new_date.wday == 0) > end > new_date > end > > I'm pretty certain it doesn't, but, on the off chance it makes a > difference this is to be used in a Rails app. > > Chris Hi Chris, i guess the iteration isn't necessary at all, perhaps this would do the job (not so much tested): class Date def add_weekdays(days) new_date = self new_date += (days / 5) * 7 new_date += 1 if (new_date.wday==6) new_date += 1 if (new_date.wday==0) new_date += 2 if (new_date.wday + (days % 5)) > 5 new_date += (days % 5) new_date end end (I'm sure this could be written even more compressed) Simon