Basically I have the inverse of this problem: Python Time Seconds to h:m:s
I have a string in the format H:MM:SS (always 2 digits for minutes and seconds), and I nee
Just a simple generalization to the great response of taskinoor
In the context of my problem the format is similar, but includes AM or PM.
Format 'HH:MM:SS AM' or 'HH:MM:SS PM'
For this case the function changes to:
def get_sec(time_str):
"""Get Seconds from time."""
if 'AM' in time_str:
time_str = time_str.strip('AM')
h, m, s = time_str.split(':')
seconds = int(h) * 3600 + int(m) * 60 + int(s)
if 'PM' in time_str:
time_str = time_str.strip('PM')
h, m, s = time_str.split(':')
seconds = (12 + int(h)) * 3600 + int(m) * 60 + int(s)
return seconds