I have a method which takes two variables:
- Current Time
- Minutes
The method is pretty simple. The user inputs the two values, the first being a string eg. ’23:01′
The second being an integer eg 50. So the method calculates the new time by adding the two together, so the new time should be 23:51.
I get this to work, however when the time becomes 24:01 + 60 it becomes 25:01 which is an incorrect time.
How do I simply use the time method in python to add the times?
Here is my code so far which throws up an error
import time
class timeCalc(object):
def nextTime(self, time, minutes):
t = time.split(':')
mins = int(t[0]) * 60 + int(t[1]) + minutes
h = str(mins/60)
m = str(mins%60)
if len(h) == 1:
h = '0' + h
if len(m) == 1:
m = '0' + m
nTime = time.strptime(h + ':' + m, "%H:%M")
print nTime
ts = timeCalc()
ts.nextTime('23:01',60)
you don’t need parse the time parameter on that way, you can do:
Line 4:
You format the ‘time’ parameter to a datetime object with format %H:%M called base_time
Line 5:
You convert the minutestoadd parameter to a compatible object format to add
Line 6:
The sum of both datetime object values
Line 7:
Print the result on a format like 23:40:00