# 1. 一个实例

#Station  Line  A/D  Time
Arènes    T1    A    05:27:00
Arènes    64    D    05:30:00
Arènes    T2    D    05:35:00
Arènes    T1    A    05:38:00
Arènes    T1    D    05:45:00
Arènes    65    D    05:55:00


class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)   # 注意参数的顺序

# A duration expressing the difference between two date, time, or datetime instances to microsecond resolution. Arguments may be integers or floats, and may be positive or negative.



# 2. 字符串转换成timedelta

from datetime import timedelta

def convert2timedelta(s):
"""convert string, like 00:00:00, to timedelta"""
tm = map(int, s.split(':'))
return timedelta(hours=tm[0], minutes=tm[1], seconds=tm[2])  # convert to timedelta


Python3支持的操作很丰富，包括+, -, *, /, //, %, divmod。除此之外，timedelta是hashable(作为字典的key)，还可以进行比较==, !=。

# 3. timedelta

timedelta属性和方法如下:

str(tm)                    # 返回的格式是：[D day[s], ][H]H:MM:SS[.UUUUUU]， 如'1 day, 5:05:06.000001'
timedelta.total_seconds()  # 返回总的秒数，等同于td / timedelta(seconds=1)

timedelta.min              # 最小值，'-999999999 days, 0:00:00'
timedelta.max              # 最大值，'999999999 days, 23:59:59.999999'
timedelta.resolution       # 精度，0:00:00.000001
repr(tm)                   # 返回的格式是：datetime.timedelta(D[, S[, U]])，如'datetime.timedelta(1, 18306, 1)'