Given a time in 12-hour AM/PM format, convert it to military (24-hour) time. Note : Midnight is 12:00:00 AM on a 12-hour clock and 00:00:00 on a 24-hour clock. Noon is 12:00:00 PM on 12-hour clock and 12:00:00 on 24-hour clock. Examples :
Input : 11:21:30 PM
Output : 23:21:30
Input : 12:12:20 AM
Output : 00:12:20
How to Convert AM/PM to 24 Hour Time
Whether the time format is 12 hours or not, can be found out by using list slicing. Check if last two elements are PM, then simply add 12 to them. If AM, then don’t add. Remove AM/PM from the updated time. Below is the implementation :
Python3
def convert24(str1):
if str1[ - 2 :] = = "AM" and str1[: 2 ] = = "12" :
return "00" + str1[ 2 : - 2 ]
elif str1[ - 2 :] = = "AM" :
return str1[: - 2 ]
elif str1[ - 2 :] = = "PM" and str1[: 2 ] = = "12" :
return str1[: - 2 ]
else :
return str ( int (str1[: 2 ]) + 12 ) + str1[ 2 : 8 ]
print (convert24( "08:05:45 PM" ))
|
Output :
20:05:45
Time Complexity: O(1)
Auxiliary Space: O(1)
Here is another approach to the problem that uses the datetime module in Python to convert the time from 12-hour to 24-hour format:
Python3
from datetime import datetime
def convert24(time):
t = datetime.strptime(time, '%I:%M:%S %p' )
return t.strftime( '%H:%M:%S' )
print (convert24( '11:21:30 PM' ))
print (convert24( '12:12:20 AM' ))
|
This approach has the advantage of handling invalid time formats and edge cases, such as the time being in an invalid format or the hours being greater than 12. It also allows for the input time to be in any valid time format recognized by the datetime module, such as using a single digit for the hour or using a different separator between the time parts.
The time complexity of this approach is O(1), as it only involves parsing and formatting the time string.
Using regular expression
Use regular expressions to extract the hour, minute, second, and AM/PM parts from the input time string. Apply the necessary logic to convert the hour to 24-hour format. Format the hour, minute, and second parts into the desired 24-hour time format.
Algorithm
1. Use regular expressions to extract the hour, minute, second, and AM/PM parts from the input time string.
2. If AM/PM is “PM” and hour is not equal to 12, add 12 to the hour value
3. If AM/PM is “AM” and hour is equal to 12, set hour to 0
4. Format the time into 24-hour format and return the result
Python3
import re
def convert_to_24hour(time_str):
hour, minute, second, am_pm = re.findall( '\d+|\w+' , time_str)
hour = int (hour)
if am_pm = = 'PM' and hour ! = 12 :
hour + = 12
elif am_pm = = 'AM' and hour = = 12 :
hour = 0
return f '{hour:02d}:{minute}:{second}'
print (convert_to_24hour( '11:21:30 PM' ))
print (convert_to_24hour( '12:12:20 AM' ))
|
Time complexity: O(1)
Space complexity: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!