In [1]:
import datetime

In [2]:
td = datetime.timedelta(weeks=1, days=1, hours=1, minutes=1,
                        seconds=1, milliseconds=1, microseconds=1)

In [3]:
print(td)


8 days, 1:01:01.001001

In [4]:
print(type(td))


<class 'datetime.timedelta'>

In [5]:
print(datetime.timedelta(days=0.5, hours=-6, minutes=120))


8:00:00

In [6]:
td = datetime.timedelta(weeks=1, days=1, hours=1, minutes=1,
                        seconds=1, milliseconds=1, microseconds=1)

In [7]:
print(td)


8 days, 1:01:01.001001

In [8]:
print(type(td))


<class 'datetime.timedelta'>

In [9]:
print(td.days)


8

In [10]:
print(type(td.days))


<class 'int'>

In [11]:
print(td.seconds)


3661

In [12]:
print(type(td.seconds))


<class 'int'>

In [13]:
print(td.microseconds)


1001

In [14]:
print(type(td.microseconds))


<class 'int'>

In [15]:
print(td.total_seconds())


694861.001001

In [16]:
print(type(td.total_seconds()))


<class 'float'>

In [17]:
print(td.days * 24 * 60 * 60 + td.seconds + td.microseconds / 1000000)


694861.001001

In [18]:
s = str(td)

In [19]:
print(s)


8 days, 1:01:01.001001

In [20]:
print(type(s))


<class 'str'>

In [21]:
td = datetime.timedelta(days=1, hours=1, milliseconds=100)

In [22]:
sec = td.total_seconds()

In [23]:
print(sec)


90000.1

In [24]:
print(type(sec))


<class 'float'>

In [25]:
sec = datetime.timedelta(days=1, hours=1, milliseconds=100).total_seconds()

In [26]:
print(sec)


90000.1

In [27]:
td = datetime.timedelta(seconds=123456.789)

In [28]:
print(td)


1 day, 10:17:36.789000

In [29]:
s = str(td)

In [30]:
print(s)


1 day, 10:17:36.789000

In [31]:
print(type(s))


<class 'str'>

In [32]:
print(td.days)


1

In [33]:
print(td.seconds)


37056

In [34]:
print(td.microseconds)


789000

In [35]:
def get_h_m_s(td):
    m, s = divmod(td.seconds, 60)
    h, m = divmod(m, 60)
    return h, m, s

In [36]:
print(get_h_m_s(td))


(10, 17, 36)

In [37]:
print(type(get_h_m_s(td)))


<class 'tuple'>

In [38]:
h, m, s = get_h_m_s(td)

In [39]:
print(h)


10

In [40]:
print(m)


17

In [41]:
print(s)


36

In [42]:
def get_d_h_m_s_us(sec):
    td = datetime.timedelta(seconds=sec)
    m, s = divmod(td.seconds, 60)
    h, m = divmod(m, 60)
    return td.days, h, m, s, td.microseconds

In [43]:
print(get_d_h_m_s_us(123456.789))


(1, 10, 17, 36, 789000)

In [44]:
print(get_d_h_m_s_us(123.456789))


(0, 0, 2, 3, 456789)

In [45]:
td_m = datetime.timedelta(seconds=-123456.789)

In [46]:
print(td_m)


-2 days, 13:42:23.211000

In [47]:
print(td_m.days)


-2

In [48]:
print(td_m.seconds)


49343

In [49]:
print(td_m.microseconds)


211000

In [50]:
print(td_m.total_seconds())


-123456.789

In [51]:
print(get_h_m_s(td_m))


(13, 42, 23)

In [52]:
print(get_d_h_m_s_us(-123456.789))


(-2, 13, 42, 23, 211000)

In [53]:
print(abs(td_m))


1 day, 10:17:36.789000

In [54]:
print(get_h_m_s(abs(td_m)))


(10, 17, 36)

In [55]:
print(get_d_h_m_s_us(abs(-123456.789)))


(1, 10, 17, 36, 789000)