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

con*_*lee 2 python datetime

在python 2.6中尝试以下内容:

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

这是什么交易?这是一个错误吗?我找不到记录,但也许我没有找到正确的地方.

如果我在这里转换那个unix时间戳,它会告诉我:Sun,2012年1月1日12:00:00 GMT

更新: 下面的答案解释了这不是python错误,而是由于ISO规范.如果你想得到第52周中一年中头几天不会出现的周数,你可以尝试以下方法:

week_no = int(time.strftime("%U", datetime_object.timetuple()))
Run Code Online (Sandbox Code Playgroud)

我不确定它对应的标准是什么,但从我的角度来看它的行为更直观(在我的应用程序中,一年内的数字应该随着时间的推移而提升).

Dre*_*ann 5

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

ISO日历年的第一周是最早的一周,其中包含1月份的至少4天......(假设从星期一开始)

这使得ISO 2012将于1月2日开始.您的时间戳是Sun,2012年1月1日12:00:00 GMT