K
Kroeger Simon (ext)
=20
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 =3D self
=20
new_date +=3D (days / 5) * 7=20
new_date +=3D 1 if (new_date.wday=3D=3D6)=20
new_date +=3D 1 if (new_date.wday=3D=3D0)=20
=20
new_date +=3D 2 if (new_date.wday + (days % 5)) > 5
=20
new_date +=3D (days % 5)
new_date
end
end
(I'm sure this could be written even more compressed)
Simon
Thanks for the alternative implementation Todd. I'm still=20
running with=20
my original but I've changed it ever so slightly, as it's now in the=20
Time class rather than Date, and therefore I'm first creating a new=20
DateTime and then adding days to it.
=20
def add_weekdays(days)
new_date =3D DateTime.new(self.year, self.month, self.day,=20
self.hour,=20
self.min, self.sec)
while days > 0
new_date +=3D 1
days -=3D 1 unless (new_date.wday =3D=3D 6 || new_date.wday = =3D=3D 0)
end
new_date
end
=20
I'm pretty certain it doesn't, but, on the off chance it makes a=20
difference this is to be used in a Rails app.
=20
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 =3D self
=20
new_date +=3D (days / 5) * 7=20
new_date +=3D 1 if (new_date.wday=3D=3D6)=20
new_date +=3D 1 if (new_date.wday=3D=3D0)=20
=20
new_date +=3D 2 if (new_date.wday + (days % 5)) > 5
=20
new_date +=3D (days % 5)
new_date
end
end
(I'm sure this could be written even more compressed)
Simon