Time Conversion

  • + 0 comments
    def timeConversion(s):
        # get the time indicator
        time_indicator = s[-2:]
        
        # extract minutes and seconds because they stay fixed
        # format (e.g.): ":00:00"
        mins_secs = s[2:-2]
        
        # extract hour (the thing to alter if necessary)
        hour = s[:2]
        
        # handle edge cases (i.e. 12)
        if hour == "12":
            if time_indicator == "AM":
                military_time = "00" + mins_secs
            else:
                military_time = hour + mins_secs
        # all the other hours
        else:
            if time_indicator == "AM":
                military_time = hour + mins_secs
            else:
                # add leading zero if not already two digits
                adjusted_hour = "{:0>2}".format(str(int(hour) + 12))
                military_time = adjusted_hour + mins_secs
                
        return military_time