python中weekday_为什么Python的datetime.strftime('%w')和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

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

What's the history of why these two are different?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值