为什么带有正参数的 relativedelta return 是过去的日期?
Why does relativedelta with positive arguments return a date in the past?
我无法理解 dateutil.relativedelta 的行为。
我知道如果我使用 relativedelta doc.
中指定的否定参数,relativedelta 可以 return 过去的日期
但是,当我提供正参数时,我希望它总是 return 将来的日期......这似乎是合法的吧?
我的用例如下:我们是星期二,现在是 8:35。我想在 6:00 获取最近的星期一和星期二的日期。
这是我所做的。第一个结果对我来说是正确的,而第二个结果是错误的。
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2016, 11, 29, 8, 35, 23, 786349)
>>> from dateutil import relativedelta
>>> now.weekday()
1
>>> now + relativedelta.relativedelta(weekday=0, hour=6, minute=0) # should give a time in the future
datetime.datetime(2016, 12, 5, 6, 0, 23, 786349) # here this is correct, in the future
>>> now + relativedelta.relativedelta(weekday=1, hour=6, minute=0) # should give a time in the future
datetime.datetime(2016, 11, 29, 6, 0, 23, 786349) # but this is in the past / I would expect result (2016, 12, 6, 6, 0, 23, 786349)
那么,我是不是做错了什么?
所以根据你的初始日期,你实际上是早上 8 点,但是你通过使用 hour
参数将目标定为早上 6 点,如果你想增加一小时,你应该使用 hours
和minutes
分别
>>> now
datetime.datetime(2016, 11, 29, 3, 5, 41, 763818)
>>> now.weekday()
1
>>> now + relativedelta.relativedelta(weekday=1, hour=1)
datetime.datetime(2016, 11, 29, 1, 5, 41, 763818) # Notice how it's in the past
>>> now + relativedelta.relativedelta(weekday=1, hours=1)
datetime.datetime(2016, 11, 29, 4, 5, 41, 763818) # Notice how it's one hour in the future
>>> n + relativedelta.relativedelta(weekday=1, hour=6, minute=0, weeks=1)
datetime.datetime(2016, 12, 6, 6, 0, 41, 763818)
我认为它在文档中:
开始于,大约 weekday
:
These instances may receive a parameter N, specifying the Nth weekday, which could be positive or negative (like MO(+1) or MO(-2). Not specifying it is the same as specifying +1.
所以通过 1
,就像您通过了 (1, 1)
然后,继续文档,在 behavior of operations with relativedelta
的第 7 个点:
Notice that if the calculated date is already Monday, for example, using (0, 1) or (0, -1) won’t change the day.
所以 11 月 29 日已经是星期二了,而您要的是星期二。
所以什么都没有改变。
我无法理解 dateutil.relativedelta 的行为。 我知道如果我使用 relativedelta doc.
中指定的否定参数,relativedelta 可以 return 过去的日期但是,当我提供正参数时,我希望它总是 return 将来的日期......这似乎是合法的吧?
我的用例如下:我们是星期二,现在是 8:35。我想在 6:00 获取最近的星期一和星期二的日期。
这是我所做的。第一个结果对我来说是正确的,而第二个结果是错误的。
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2016, 11, 29, 8, 35, 23, 786349)
>>> from dateutil import relativedelta
>>> now.weekday()
1
>>> now + relativedelta.relativedelta(weekday=0, hour=6, minute=0) # should give a time in the future
datetime.datetime(2016, 12, 5, 6, 0, 23, 786349) # here this is correct, in the future
>>> now + relativedelta.relativedelta(weekday=1, hour=6, minute=0) # should give a time in the future
datetime.datetime(2016, 11, 29, 6, 0, 23, 786349) # but this is in the past / I would expect result (2016, 12, 6, 6, 0, 23, 786349)
那么,我是不是做错了什么?
所以根据你的初始日期,你实际上是早上 8 点,但是你通过使用 hour
参数将目标定为早上 6 点,如果你想增加一小时,你应该使用 hours
和minutes
分别
>>> now
datetime.datetime(2016, 11, 29, 3, 5, 41, 763818)
>>> now.weekday()
1
>>> now + relativedelta.relativedelta(weekday=1, hour=1)
datetime.datetime(2016, 11, 29, 1, 5, 41, 763818) # Notice how it's in the past
>>> now + relativedelta.relativedelta(weekday=1, hours=1)
datetime.datetime(2016, 11, 29, 4, 5, 41, 763818) # Notice how it's one hour in the future
>>> n + relativedelta.relativedelta(weekday=1, hour=6, minute=0, weeks=1)
datetime.datetime(2016, 12, 6, 6, 0, 41, 763818)
我认为它在文档中:
开始于,大约 weekday
:
These instances may receive a parameter N, specifying the Nth weekday, which could be positive or negative (like MO(+1) or MO(-2). Not specifying it is the same as specifying +1.
所以通过 1
,就像您通过了 (1, 1)
然后,继续文档,在 behavior of operations with relativedelta
的第 7 个点:
Notice that if the calculated date is already Monday, for example, using (0, 1) or (0, -1) won’t change the day.
所以 11 月 29 日已经是星期二了,而您要的是星期二。
所以什么都没有改变。