Calculating the ISO week number

By Jan Stout

To explain the routine for calculating the ISO week number, we first focus on the simple week number (SWN) defined below. At the end we give the necessary corrections to calculate the ISO week number.


Introducing the simple week number.

The simple week number we define such that

For an arbitrary day (1..31) of a given month (1..12) and year, the SWN can be calculated by:

SWN( y, m, d ) = 1 + ( DP( y, m ) + d-1 ) / 7

where DP ("Days Passed") is given by:

and ML ("Month Length") is defined as:

and LEAP( y ) is defined as:

Approximating DP.

To improve on the full-fledged calculation described above, we first turn our attention to the DP function. As a first approximation we ignore the exact month lengths and instead assume a standard month length of 30 days. With this approximation of DP, a first approximation of SWN can be calculated by:

With this formula, the simple week number is calculated correctly for some 240 of the 365/366 days in a year. By adding a rounding term of + 2 we can increase the number of correctly calculated week numbers to almost 290:

However, we can correctly calculate the week number for all 365/366 days in a year by making use of the simple day of the week (SDOW) function, defined as the current day for a given date, assuming that the year starts on a Monday.

For an arbitrary day (1..31) of a given month (1..12) and a year that starts on a monday, the SDOW can be calculated by:

Using this function, we define the third approximation of SWN as:

By exhaustive experiments, we verified that SWN3 equals SWN for all possible values of y, m, and d.

Getting rid of the division.

The calculation of SWN3 still contains an division by 7, which is not a standard operation on the 6805. We approximate this division with a formula taken from [BLINN95], which states that for division of an integer i by a number n = 2^k-1, we can use the following approximation:

In our case this leads to the fourth approximation for SWN:

Note that the rounding term has been adjusted, in order to guarantee equivalence between SWN4 and SWN.

Although the division by 64 can be implemented as a series of right shifts, the 8x8->16 bits MUL operation available on the 6805 offers yet another possibility of optimization: Instead of multiplying by 9, we can multiply both numerator and denominator of the 9/64 fraction by 4 and get the division result directly from the high byte of the multiplication result:

From the simple week number to the ISO week number.

In [ISO8601], the week number is defined by:

This means that the days before week 1 in a given year are attributed to the last week of the previous year. Also the days that come after the last week of a given year are attributed to the first week of the next year.

If we adapt approximation SWN5 for the simple week number to reflect the differences between the definitions of both week numbers, we arrive at the final solution, adopted for the week number wristapp:

Note that the simple DOW function has been replaced by the (true) DOW, that is readily available in the Timex Datalink ROM.

References:

Stout, J.M.,The Netherlandsjanstout@bigfoot.com