except you have to handle december specially - else you'll end up with the
with the wrong year.
easiest is probably:
harp:~ > cat a.rb
require 'date'
class Date
def self.last_day_of_the_month yyyy, mm
d = new yyyy, mm
d += 42 # warp into the next month
new(d.year, d.month) - 1 # back off one day from first of that month
end
end
Ok, I know that I am going to be smacked for this, but we DO know how
many days are in each month. Why not just have one for february with
considerations for leap year, and lists for those with 30 and 31 days,
or possibly 30 days and everything else has 31. It is nothing more than
a lookup.
Yeah that was my first thought too, but notice he is constructing a
new date at the beginning of the month. I am guessing he picked 42,
over 32 as it is just much better number.
pth
···
On 9/21/06, James Moore <banshee@banshee.com> wrote:
> d += 42 # warp into the next month
You need to add a month, not a fixed number of days, since you can't predict
how many days are in the next month:
the point is that it always lands into the next month.
-a
···
On Fri, 22 Sep 2006, Patrick Hurley wrote:
Yeah that was my first thought too, but notice he is constructing a
new date at the beginning of the month. I am guessing he picked 42,
over 32 as it is just much better number.
--
in order to be effective truth must penetrate like an arrow - and that is
likely to hurt. -- wei wu wei
On Wed, Jul 16, 2008 at 10:44:35PM +0900, Bill Walton wrote:
>
> Why override Date at all when we've got
> ActiveSupport::CoreExtensions::Calculations and can just use
> 'end_of_month'?
Because this is the ruby list, not the rubyonrails list.
Or the month after that - depending on where you start.
robert
···
ara.t.howard@noaa.gov wrote:
On Fri, 22 Sep 2006, Patrick Hurley wrote:
Yeah that was my first thought too, but notice he is constructing a
new date at the beginning of the month. I am guessing he picked 42,
over 32 as it is just much better number.
exactly
the point is that it always lands into the next month.
sure - but it always starts on the first day of the month:
require 'date'
class Date
def self.last_day_of_the_month yyyy, mm
d = new yyyy, mm # no day means the first one
d += 42 # always the next month
new(d.year, d.month) - 1
end
end
Yeah that was my first thought too, but notice he is constructing a
new date at the beginning of the month. I am guessing he picked 42,
over 32 as it is just much better number.
exactly
the point is that it always lands into the next month.
Or the month after that - depending on where you start.
--
in order to be effective truth must penetrate like an arrow - and that is
likely to hurt. -- wei wu wei
Yes, of course. I didn't want to insunuate you code would not do what you claimed it to do. It was merely a silly remark, probably stirred up by this strange number that I haven't seen in a while. I am sorry.
Kind regards
robert
···
ara.t.howard@noaa.gov wrote:
On Fri, 22 Sep 2006, Robert Klemme wrote:
ara.t.howard@noaa.gov wrote:
On Fri, 22 Sep 2006, Patrick Hurley wrote:
Yeah that was my first thought too, but notice he is constructing a
new date at the beginning of the month. I am guessing he picked 42,
over 32 as it is just much better number.
exactly
the point is that it always lands into the next month.
Or the month after that - depending on where you start.
sure - but it always starts on the first day of the month: