How to add delta to python datetime.time?

PythonDatetimeTimeTimedelta

Python Problem Overview


From:

http://docs.python.org/py3k/library/datetime.html#timedelta-objects

> A timedelta object represents a duration, the difference between two > dates or times.

So why i get error with this:

>>> from datetime import datetime, timedelta, time
>>> datetime.now() + timedelta(hours=12)
datetime.datetime(2012, 9, 17, 6, 24, 9, 635862)
>>> datetime.now().date() + timedelta(hours=12)
datetime.date(2012, 9, 16)

>>> datetime.now().time() + timedelta(hours=12)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'

Python Solutions


Solution 1 - Python

datetime.time objects do not support addition with datetime.timedeltas.

There is one natural definition though, clock arithmetic. You could compute it like this:

import datetime as dt
now = dt.datetime.now()
delta = dt.timedelta(hours = 12)
t = now.time()
print(t)
# 12:39:11.039864

print((dt.datetime.combine(dt.date(1,1,1),t) + delta).time())
# 00:39:11.039864

dt.datetime.combine(...) lifts the datetime.time t to a datetime.datetime object, the delta is then added, and the result is dropped back down to a datetime.time object.

Solution 2 - Python

All the solutions above are too complicated, OP had already shown that we can do calculation between datetime.datetime and datetime.timedelta, so why not just do:

(datetime.now() + timedelta(hours=12)).time()

Solution 3 - Python

How would this work? datetime.datetime.now().time() returns only hours, minutes, seconds and so on, there is no date information what .time() returns, only time.

Then, what should 18:00:00 + 8 hours return?
There's not answer to that question, and that's why you can't add a time and a timedelta.

In other words:

18:28:44, Sep. 16, 2012 + 8 hours #makes sense: it's 2:28:44, Sep. 17, 2012
18:28:44 + 8 hours # Doesn't make sense.

Solution 4 - Python

Here is a function that adds a time to a timedelta:

def time_plus(time, timedelta):
    start = datetime.datetime(
        2000, 1, 1,
        hour=time.hour, minute=time.minute, second=time.second)
    end = start + timedelta
    return end.time()

This will provide the expected result so long as you don't add times in a way that crosses a midnight boundary.

Solution 5 - Python

A datetime.time object can be split into separate integer components that you can add to. No need for timedelta eg:

from datetime import datetime, time
    time_now = datetime.now().time()
    twelve_hours_time =  time(time_now.hour + 12, time_now.minute)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionxliivView Question on Stackoverflow
Solution 1 - PythonunutbuView Answer on Stackoverflow
Solution 2 - PythonsswordView Answer on Stackoverflow
Solution 3 - PythonThomas OrozcoView Answer on Stackoverflow
Solution 4 - PythonDavid FosterView Answer on Stackoverflow
Solution 5 - PythonMartin JonesView Answer on Stackoverflow