如何在Python中导出给定(iso)周数/年的周开始

发布于 2024-10-13 15:33:34 字数 73 浏览 1 评论 0原文

我知道我可以使用 datetime.isocalendar() 来获取给定特定日期的周数。给定周数和年份,如何逆向检索该周的第一天。

I know I can use datetime.isocalendar() for getting the weeknumber given a certain date. How can I do the inverse, given a weeknumber and year retrieve the first day of that week.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后 2024-10-20 15:33:34

如果您仅限于 stdlib,您可以执行以下操作:

>>> datetime.datetime.strptime('2011, 4, 0', '%Y, %U, %w')
datetime.datetime(2011, 1, 23, 0, 0)

If you're limited to stdlib you could do the following:

>>> datetime.datetime.strptime('2011, 4, 0', '%Y, %U, %w')
datetime.datetime(2011, 1, 23, 0, 0)
梦里南柯 2024-10-20 15:33:34

找不到标准库,所以不得不推出自己的库。主要困难是找到 ISO 年份的第一天(可能是上一年)。您需要添加一些输入检查等...

import datetime
def isoWeekToGregorian(isoYear, isoWeek, isoDayOfWeek):
    #we have to find the date of the iso year
    t0 = datetime.datetime(isoYear,1,1,0,0,0)
    t0iso = t0.isocalendar()
    if (t0iso[1] != 1):
        t0prime = t0 + datetime.timedelta(7-t0iso[2])
    else:
        t0prime = t0 - datetime.timedelta(t0iso[2])
    #we can add our weeks and days...
    return t0prime + datetime.timedelta(7*(isoWeek-1) + isoDayOfWeek)

我们可以创建一个简单的测试:

#TEST: we know 2004 has 53 weeks...
t0 = datetime.datetime(2004,12,26,0,0,0)
t1 = datetime.datetime(2005,1,10,0,0,0)
ndays = (t1-t0).days + 1
for i in range(ndays):
    d0 = t0 + datetime.timedelta(i)
    d0iso = d0.isocalendar()
    if (d0 != isoWeekToGregorian(d0iso[0],d0iso[1],d0iso[2])):
        print "failed: %s" % d0
    else:
        print d0, d0iso

哪个正确打印:

2004-12-26 00:00:00 (2004, 52, 7)
2004-12-27 00:00:00 (2004, 53, 1)
2004-12-28 00:00:00 (2004, 53, 2)
2004-12-29 00:00:00 (2004, 53, 3)
2004-12-30 00:00:00 (2004, 53, 4)
2004-12-31 00:00:00 (2004, 53, 5)
2005-01-01 00:00:00 (2004, 53, 6)
2005-01-02 00:00:00 (2004, 53, 7)
2005-01-03 00:00:00 (2005, 1, 1)
2005-01-04 00:00:00 (2005, 1, 2)
2005-01-05 00:00:00 (2005, 1, 3)
2005-01-06 00:00:00 (2005, 1, 4)
2005-01-07 00:00:00 (2005, 1, 5)
2005-01-08 00:00:00 (2005, 1, 6)
2005-01-09 00:00:00 (2005, 1, 7)
2005-01-10 00:00:00 (2005, 2, 1)

Couldn't find a standard library, so had to roll my own. Main difficulty is finding the first day of the ISO year (which could be in the previous year). You'll need to add some input checks etc...

import datetime
def isoWeekToGregorian(isoYear, isoWeek, isoDayOfWeek):
    #we have to find the date of the iso year
    t0 = datetime.datetime(isoYear,1,1,0,0,0)
    t0iso = t0.isocalendar()
    if (t0iso[1] != 1):
        t0prime = t0 + datetime.timedelta(7-t0iso[2])
    else:
        t0prime = t0 - datetime.timedelta(t0iso[2])
    #we can add our weeks and days...
    return t0prime + datetime.timedelta(7*(isoWeek-1) + isoDayOfWeek)

We can create a simple test:

#TEST: we know 2004 has 53 weeks...
t0 = datetime.datetime(2004,12,26,0,0,0)
t1 = datetime.datetime(2005,1,10,0,0,0)
ndays = (t1-t0).days + 1
for i in range(ndays):
    d0 = t0 + datetime.timedelta(i)
    d0iso = d0.isocalendar()
    if (d0 != isoWeekToGregorian(d0iso[0],d0iso[1],d0iso[2])):
        print "failed: %s" % d0
    else:
        print d0, d0iso

Which correctly prints:

2004-12-26 00:00:00 (2004, 52, 7)
2004-12-27 00:00:00 (2004, 53, 1)
2004-12-28 00:00:00 (2004, 53, 2)
2004-12-29 00:00:00 (2004, 53, 3)
2004-12-30 00:00:00 (2004, 53, 4)
2004-12-31 00:00:00 (2004, 53, 5)
2005-01-01 00:00:00 (2004, 53, 6)
2005-01-02 00:00:00 (2004, 53, 7)
2005-01-03 00:00:00 (2005, 1, 1)
2005-01-04 00:00:00 (2005, 1, 2)
2005-01-05 00:00:00 (2005, 1, 3)
2005-01-06 00:00:00 (2005, 1, 4)
2005-01-07 00:00:00 (2005, 1, 5)
2005-01-08 00:00:00 (2005, 1, 6)
2005-01-09 00:00:00 (2005, 1, 7)
2005-01-10 00:00:00 (2005, 2, 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文