Program structure
timeconversion
is not a very descriptive function name. What is it converting from, and what is it converting to? I suggest convert_12h_to_24h
as a function name. It would be better for the function to return its result rather than printing it, so that the caller of the function can decide what to do with the result.
A function should have a docstring, especially a function like this that expects its input to be in a very specific format.
You should put the function definition first. Plopping a function definition into the middle of the "main" program makes your code harder to understand.
So far, we would have…
def convert_12h_to_24h(s):
"""
Convert a time from a 12-hour format into 24-hour format.
The input should be a 10-character string like "01:23:45PM", and the
output would be "13:23:45".
"""
…
return …
s = "05:43:12AM"
print(convert_12h_to_24h(s))
Variables
zone
is a poor choice of variable name, because it makes me think of timezones. I don't know exactly what the right terminology is for a half-day, but I'd choose am_pm
for the variable name.
Likewise, time
is a poor variable name. You could just call it hour
again.
It's a bit weird that hour
is singular, but minutes
and seconds
are plural.
l = list(s.strip(':'))
is pointless — you could just use s
directly.
The multiple variable assignment statements for extracting parts of the string could be written more elegantly using string slices and multiple assignment:
hours, minutes, seconds, am_pm = s[0:2], s[3:5], s[6:8], s[8:10]
I'd recommend dissecting the string using indexing from the end, so that it works whether or not there is a leading 0
:
hours, minutes, seconds, am_pm = s[-10:-8], s[-7:-5], s[-4:-2], s[-2:]
Putting it all together
One additional change I'd make is to swap the first two branches if your if-elif
chain for consistency.
def convert_12h_to_24h(s):
"""
Convert a time from a 12-hour format into 24-hour format.
The input should be a 10-character string like "01:23:45PM", and the
output would be "13:23:45".
"""
hours, minutes, seconds, am_pm = s[-10:-8], s[-7:-5], s[-4:-2], s[-2:]
if am_pm == 'PM' and hours == '12':
return f"{hours}:{minutes}:{seconds}"
elif am_pm == 'PM':
hours = int(hours) + 12
return f"{hours}:{minutes}:{seconds}"
elif am_pm == 'AM' and hours == '12':
return f"00:{minutes}:{seconds}"
elif am_pm == 'AM':
return f"{hours}:{minutes}:{seconds}"
#s normally is input, but this code will only work with this type of string input
s = "05:43:12AM"
print(convert_12h_to_24h(s))
Better yet, notice that there are only two cases for altering hours
:
def convert_12h_to_24h(s):
"""
Convert a time from a 12-hour format into 24-hour format.
The input should be a 10-character string like "01:23:45PM", and the
output would be "13:23:45".
"""
hours, minutes, seconds, am_pm = s[-10:-8], s[-7:-5], s[-4:-2], s[-2:]
if am_pm == 'PM' and hours != '12':
hours = int(hours) + 12
elif am_pm == 'AM' and hours == '12':
hours = '00'
return f"{hours}:{minutes}:{seconds}"
#s normally is input, but this code will only work with this type of string input
s = "05:43:12AM"
print(convert_12h_to_24h(s))