I'm trying to do something like this:
time() + timedelta(hours=1)
however, python has no allowance for it, apparently for good reason http://bugs.python.org/issue1487389
Does anyone have a simple work around?
I'm trying to do something like this:
time() + timedelta(hours=1)
however, python has no allowance for it, apparently for good reason http://bugs.python.org/issue1487389
Does anyone have a simple work around?
Workaround:
t = time()
t2 = time(t.hour+1, t.minute, t.second, t.microsecond)
You can also omit the microseconds, if you don't need that much precision.
This is a bit nasty, but:
from datetime import datetime, timedelta
now = datetime.now().time()
# Just use January the first, 2000
d1 = datetime(2000, 1, 1, now.hour, now.minute, now.second)
d2 = d1 + timedelta(hours=1, minutes=23)
print d2.time()
The solution is in the link that you provided in your question:
datetime.combine(date.today(), time()) + timedelta(hours=1)
Full example:
from datetime import date, datetime, time, timedelta
dt = datetime.combine(date.today(), time(23, 55)) + timedelta(minutes=30)
print dt.time()
Output:
00:25:00