From: Serg Koren Date: 2008-02-29T11:07:46+09:00 Subject: Re: Getting number of days in a month -- getting the number of weeks in a year. Good job. That's exactly what I meant. On Feb 28, 2008, at 9:01 PM, Rick DeNatale wrote: > On 2/28/08, Serg Koren wrote: >> A more interesting question is how do you get the number of weeks >> in a >> year. >> Some years have 52...some have 54. Calendar weeks...not 7 day weeks. > > I assume you mean here how many 7 day weeks starting on a particular > week day have at least one day in a given year. > > If this is the case then in all but one case every year has 53 > 'weeks.' The only exception is a leap year which starts on the last > day of the week which has 54 weeks. > > for start in (1..7) do > for days in (365..366) do > s = "a #{{365 => 'normal', 366 => 'leap'}[days]} year starting on > weekday #{start} starts with " > start_week_days = start == 1 ? 0 : (8 - start) > s << "a 'week' of #{start_week_days} days then " unless > start_week_days == 0 > days_remaining = days - start_week_days > full_weeks = days_remaining / 7 > end_week_days = days_remaining % 7 > s << "#{full_weeks} full weeks" > s << " and ends with a 'week' of #{end_week_days} days" unless > end_week_days == 0 > s << " a total of #{(start_week_days > 0 ? 1 : 0) + full_weeks + > (end_week_days > 0 ? 1 : 0)} 'weeks.'" > puts s > end > puts > end > > > 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.' > > a normal year starting on weekday 2 starts with a 'week' of 6 days > then 51 full weeks and ends with a 'week' of 2 days a total of 53 > 'weeks.' > a leap year starting on weekday 2 starts with a 'week' of 6 days then > 51 full weeks and ends with a 'week' of 3 days a total of 53 'weeks.' > > a normal year starting on weekday 3 starts with a 'week' of 5 days > then 51 full weeks and ends with a 'week' of 3 days a total of 53 > 'weeks.' > a leap year starting on weekday 3 starts with a 'week' of 5 days then > 51 full weeks and ends with a 'week' of 4 days a total of 53 'weeks.' > > a normal year starting on weekday 4 starts with a 'week' of 4 days > then 51 full weeks and ends with a 'week' of 4 days a total of 53 > 'weeks.' > a leap year starting on weekday 4 starts with a 'week' of 4 days then > 51 full weeks and ends with a 'week' of 5 days a total of 53 'weeks.' > > a normal year starting on weekday 5 starts with a 'week' of 3 days > then 51 full weeks and ends with a 'week' of 5 days a total of 53 > 'weeks.' > a leap year starting on weekday 5 starts with a 'week' of 3 days then > 51 full weeks and ends with a 'week' of 6 days a total of 53 'weeks.' > > a normal year starting on weekday 6 starts with a 'week' of 2 days > then 51 full weeks and ends with a 'week' of 6 days a total of 53 > 'weeks.' > a leap year starting on weekday 6 starts with a 'week' of 2 days then > 52 full weeks a total of 53 'weeks.' > > a normal year starting on weekday 7 starts with a 'week' of 1 days > then 52 full weeks a total of 53 'weeks.' > a leap year starting on weekday 7 starts with a 'week' of 1 days then > 52 full weeks and ends with a 'week' of 1 days a total of 54 'weeks.' > > Or did you mean something else. > > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.com/ >