Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Getting number of days in a month (http://www.velocityreviews.com/forums/t848448-getting-number-of-days-in-a-month.html)

Shandy Nantz 02-27-2008 04:17 PM

Getting number of days in a month
 
This is probably an easy question but I am trying to get at the number
of days that are in a month. I have this calendar that I have built, the
idea being that when a month turns from February to March, for example,
the calendar should redisplay itself properly formated showing the new
month and the correct number of days. I have it so that it starts
counting the days on the right day of the week, but I have to know when
to stop counting. Any ideas, Thanks,

-S
--
Posted via http://www.ruby-forum.com/.


Gregory Seidman 02-27-2008 04:40 PM

Re: Getting number of days in a month
 
On Thu, Feb 28, 2008 at 01:17:22AM +0900, Shandy Nantz wrote:
> This is probably an easy question but I am trying to get at the number
> of days that are in a month. I have this calendar that I have built, the
> idea being that when a month turns from February to March, for example,
> the calendar should redisplay itself properly formated showing the new
> month and the correct number of days. I have it so that it starts
> counting the days on the right day of the week, but I have to know when
> to stop counting. Any ideas, Thanks,


require 'date'

def days_in_month(month, year)
month = month.to_i
year = year.to_i
raise ArgumentError.new("invalid month") unless (1..12).to_a.include? month
first = Date.parse sprintf("%04d%02d01", year, month)
next_month = first + 32
(last - last.mday).mday
end

> -S

--Greg



Thomas Preymesser 02-27-2008 04:55 PM

Re: Getting number of days in a month
 
On 27/02/2008, Shandy Nantz <shandybleu@yahoo.com> wrote:
>
> This is probably an easy question but I am trying to get at the number
> of days that are in a month. I have this calendar that I have built, the
> idea being that when a month turns from February to March, for example,
> the calendar should redisplay itself properly formated showing the new
> month and the correct number of days. I have it so that it starts
> counting the days on the right day of the week, but I have to know when
> to stop counting. Any ideas, Thanks,



i have not completely understood your problem, but there is a gem call
'date_utils' which does various calendar calculations.

See http://thopre.wordpress.com/2007/06/...e-week-200724/ for
some examples!

-Thomas

--=20
Thomas Preymesser
thopre@gmail.com
thomas@thopre.com
B=FCro: 030 - 830 353 88
mobil: 0176 - 75 03 03 04
Privat: 030 - 49 78 37 06
http://thopre.wordpress.com/
http://www.thopre.com/


Morton Goldberg 02-27-2008 05:07 PM

Re: Getting number of days in a month
 
On Feb 27, 2008, at 11:17 AM, Shandy Nantz wrote:

> This is probably an easy question but I am trying to get at the number
> of days that are in a month. I have this calendar that I have
> built, the
> idea being that when a month turns from February to March, for
> example,
> the calendar should redisplay itself properly formated showing the new
> month and the correct number of days. I have it so that it starts
> counting the days on the right day of the week, but I have to know
> when
> to stop counting. Any ideas, Thanks,


<code>
require "Date"
d = Date.new(2008, 2, -1)
d.day # => 29
</code>

You can find the last day of a month by creating a Date object for
day -1.

Regards, Morton


Brian Adkins 02-27-2008 05:47 PM

Re: Getting number of days in a month
 
On Feb 27, 11:40 am, Gregory Seidman <gsslist+r...@anthropohedron.net>
wrote:
> On Thu, Feb 28, 2008 at 01:17:22AM +0900, Shandy Nantz wrote:
> > This is probably an easy question but I am trying to get at the number
> > of days that are in a month. I have this calendar that I have built, the
> > idea being that when a month turns from February to March, for example,
> > the calendar should redisplay itself properly formated showing the new
> > month and the correct number of days. I have it so that it starts
> > counting the days on the right day of the week, but I have to know when
> > to stop counting. Any ideas, Thanks,

>
> require 'date'
>
> def days_in_month(month, year)
> month = month.to_i
> year = year.to_i
> raise ArgumentError.new("invalid month") unless (1..12).to_a.include? month
> first = Date.parse sprintf("%04d%02d01", year, month)
> next_month = first + 32
> (last - last.mday).mday
> end
>
> > -S

>
> --Greg


might want to try running that before posting

Gregory Seidman 02-27-2008 06:00 PM

Re: Getting number of days in a month
 
On Thu, Feb 28, 2008 at 02:49:54AM +0900, Brian Adkins wrote:
> On Feb 27, 11:40 am, Gregory Seidman <gsslist+r...@anthropohedron.net>
> wrote:
> > On Thu, Feb 28, 2008 at 01:17:22AM +0900, Shandy Nantz wrote:
> > > This is probably an easy question but I am trying to get at the number
> > > of days that are in a month. I have this calendar that I have built, the
> > > idea being that when a month turns from February to March, for example,
> > > the calendar should redisplay itself properly formated showing the new
> > > month and the correct number of days. I have it so that it starts
> > > counting the days on the right day of the week, but I have to know when
> > > to stop counting. Any ideas, Thanks,

> >
> > require 'date'
> >
> > def days_in_month(month, year)
> > month = month.to_i
> > year = year.to_i
> > raise ArgumentError.new("invalid month") unless (1..12).to_a.include? month
> > first = Date.parse sprintf("%04d%02d01", year, month)
> > next_month = first + 32
> > (last - last.mday).mday
> > end
> >
> > > -S

> > --Greg

>
> might want to try running that before posting


Ah, details. Change the last line of the method to:

(next_month + next_month.mday).mday

Anyhow, it's worth noting that ActiveSupport includes Time.days_in_month.

--Greg



Brian Adkins 02-27-2008 06:11 PM

Re: Getting number of days in a month
 
On Feb 27, 11:17 am, Shandy Nantz <shandyb...@yahoo.com> wrote:
> This is probably an easy question but I am trying to get at the number
> of days that are in a month. I have this calendar that I have built, the
> idea being that when a month turns from February to March, for example,
> the calendar should redisplay itself properly formated showing the new
> month and the correct number of days. I have it so that it starts
> counting the days on the right day of the week, but I have to know when
> to stop counting. Any ideas, Thanks,


require 'date'

def days_in_month year, month
((month < 12) ?
Date.new(year, month + 1) :
Date.new(year + 1, 1)).-(1).mday
end

puts days_in_month(2008,2) # => 29

Brian Adkins 02-27-2008 06:15 PM

Re: Getting number of days in a month
 
On Feb 27, 12:07 pm, Morton Goldberg <m_goldb...@ameritech.net> wrote:
> On Feb 27, 2008, at 11:17 AM, Shandy Nantz wrote:
>
> > This is probably an easy question but I am trying to get at the number
> > of days that are in a month. I have this calendar that I have
> > built, the
> > idea being that when a month turns from February to March, for
> > example,
> > the calendar should redisplay itself properly formated showing the new
> > month and the correct number of days. I have it so that it starts
> > counting the days on the right day of the week, but I have to know
> > when
> > to stop counting. Any ideas, Thanks,

>
> <code>
> require "Date"
> d = Date.new(2008, 2, -1)
> d.day # => 29
> </code>
>
> You can find the last day of a month by creating a Date object for
> day -1.
>
> Regards, Morton


And we have a winner :)

I wish I had read your post before posting mine. I need to read the
stdlib doc more carefully.

Brian Adkins 02-27-2008 06:26 PM

Re: Getting number of days in a month
 
On Feb 27, 1:00 pm, Gregory Seidman <gsslist+r...@anthropohedron.net>
wrote:
> On Thu, Feb 28, 2008 at 02:49:54AM +0900, Brian Adkins wrote:
> > On Feb 27, 11:40 am, Gregory Seidman <gsslist+r...@anthropohedron.net>
> > wrote:
> > > On Thu, Feb 28, 2008 at 01:17:22AM +0900, Shandy Nantz wrote:
> > > > This is probably an easy question but I am trying to get at the number
> > > > of days that are in a month. I have this calendar that I have built, the
> > > > idea being that when a month turns from February to March, for example,
> > > > the calendar should redisplay itself properly formated showing the new
> > > > month and the correct number of days. I have it so that it starts
> > > > counting the days on the right day of the week, but I have to know when
> > > > to stop counting. Any ideas, Thanks,

>
> > > require 'date'

>
> > > def days_in_month(month, year)
> > > month = month.to_i
> > > year = year.to_i
> > > raise ArgumentError.new("invalid month") unless (1..12).to_a.include? month
> > > first = Date.parse sprintf("%04d%02d01", year, month)
> > > next_month = first + 32
> > > (last - last.mday).mday
> > > end

>
> > > > -S
> > > --Greg

>
> > might want to try running that before posting

>
> Ah, details. Change the last line of the method to:
>
> (next_month + next_month.mday).mday
>
> Anyhow, it's worth noting that ActiveSupport includes Time.days_in_month.
>
> --Greg


You still didn't run it, did you? <sigh>

Some ideas you may want to consider:
1) it's probably reasonable to expect numeric month and day arguments,
so you can skip the .to_i calls
2) instead of creating a range, converting it to an array and calling
include?, wouldn't it be better to just use a simple comparison such
as "unless month > 0 && month < 13
3) sprintf'ing a date just to parse it is unnecessary & inefficient
4) it's still broken

Gregory Seidman 02-27-2008 07:17 PM

Re: Getting number of days in a month
 
On Thu, Feb 28, 2008 at 03:29:54AM +0900, Brian Adkins wrote:
> On Feb 27, 1:00 pm, Gregory Seidman <gsslist+r...@anthropohedron.net>
> wrote:
> > On Thu, Feb 28, 2008 at 02:49:54AM +0900, Brian Adkins wrote:
> > > On Feb 27, 11:40 am, Gregory Seidman <gsslist+r...@anthropohedron.net>
> > > wrote:
> > > > On Thu, Feb 28, 2008 at 01:17:22AM +0900, Shandy Nantz wrote:
> > > > > This is probably an easy question but I am trying to get at the number
> > > > > of days that are in a month. I have this calendar that I have built, the
> > > > > idea being that when a month turns from February to March, for example,
> > > > > the calendar should redisplay itself properly formated showing the new
> > > > > month and the correct number of days. I have it so that it starts
> > > > > counting the days on the right day of the week, but I have to know when
> > > > > to stop counting. Any ideas, Thanks,

> >
> > > > require 'date'

> >
> > > > def days_in_month(month, year)
> > > > month = month.to_i
> > > > year = year.to_i
> > > > raise ArgumentError.new("invalid month") unless (1..12).to_a.include? month
> > > > first = Date.parse sprintf("%04d%02d01", year, month)
> > > > next_month = first + 32
> > > > (last - last.mday).mday
> > > > end

> >
> > > > > -S
> > > > --Greg

> >
> > > might want to try running that before posting

> >
> > Ah, details. Change the last line of the method to:
> >
> > (next_month + next_month.mday).mday
> >
> > Anyhow, it's worth noting that ActiveSupport includes Time.days_in_month.
> >
> > --Greg

>
> You still didn't run it, did you? <sigh>


Don't sigh at me. I wrote some code off the cuff and fired it off. I also
didn't include unit tests. Yes, it was buggy and inefficient, but it got
across the approach I was using.

> Some ideas you may want to consider:
> 1) it's probably reasonable to expect numeric month and day arguments,
> so you can skip the .to_i calls


Given that it was for clarity, I think it's valuable.

> 2) instead of creating a range, converting it to an array and calling
> include?, wouldn't it be better to just use a simple comparison such
> as "unless month > 0 && month < 13


Arguable. I prefer range inclusion to a pair of comparisons, but that's a
matter of taste. The to_a only matters if I hadn't performed a to_i on the
month argument previously.

> 3) sprintf'ing a date just to parse it is unnecessary & inefficient


True enough. Date.new (a.k.a. Date.civil) takes year, month, and day
arguments. For that matter, as pointed out elsewhere in this thread, a -1
for the day argument gives the last day of the month, making the rest of
the method moot.

> 4) it's still broken


Typo. The + should have been a - in the correction. The correct, if
unnecessary, method is:

require 'date'

def days_in_month(month, year)
month = month.to_i
year = year.to_i
raise ArgumentError.new("invalid month") unless (1..12).include? month
first = Date.civil(year, month, 1)
next_month = first + 32
(next_month - next_month.mday).mday
end

An even simpler method, taken from elsewhere in the thread:

require 'date'

def days_in_month(month, year)
Date.civil(year, month, -1).mday
end

--Greg




All times are GMT. The time now is 02:09 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.