In Python, showing the day of the week as an integer using datetime.strftime()
shows a different result than using datetime.weekday()
.
Python's strftime
function emulates that in the c library. Thus, the motivation that %w
returns 0
for a Sunday comes entirely from that.
In contrast, the method date.weekday()
returns a 6
for Sunday as it seeks to match the behaviour of the much older time
module. Within that module times are generally represented by a struct_time
and within this, struct_time.tm_day
uses a 6
to represent a Sunday.
The correct question then becomes ... why does time.struct_time
represent a Sunday as a 6
, when the C library's tm
struct uses a 0
??
And the answer is ... because it just does. This behaviour has existed ever since Guido first checked in gmtime
and localtime
functions in 1993.
And Guido cannot be wrong ... so you best ask him.