Good job. That’s exactly what I meant.
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 W.
Rick Denatale wrote:
On 2/28/08, Serg K. [email protected] wrote:
(…)
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
That’s why i like this group. You learn a lot and get some good Ruby
code.
Jari W. wrote:
knowledge is a bit lacking, but I guessed that if I could access
that, I could have just used that :-SThose constants are declared private.
Best regards,
Jari W.
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.
On 2/29/08, Siep K. [email protected] wrote:
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.
Week - Wikipedia
ISO week date - Wikipedia
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/