The ISO calendar has a range of confusing rules that are likely the source of the issues you face here. In particular, the .NET implementation of GetWeekOfYear
(with the parameters you have used) is so similar to ISO that explicit clarifications are required and linked from MSDN. The result is that isocalendar
and GetWeekOfYear
will produce results that differ [my emphasis]:
Specifically ISO 8601 always has 7 day weeks. If the first partial week of a year doesn't contain Thursday, then it is counted as the last week of the previous year. Likewise, if the last week of the previous year doesn't contain Thursday then its treated like the first week of the next year. GetWeekOfYear() has the first behavior, but not the second.
Some suggestions for correcting GetWeekOfYear
are discussed at the link above and also at Is .NET giving me the wrong week number for Dec. 29th 2008? If you actually want to match the GetWeekOfYear
behaviour these two links will assist, although you will be applying the reverse transformation to their examples.
The most 'Pythonic' way of doing it is to create a function. Nothing particularly special here, except the usual attempt to minimise code by doing things like day < 4
rather than day == 1 or day == 2 ...
etc.
(Of course, all of this is assuming that Python's isocalendar
correctly matches the ISO calendar. The documentation seems to suggest that it does, where the GetWeekOfYear
explicitly states that it doesn't, so it would seem a reasonable assumption. However, if this is a critical calculation, you will be testing it thoroughly yourself anyway.)