Date-WeekOfYear

 view release on metacpan or  search on metacpan

WeekOfYear.pm  view on Meta::CPAN


A year will be a leap year if it is divisible by 4 but not by 100. If a
year is divisible by 4 and by 100, it is not a leap year unless it is
also divisible by 400.  Thus years such as 1996, 1992, 1988 and so on
are leap years because they are divisible by 4 but not by 100.
For century years, the 400 rule is important. Thus, century years 1900,
1800 and 1700 while all still divisible by 4 are also exactly divisible
by 100. As they are not further divisible by 400, they are not leap years.


=head2 jan1week_day

Returns the week_day of the 1st of January for the year in question.
The week_day is a numeric value indicating the day and differs from
that returned by the core function localtime() in that Sunday is 7
rather than 0.

The returned values are:

 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat and 7=Sun

The expected argument is the year in yyyy format, eg 2014.

=head2 week_day

week_day takes the year (yyyy eg 2014), month (1 to 12) and month_day as arguments and returns the week day.

The week day returned is an integer representing the day of the week where:

 1 = Monday
 2 = Tuesday
 3 = Wednesday
 4 = Thursday
 5 = Friday
 6 = Saturday
 7 = Sunday.

B<Note> this is similar to that returned by localtime except that Sunday is 7 rather than 0

=head2 week_number

week_number takes the year (yyyy eg 2014), month (1 to 12) and month_day as arguments and returns
the week number as defined by ISO-8061.  That is week 1 starts on a Monday and contains the first
Thursday in the year.  As a result week 1 can start in the previous year and a year can have either
52 or 53 weeks.

=head1 CHANGES

As of version 1.5 the ISO 8601 week number is calculated.  For backwards compatibility
a flag can be passed after the time to give the previous functionality.

For example:

  my $weekNo = WeekOfYear(undef, 1);  # Week number now in pre ISO 8601 mode
  or
  my $weekNo = WeekOfYear($the_time, 1);  # Week number for $the_time in pre ISO 8601 mode


=head1 ISO 8601

Weeks in a Gregorian calendar year can be numbered for each year. This style of
numbering is commonly used (for example, by schools and businesses) in some European
and Asian countries, but rare elsewhere.

ISO 8601 includes the ISO week date system, a numbering system for weeks - each week
begins on a Monday and is associated with the year that contains that week's Thursday
(so that if a year starts in a long weekend Friday-Sunday, week number one of the year
will start after that). For example, week 1 of 2004 (2004W01) ran from Monday 29
December 2003 to Sunday, 4 January 2004, because its Thursday was 1 January 2004,
whereas week 1 of 2005 (2005W01) ran from Monday 3 January 2005 to Sunday 9 January
2005, because its Thursday was 6 January 2005 and so the first Thursday of 2005. The
highest week number in a year is either 52 or 53 (it was 53 in the year 2004).

An ISO week-numbering year (also called ISO year informally) has 52 or 53 full weeks.
That is 364 or 371 days instead of the usual 365 or 366 days.  The extra week is
referred to here as a leap week, although ISO 8601 does not use this term.  Weeks start
with Monday. The first week of a year is the week that contains the first Thursday
(and, hence, 4 January) of the year. ISO week year numbering therefore slightly
deviates from the Gregorian for some days close to 1 January.

=head2 Calculations

=head3 Ordinal Day

If the ordinal date is not known, it can be computed by any of several methods.
perhaps the most direct is a table such as the following:

 To the day of:  Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
 Add:            0   31  59  90  120 151 181 212 243 273 304 334
 For leap years: 0   31  60  91  121 152 182 213 244 274 305 335

=head3 Week Number

Calculating the week number of a given date

The week number of any date can be calculated, given its ordinal date (i.e. position
within the year) and its day of the week.

B<Method:> Using ISO weekday numbers (running from 1 for Monday to 7 for Sunday),
subtract the weekday from the ordinal date, then add 10. Divide the result by 7.
Ignore the remainder; the quotient equals the week number. If the week number
thus obtained equals 0, it means that the given date belongs to the preceding
(week-based) year. If a week number of 53 is obtained, one must check that the
date is not actually in week 1 of the following year.

    week(date) = int((ordinal(date) - weekday(date) + 10)/7)

Example: Friday 26 September 2008

    Ordinal day: 244 + 26 = 270
    Weekday: Friday = 5
    270 - 5 + 10 = 275
    275 / 7 = 39.28
    Result: Week 39

=head3 53 Week Years

The long years, with 53 weeks in them, can be described by any of the following equivalent definitions:

 - any year starting on Thursday and any leap year starting on Wednesday
 - any year ending on Thursday and any leap year ending on Friday



( run in 5.170 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )