Getting number of days in a month

M

Mohit Sindhwani

Brian said:
And we have a winner :)

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

This may be a bit of a silly question, but this problem came up for me
recently and I wasn't able to solve it actually. I did it another way
and I'll put the code at the end.

If you look at the documentation for the Time class -
http://www.ruby-doc.org/core/classes/Time.html
it has the following:

LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31]

Why can't I access these constants directly? Perhaps, my Ruby knowledge
is a bit lacking, but I guessed that if I could access that, I could
have just used that :-S

Anyway, this is a snippet of what I used eventually for getting the
number of days in a month. It's not as short as some of the others, but
I like it :)

require 'time'
require 'date'

#t is a full date from which we use t.month to find the month for which
you want the number of days
LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if Date.gregorian_leap?(t.year)
days_of_months = LeapYearMonthDays
else
days_of_months = CommonYearMonthDays
end

puts "There are #{days_of_months[t.month]} days in this month."

Yes, it's not tested - it's been snipped out of a running application,
though.

Cheers,
Mohit.
2/29/2008 | 1:35 PM.
 
J

Jari Williamsson

If you look at the documentation for the Time class -
http://www.ruby-doc.org/core/classes/Time.html
it has the following:

LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31]
CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31]

Why can't I access these constants directly? Perhaps, my Ruby knowledge
is a bit lacking, but I guessed that if I could access that, I could
have just used that :-S

Those constants are declared private.


Best regards,

Jari Williamsson
 
S

Siep Korteling

Rick said:
(...)
a normal year starting on weekday 1 starts with 52 full weeks and ends
with a 'week' of 1 days a total of 53 'weeks.'
a leap year starting on weekday 1 starts with 52 full weeks and ends
with a 'week' of 2 days a total of 53 'weeks.'
(...)

Following this analysis:

require 'date'

def numweeks(year)
start_year = Date.civil(year,1,1)
end_year = Date.civil(year,-1,-1)
(start_year.strftime("%U") .. end_year.strftime("%U")).to_a.size
end

puts numweeks(2028) # =>54

regards,

Siep
 
R

Rick DeNatale

(...)

Following this analysis:

require 'date'

def numweeks(year)
start_year = Date.civil(year,1,1)
end_year = Date.civil(year,-1,-1)
(start_year.strftime("%U") .. end_year.strftime("%U")).to_a.size
end

puts numweeks(2028) # =>54

Well, that doesn't exactly follow my analysis, but it does work, but
only if you define the week to start on Sunday.

Another way which DOES follow my analysis

def leap?(year)
year % 400 == 0 || year % 4 == 0 && year % 100 != 0
end

def numweeks(year, start_wday = 0)
leap?(year) && Date.civil(year,1,2).wday == start_wday ? 54 : 53
end

As it turns out, rather unsurprisingly given the world in which we
live, numbering the weeks in a year is more complicated than counting
them.

http://en.wikipedia.org/wiki/Week#Week_number
http://en.wikipedia.org/wiki/ISO_week_date
 
M

Mohit Sindhwani

Jari said:
If you look at the documentation for the Time class -
http://www.ruby-doc.org/core/classes/Time.html
it has the following:

LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31]
CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31]

Why can't I access these constants directly? Perhaps, my Ruby
knowledge is a bit lacking, but I guessed that if I could access
that, I could have just used that :-S

Those constants are declared private.


Best regards,

Jari Williamsson

Thanks Jari - that clears that up. I guess that means that if I add a
function for 'number of days in a month' to the Time class, I could use
these constants to serve up the number directly without any other
calculation.

Cheers,
Mohit.
3/1/2008 | 9:20 PM.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top