Why is Python's weekday() different from tm_wday in C?

c, datetime, python

Solution

This was an explicit decision by Guido van Rossum, when he first created the `time` module for Python version 0.9.9; the original commit doesn't explain why he made this choice, but using 0 meaning Monday has been part of Python from the very moment the `localtime` and `gmtime` functions were added. See the `time_convert()` function in that early revision.

We'll have to guess as to why he did this. Most likely, Guido stuck to the ISO 8601 convention for weekdays instead of the C stdlib convention, perhaps because he's European where Monday is the prevailing start if the week. Another option is that he patterned the behaviour on another language entirely; Python's roots are various, and apart from ABC, C and C++ also includes Modula 3. Not that the latter uses this convention; it follows the C stdlib instead.

Note that he also used a different range for the `tm_mon` value, from 1 through to 12 instead of the C stdlib convention of using 0 through to 11.

In any case, a question on comp.lang.python in 2000 about why `time.gmtime()` uses 0 for Monday remained unanswered.

Problem

Python documentation defines `datetime.weekday()` as an integer, where Monday is 0 and Sunday is 6, while C's `tm.tm_wday` is defined as days since Sunday. Therefore `tm_wday` is `(datetime.weekday() + 1) % 7`, which is quite inconvenient. Given that Python generally sticks close to C equivalents, why was this made this way?

Original source