timedelta对象是用来表示时间的区间,主要是两个日期或时间之差。
class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
所有输入参数都是可选的,默认都设置为0。参数可以是整数或浮点数,可以正数,也可以是负数。在这个类里直接保存三个变量:天数、秒和微秒。其它参数都按下面的单位进行转换:
l 一毫秒等于1000微秒
l 一分钟等于60秒
l 一小时等于3600秒
l 一周等于7天
根据上面进制表示,天数、秒和微秒都有一个上下限表示范围:
l 0 <= 微秒 < 1000000
l 0 <= 秒 < 3600*24
l -999999999 <= 天数 <= 999999999
如果有浮点数的参数,并且在参数后面有小数部分的微秒,这些浮点参数的小数部分会以“结果为偶数的方式对小数部分丢掉”(round-half-to-even)。如果没有浮点参数,就不会出现这种丢掉小数部分的处理,并且严格地与整数运算一样。当进行转换为内部表示时,如果超出所能表示的范围就会抛出异常OverflowError。
值得注意是在对象timedelta是可以用负数表示,如下:
#python 3.4.3
import datetime
d = datetime.timedelta(microseconds=-1)
print(d.days, d.seconds, d.microseconds)
结果输出如下:
-1 86399 999999
timedelta类拥有的属性如下:
timedelta.min
表示最小的负值对象timedelta(-999999999).
例子:
#python 3.4.3
import datetime
d = datetime.timedelta(microseconds = 0)
print(datetime.timedelta.min)
结果输出如下:
-999999999 days, 0:00:00
timedelta.max
表示最大正值的对象timedelta(days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999).
例子:
#python 3.4.3
import datetime
d = datetime.timedelta(microseconds = 0)
print(datetime.timedelta.min)
print(datetime.timedelta.max)
结果输出如下:
-999999999 days, 0:00:00
999999999 days, 23:59:59.999999
timedelta.resolution
表示最小精度分隔对象timedelta(microseconds=1)
例子:
#python 3.4.3
import datetime
d = datetime.timedelta(microseconds = 0)
print(datetime.timedelta.min)
print(datetime.timedelta.max)
print(datetime.timedelta.resolution)
结果输出如下:
-999999999 days, 0:00:00
999999999 days, 23:59:59.999999
0:00:00.000001
值得注意的是由于转换存在精度的丢失,所以timedelta.max > -timedelta.min。-timedelta.max已经超出表示范围,所以不再作为timedelta对象看待。
timedelta实例只读属性:
属性 |
值 |
days |
在范围-999999999 和 999999999 之间。 |
seconds |
在范围0和86399之间。 |
microseconds |
在范围0和999999之间。 |
例子:
#python 3.4.3
import datetime
d = datetime.timedelta(microseconds = 12348850005600)
print(d.days)
print(d.seconds)
print(d.microseconds)
结果输出如下:
142
80050
5600
timedelta类支持的操作:
两个对象相加t1= t2 + t3
表示两个时间变量对象之和。并且t1-t2 == t3 和 t1-t3 == t2都应返回True。
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 0)
t3 = datetime.timedelta(microseconds = 8)
t1 = t2 + t3
print('t1 - t2 = :', t1 - t2 == t3)
print(t1.days)
print(t1.seconds)
print(t1.microseconds)
结果输出如下:
t1 - t2 = : True
0
0
8
两对象相减t1 = t2 - t3
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 0)
t3 = datetime.timedelta(microseconds = 8)
t1 = t2 - t3
print('t1 - t2 = :', t1 + t3 == t2)
print(t1.days)
print(t1.seconds)
print(t1.microseconds)
结果输出如下:
t1 - t2 = : True
-1
86399
999992
与整数相乘t1 = t2*i 或 t1 = i*t2
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
t1 = t2*9
t4 = t3*9
print(t1.days)
print(t1.seconds)
print(t1.microseconds)
print(t4.days)
print(t4.seconds)
print(t4.microseconds)
结果输出如下:
0
0
81
9
0
72
与浮点数相乘t1 = t2*f 或t1 = f*t2
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
t1 = t2*9.5
t4 = t3*9.5
print(t1.days)
print(t1.seconds)
print(t1.microseconds)
print(t4.days)
print(t4.seconds)
print(t4.microseconds)
结果输出如下:
0
0
86
9
43200
76
通过结果可以看出,浮点数的时间是按向偶数取整方式进行舍掉小数部分,比如85.5向偶数取整就是86。
两个时间变量对象相除f = t2 / t3
结果返回一个浮点数据对象。
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
t1 = t2 / t3
print(t1)
结果输出如下:
1.041666666570216e-10
与浮点数或整数相除t1 = t2 / f 或 t1 = t2 / i
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
t1 = t2 / 0.5
t4 = t3 / 9
print(t1)
print(t1.microseconds)
print(t4)
print(t4.microseconds)
结果输出如下:
0:00:00.000018
18
2:40:00.000001
1
与整数或时间变量对象整除t1 = t2 // i 或 t1 = t2 // t3
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
t1 = t2 // 5
t4 = t3 // t2
print(t1)
print(t1.microseconds)
print(t4)
结果输出如下:
0:00:00.000001
1
9600000000
两个时间变量整除出来是一个整数。
两个时间变量取模t1 = t2 % t3
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
t1 = t2 % t3
print(t1)
print(t1.microseconds)
结果输出如下:
0:00:00.000009
9
两个时间变量取模之后,还是时间变量对象。
商和余数运算q, r = divmod(t1, t2)
相当于这样计算:q = t1 // t2 和r = t1 % t2, q是整数,r是一个timedelta对象。
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
q, r = divmod(t2, t3)
print(1)
print(r.microseconds)
结果输出如下:
1
9
正号运算+t1
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
t1 = +t2
t4 = +t3
print(t2, t3)
print(t1, t4)
结果输出如下:
0:00:00.000009 1 day, 0:00:00.000008
0:00:00.000009 1 day, 0:00:00.000008
负号运算-t1
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
t1 = -t2
t4 = -t3
print(t2, t3)
print(t1, t4)
结果输出如下:
0:00:00.000009 1 day, 0:00:00.000008
-1 day, 23:59:59.999991 -2 days, 23:59:59.999992
对timedelta对象取绝对值abs(t)
相当于当t大于0时,采用+t运算;当t小于0时采用-t运算。
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
t1 = abs(-t2)
t4 = -t3
print(t2, t3)
print(t1, t4)
结果输出如下:
0:00:00.000009 1 day, 0:00:00.000008
0:00:00.000009 -2 days, 23:59:59.999992
格式化字符串运算str(t)
把timedelta对象按格式[D day[s], ][H]H:MM:SS[.UUUUUU]来格式化。当t是负值的,那么D也是负值的。
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
print(str(t2), str(-t3))
结果输出如下:
0:00:00.000009 -2 days, 23:59:59.999992
repr(t)运算
以方式:datetime.timedelta(D[, S[, U]])来格式timedelta对象。
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
print(repr(t2), repr(-t3))
结果输出如下:
datetime.timedelta(0, 0, 9) datetime.timedelta(-2, 86399, 999992)
timedelta.total_seconds()
返回时间变量里包含所有秒数。
例子:
#python 3.4.3
import datetime
t2 = datetime.timedelta(microseconds = 9)
t3 = datetime.timedelta(days = 1, microseconds = 8)
print(repr(t2), repr(-t3))
print(t2.total_seconds(), t3.total_seconds())
结果输出如下:
datetime.timedelta(0, 0, 9) datetime.timedelta(-2, 86399, 999992)
9e-06 86400.000008
蔡军生 QQ:9073204 深圳