gpt4 book ai didi

python - 为什么 python 的 datetime.isocalendar() 和 datetime.year 不同?

转载 作者:行者123 更新时间:2023-11-28 20:51:43 30 4
gpt4 key购买 nike

在 python 2.6 中尝试以下操作:

newyear_2012 = datetime.date.fromtimestamp(1325419200)
newyear.year # returns 2012
newyear.isocalendar() # return (2011, 52, 7)

这是怎么回事?这是一个错误吗?我找不到它的文档,但也许我没找对地方。

如果我转换那个 unix 时间戳 here ,它告诉我:2012 年 1 月 1 日星期日 12:00:00 GMT

更新:下面的答案解释了这不是 python 错误,而是由于 ISO 规范。如果你想获得永远不会把一年的前几天放在#52 周的周数,你可以尝试以下方法:

week_no = int(time.strftime("%U", datetime_object.timetuple()))

我不确定它对应的标准是什么,但从我的角度来看它表现得更直观(在我的应用程序中,一年内的周数应该随时间上升)。

最佳答案

来自 https://www.staff.science.uu.nl/~gent0113/calendar/isocalendar_text2.htm

ISO 日历年的第一周是包含一月份至少四天的最早一周...(假定从星期一开始)

这使得 ISO 2012 从 1 月 2 日开始。您的时间戳是 Sun, 01 Jan 2012 12:00:00 GMT

关于python - 为什么 python 的 datetime.isocalendar() 和 datetime.year 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8874592/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com