Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

c

datetime

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?

like image 539
dragonroot Avatar asked Jan 10 '14 13:01

dragonroot


1 Answers

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.

like image 193
Martijn Pieters Avatar answered Nov 15 '22 20:11

Martijn Pieters