为什么Python的datetime.strftime('%w')和datetime.weekday()在一周中使用不同的索引? [英] Why do Python's datetime.strftime('%w') and datetime.weekday() use different indexes for the days of the week?

查看:431
本文介绍了为什么Python的datetime.strftime('%w')和datetime.weekday()在一周中使用不同的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,使用 datetime.strftime()显示一周中的日期与使用 datetime.weekday( )

In Python, showing the day of the week as an integer using datetime.strftime() shows a different result than using datetime.weekday().

>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime('%A')
'Sunday'
>>> now.strftime('%w') # Day of the week as an integer.
'0'
>>> now.weekday() # Day of the week as an integer, a different way.
6

使用 strftime(),字符串格式%w 将星期日作为一周的第一天。以 weekday()为周一。

With strftime(), the string format %w has Sunday as the first day of the week. With weekday(), it's Monday instead.

为什么这两个不一样的历史是什么?

推荐答案

Python的 strftime 函数模拟在c图书馆。因此,一个星期天的动机%w 返回 0 完全是从这个。

Python's strftime function emulates that in the c library. Thus, the motivation that %w returns 0 for a Sunday comes entirely from that.

相比之下,方法 date.weekday()返回星期日的 6 它旨在匹配更老的时间模块的行为。在该模块中,时间通常由 struct_time 表示,在此内容中, struct_time.tm_day 使用 6 表示一个星期天。

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.

正确的问题然后变成...为什么 time.struct_time 表示一个星期天作为 6 ,当C库的 tm struct使用 0 ??

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 ??

答案是...,因为它只是。 Guido自1993年以来一直存在于 gmtime localtime 函数之中。

And the answer is ... because it just does. This behaviour has existed ever since Guido first checked in gmtime and localtime functions in 1993.

而Guido不能错,所以你最好问他。

And Guido cannot be wrong ... so you best ask him.

这篇关于为什么Python的datetime.strftime('%w')和datetime.weekday()在一周中使用不同的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆