I've had a look at the other questions relating to strptime and Unconverted Data Remain, but struggling to see where I am going wrong within the code.
currentTime = format(currentTime, '%H:%M:%S')
#remove the current time from phone time
timeDifferenceValue = datetime.datetime.strptime(str(currentTime), FMS) - datetime.datetime.strptime(
str(ptime) + ":00", FMS)
else:
time_left = time_left[-7:]
time_leftHatch = datetime.datetime.strptime(time_left, FMS) - timeDifferenceValue
time_leftHatch = format(time_leftHatch, '%H:%M:%S')
The error was being found at this stage:
time_leftHatch = datetime.datetime.strptime(time_left, FMS) - timeDifferenceValue
The values of timeleft and timeDifference Value are:
time_left = 1:29:47 timeDifferenceValue = 0:13:31
The Error: unconverted data remains: :47
John in the comments mentioned I should change remove the excessive use of strptime when it is unneccessary. This seems to have resolve the Unconverted Data Remains.
For example, timeDifferenceValue was already in the format of time, so this didn't need changed at all.
Any ideas?
The immediate problem is this:
datetime.datetime.strptime("0:"+str(time_left), FMS)
I'm not sure why you're prepending "0:" but that results in "0:1:29:47" for which your strptime format only parses the first three values. The "unconverted data" error is letting you know that there's some extra information in your input string that the format string isn't handling.
The better fix, though, is to stop stringifying and strptiming which will continue to cause you grief. You already have some hints of using timedelta
in your code. Taking the difference between times in Python returns a timedelta, and adding a timedelta to a time returns a new time. Doing it this way keeps you in structured data and well-defined results.
For example,
# do this early, don't go back to string
phone_time = datetime.datetime.strptime(ptime + ':00', FMS)
# and leave this a datetime instead of stringifying it later
currentTime = datetime.datetime.now() + timedelta(hours=1)
# now you can just take the difference
timeDifferenceValue = currentTime - ptime
And:
if str(timeDifferenceValue).__contains__("-"):
becomes
if timeDifferenceValue < timedelta(0):
Hope that gets you pointed in the right direction!