如何计算下周五凌晨3点?

时间:2010-03-13 01:02:17

标签: python date-arithmetic

如何计算下周五凌晨3点作为datetime对象?

澄清:,即计算的日期应始终大于7天,且小于或等于14。

5 个答案:

答案 0 :(得分:9)

如果您安装dateutil,那么您可以执行以下操作:

import datetime
import dateutil.relativedelta as reldate

def following_friday(dt):   
    rd=reldate.relativedelta(
        weekday=reldate.FR(+2),
        hours=+21)
    rd2=reldate.relativedelta(
        hour=3,minute=0,second=0,microsecond=0)
    return dt+rd+rd2

在上方,hours=+21告诉relativedelta在找到下周五之前将dt增加21小时。因此,如果dt是2010年3月12日凌晨2点,则添加21个小时会使当天晚上11点,但如果dt是凌晨3点之后,则添加21小时推送dt进入星期六。

这是一些测试代码。

if __name__=='__main__':
    today=datetime.datetime.now()
    for dt in [today+datetime.timedelta(days=i) for i in range(-7,8)]:
        print('%s --> %s'%(dt,following_friday(dt)))

产生:

2010-03-05 20:42:09.246124 --> 2010-03-19 03:00:00
2010-03-06 20:42:09.246124 --> 2010-03-19 03:00:00
2010-03-07 20:42:09.246124 --> 2010-03-19 03:00:00
2010-03-08 20:42:09.246124 --> 2010-03-19 03:00:00
2010-03-09 20:42:09.246124 --> 2010-03-19 03:00:00
2010-03-10 20:42:09.246124 --> 2010-03-19 03:00:00
2010-03-11 20:42:09.246124 --> 2010-03-19 03:00:00
2010-03-12 20:42:09.246124 --> 2010-03-26 03:00:00 
2010-03-13 20:42:09.246124 --> 2010-03-26 03:00:00
2010-03-14 20:42:09.246124 --> 2010-03-26 03:00:00
2010-03-15 20:42:09.246124 --> 2010-03-26 03:00:00
2010-03-16 20:42:09.246124 --> 2010-03-26 03:00:00
2010-03-17 20:42:09.246124 --> 2010-03-26 03:00:00
2010-03-18 20:42:09.246124 --> 2010-03-26 03:00:00
2010-03-19 20:42:09.246124 --> 2010-04-02 03:00:00

凌晨3点之前:

two = datetime.datetime(2010, 3, 12, 2, 0)
for date in [two+datetime.timedelta(days=i) for i in range(-7,8)]:
    result = following_friday(date)
    print('{0}-->{1}'.format(date,result))

的产率:

2010-03-05 02:00:00-->2010-03-12 03:00:00
2010-03-06 02:00:00-->2010-03-19 03:00:00
2010-03-07 02:00:00-->2010-03-19 03:00:00
2010-03-08 02:00:00-->2010-03-19 03:00:00
2010-03-09 02:00:00-->2010-03-19 03:00:00
2010-03-10 02:00:00-->2010-03-19 03:00:00
2010-03-11 02:00:00-->2010-03-19 03:00:00
2010-03-12 02:00:00-->2010-03-19 03:00:00
2010-03-13 02:00:00-->2010-03-26 03:00:00
2010-03-14 02:00:00-->2010-03-26 03:00:00
2010-03-15 02:00:00-->2010-03-26 03:00:00
2010-03-16 02:00:00-->2010-03-26 03:00:00
2010-03-17 02:00:00-->2010-03-26 03:00:00
2010-03-18 02:00:00-->2010-03-26 03:00:00
2010-03-19 02:00:00-->2010-03-26 03:00:00

答案 1 :(得分:6)

这是一个符合OP要求的功能和测试:

import datetime

_3AM = datetime.time(hour=3)
_FRI = 4 # Monday=0 for weekday()

def next_friday_3am(now):
    now += datetime.timedelta(days=7)
    if now.time() < _3AM:
        now = now.combine(now.date(),_3AM)
    else:
        now = now.combine(now.date(),_3AM) + datetime.timedelta(days=1)
    return now + datetime.timedelta((_FRI - now.weekday()) % 7)

if __name__ == '__main__':
    start = datetime.datetime.now()
    for i in xrange(7*24*60*60):
        now = start + datetime.timedelta(seconds=i)
        then = next_friday_3am(now)
        assert datetime.timedelta(days=7) < then - now <= datetime.timedelta(days=14)
        assert then.weekday() == _FRI
        assert then.time() == _3AM

答案 2 :(得分:4)

我喜欢dateutil这些任务一般,但我不理解你想要的启发式 - 因为我使用的话,如果我说“下个星期五”而且是星期四,我会< / em>明天意味着(可能我一直在努力工作并且忘记了一周的哪一天)。如果你可以严格指定你的启发式方法,当然它们肯定可以被编程,但是如果它们很奇怪而又古怪,你不可能在现有包装中找到它们已预先编程; - )。

答案 3 :(得分:2)

根据您的澄清......我认为您可以这样做:

from datetime import *
>>> today = datetime.today()
>>> todayAtThreeAm = datetime(today.year, today.month, today.day, 3)
>>> todayAtThreeAm
datetime.datetime(2010, 3, 12, 3, 0)
>>> nextFridayAtThreeAm = todayAtThreeAm + timedelta(12 - today.isoweekday())
>>> nextFridayAtThreeAm
datetime.datetime(2010, 3, 19, 3, 0)

注意星期一到星期日,isoweekday()会返回1到7。 12表示下一周的星期五。所以12 - today.isoweekday()为您提供了今天需要添加的正确时间增量。

希望这有帮助。

答案 4 :(得分:1)

使用pendulum,您可以执行以下操作:

  ...
  const answer = 'This is my audio buffer'.toString('base64');
  response.body = JSON.stringify({
    id: 123,
    myBuffer: answer
  });
  return response;
};

请注意,此行有两个In [15]: pendulum.now().next(pendulum.FRIDAY).next(pendulum.FRIDAY).add(hours=3) Out[15]: DateTime(2019, 5, 3, 3, 0, 0, tzinfo=Timezone('America/Los_Angeles'))

要将其转换为字符串,

next Friday