'dateutil.relativedelta.relativedelta' 中的单复数参数有何区别?
What are differences between the singular and plural argument in 'dateutil.relativedelta.relativedelta'?
摘自dateutil.relativedelta.relativedelta
、
的描述
year, month, day, hour, minute, second, microsecond:
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.
years, months, weeks, days, hours, minutes, seconds, microseconds:
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
中的单复数参数还有哪些区别?
单数参数是绝对信息,基本上你可以认为 relativedelta(month=3)
表示 "the month of March, relative to whatever date and time it is applied to"(即替换 month
关键字)。这不适用于乘法和除法等运算,因此这些运算对绝对信息没有影响:
>>> relativedelta.relativedelta(month=3) * 3
relativedelta(month=3)
复数参数是相对的 offsets,所以他们说 "give me this many months after/before the date"。由于这些是偏移量,因此它们适用于乘法和除法:
>>> relativedelta.relativedelta(months=3) * 3
relativedelta(months=9)
tzrange
class 是使用它的一个很好的例子,它使用 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)
为您提供指定日期或之后的第一个星期日(这在 month
和 day
参数之后应用,因此当设置 day
到 1,您将获得该月的第一个星期日)。
hours=+2
- 这将在应用所有其他内容后给您 2 小时。可能在这种情况下 hour=2
对于演示这个概念更有意义,但是,因为这些 relativedelta
s 实际被使用的地方我认为首先去除时间部分(在午夜给出日期),所以这是真的想编码凌晨 2 点。
摘自dateutil.relativedelta.relativedelta
、
year, month, day, hour, minute, second, microsecond:
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.
years, months, weeks, days, hours, minutes, seconds, microseconds:
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
中的单复数参数还有哪些区别?
单数参数是绝对信息,基本上你可以认为 relativedelta(month=3)
表示 "the month of March, relative to whatever date and time it is applied to"(即替换 month
关键字)。这不适用于乘法和除法等运算,因此这些运算对绝对信息没有影响:
>>> relativedelta.relativedelta(month=3) * 3
relativedelta(month=3)
复数参数是相对的 offsets,所以他们说 "give me this many months after/before the date"。由于这些是偏移量,因此它们适用于乘法和除法:
>>> relativedelta.relativedelta(months=3) * 3
relativedelta(months=9)
tzrange
class 是使用它的一个很好的例子,它使用 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)
为您提供指定日期或之后的第一个星期日(这在month
和day
参数之后应用,因此当设置day
到 1,您将获得该月的第一个星期日)。hours=+2
- 这将在应用所有其他内容后给您 2 小时。可能在这种情况下hour=2
对于演示这个概念更有意义,但是,因为这些relativedelta
s 实际被使用的地方我认为首先去除时间部分(在午夜给出日期),所以这是真的想编码凌晨 2 点。