Date-WeekOfYear
view release on metacpan or search on metacpan
WeekOfYear.html view on Meta::CPAN
<h1 id="OTHER-FUNCTIONS">OTHER FUNCTIONS</h1>
<p>These other functions are available if the use tag ':all' is used, eg:</p>
<pre><code> use Date::WeekOfYear ':all';</code></pre>
<h2 id="day_of_year">day_of_year</h2>
<p>Returns the day of the year, 1 being the first day. The last day is either 365 or 366, the latter if the year is a leap year. Expected arguments are the year, month and day as numeric values. The year is expected as yyyy, the month as 1 to 12 for ...
<h2 id="is_leap_year">is_leap_year</h2>
<p>Returns true (1) if the year is a leap year, otherwise false (0). The expected argument is the year as a numeric value of format yyyy, eg 2014.</p>
<p>In general terms the algorithm for calculating a leap year is as follows...</p>
<p>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 divi...
<h2 id="jan1week_day">jan1week_day</h2>
<p>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.</p>
<p>The returned values are:</p>
<pre><code> 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat and 7=Sun</code></pre>
<p>The expected argument is the year in yyyy format, eg 2014.</p>
<h2 id="week_day">week_day</h2>
<p>week_day takes the year (yyyy eg 2014), month (1 to 12) and month_day as arguments and returns the week day.</p>
<p>The week day returned is an integer representing the day of the week where:</p>
<pre><code> 1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday
7 = Sunday.</code></pre>
<p><b>Note</b> this is similar to that returned by localtime except that Sunday is 7 rather than 0</p>
<h2 id="week_number">week_number</h2>
<p>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 i...
<h1 id="CHANGES">CHANGES</h1>
<p>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.</p>
<p>For example:</p>
<pre><code> 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</code></pre>
<h1 id="ISO-8601">ISO 8601</h1>
<p>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.</p>
<p>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 o...
<p>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 ...
<h2 id="Calculations">Calculations</h2>
<h3 id="Ordinal-Day">Ordinal Day</h3>
<p>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:</p>
<pre><code> 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</code></pre>
<h3 id="Week-Number">Week Number</h3>
<p>Calculating the week number of a given date</p>
<p>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.</p>
<p><b>Method:</b> 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 th...
<pre><code> week(date) = int((ordinal(date) - weekday(date) + 10)/7)</code></pre>
<p>Example: Friday 26 September 2008</p>
<pre><code> Ordinal day: 244 + 26 = 270
Weekday: Friday = 5
270 - 5 + 10 = 275
275 / 7 = 39.28
Result: Week 39</code></pre>
<h3 id="Week-Years">53 Week Years</h3>
<p>The long years, with 53 weeks in them, can be described by any of the following equivalent definitions:</p>
<pre><code> - any year starting on Thursday and any leap year starting on Wednesday
- any year ending on Thursday and any leap year ending on Friday
- years in which 1 January and 31 December (in common years) or either (in leap years) are Thursdays</code></pre>
<h3 id="Day-of-Week">Day of Week</h3>
<p>The tabular forerunner to Tondering's algorithm is embodied in the following ANSI C function. With minor changes, it is adaptable to other high level programming languages such as APL2. (A 6502 assembly language version exists as well.) Devise...
<pre><code> int dow(int y, int m, int d)
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}</code></pre>
<p>The function returns 0 = Sunday, 1 = Monday, etc.</p>
<h1 id="KNOWN-ISSUES">KNOWN ISSUES</h1>
<p><b>Versions prior to 1.5 did not follow ISO 8601.</b></p>
<p>None, however please contact the author at gng@cpan.org should you find any problems and I will endevour to resolve then as soon as possible.</p>
( run in 0.534 second using v1.01-cache-2.11-cpan-39bf76dae61 )