【问题标题】:What are differences between the singular and plural argument in 'dateutil.relativedelta.relativedelta'?'dateutil.relativedelta.relativedelta' 中的单数和复数参数有什么区别?
【发布时间】:2016-09-20 20:43:24
【问题描述】:

摘自dateutil.relativedelta.relativedelta的描述,

年、月、日、时、分、秒、微秒:

Absolute information (argument is singular); adding or subtracting a
relativedelta with absolute information does not perform an aritmetic
operation, but rather REPLACES the corresponding value in the
original datetime with the value(s) in relativedelta.

年、月、周、日、小时、分钟、秒、微秒:

Relative information, may be negative (argument is plural); adding
or subtracting a relativedelta with relative information performs
the corresponding aritmetic operation on the original datetime value
with the information in the relativedelta.

我可以从下面的例子中看出加减法的不同。

>>> from datetime import datetime
>>> from dateutil.relativedelta import relativedelta
>>> now = datetime.now()
>>> str(now)
'2016-05-23 22:32:48.427269'
>>> singular = relativedelta(month=3)
>>> plural = relativedelta(months=3)

# subtracting 
>>> str(now - singular)             # replace the corresponding value in the original datetime with the value(s) in relativedelta
'2016-03-23 22:32:48.427269'
>>> str(now - plural)               # perform the corresponding aritmetic operation on the original datetime value with the information in the relativedelta.
'2016-02-23 22:32:48.427269'

# adding
>>> str(now + singular)             # replace the corresponding value in the original datetime with the value(s) in relativedelta
'2016-03-23 22:32:48.427269'
>>> str(now + plural)               # perform the corresponding aritmetic operation on the original datetime value with the information in the relativedelta.
'2016-08-23 22:32:48.427269'

除此之外,relativedelta 中的单数和复数参数还有什么区别?

【问题讨论】:

  • “其他差异”是什么意思?您似乎非常有效地捕捉到了整体差异。
  • @Paul,我只是想确保我能很好地理解这些差异,这样我以后就不会犯错了。

标签: python python-dateutil relativedelta


【解决方案1】:

单数参数是绝对信息,基本上你可以认为relativedelta(month=3) 表示“三月,相对于它所应用的任何日期和时间”(即替换month 关键字)。这不适用于乘法和除法等运算,因此这些运算对绝对信息没有影响:

>>> relativedelta.relativedelta(month=3) * 3
relativedelta(month=3)

复数参数是相对的偏移量,所以他们说,“在日期之后/之前给我这么多月”。由于这些是偏移量,因此它们可以进行乘法和除法:

>>> relativedelta.relativedelta(months=3) * 3
relativedelta(months=9)

tzrange 类是一个很好的例子,它使用relativedelta 来模拟 POSIX 样式 TZ 字符串的行为。可以看到this test,它使用了如下对象:

tz.tzrange('EST', -18000, 'EDT', -14400,
       start=relativedelta(hours=+2, month=4, day=1, weekday=SU(+1)),
       end=relativedelta(hours=+1, month=10, day=31, weekday=SU(-1)))

这会构造一个等效于'EST5EDT' 的时区,其中start relativedelta 被添加到给定年份中的任何日期以查找该年DST 的开始,end 被添加到指定年份中的任何日期给定年份以查找该年 DST 的结束时间。

分解:

  • month 给你一个四月的约会
  • day 让你在月初开始
  • weekday=SU(+1) 为您提供指定日期或之后的第一个星期日(这是在 monthday 参数之后应用的,因此当 day 设置为 1 时,您将获得该月的第一个星期日)。
  • hours=+2 - 在所有其他东西都被应用后,这会给你 2 小时。不过,在这种情况下,hour=2 可能更适合展示这个概念,因为这些relativedeltas 实际被使用的地方我认为首先会去掉时间部分(在午夜给出一个日期),所以这真的是在尝试编码凌晨 2 点。

【讨论】:

    猜你喜欢
    • 2020-05-25
    • 2018-05-14
    • 2010-09-14
    • 1970-01-01
    • 2015-08-09
    • 2013-01-22
    • 2013-11-25
    • 2019-09-04
    相关资源
    最近更新 更多