该datetime模块提供了一种方法date.isocalendar,给定日期,以格式返回它([year], [week], [weekday]).我该如何倒退?给定一个([year], [week], [weekday])元组,我怎样才能获得一个date对象?
编辑发现问题及解决方案: What's the best way to find the inverse of datetime.isocalendar()?
我的解决方案有错误。在谷歌搜索上看到了上一个问题,经过下面的测试,该问题有效。(有关 的定义,请参阅上面链接中回答最多的问题iso_to_gregorian)。(基本上找到 iso 年份开始日期,然后使用 timedelta 从日和周数查找当前日期。
for i in range(-10000,10000,1):
x = datetime.datetime.today().date() + datetime.timedelta(i)
x_iso = datetime.datetime.isocalendar(x)
assert iso_to_gregorian(*x_iso) == x
Run Code Online (Sandbox Code Playgroud)