0

I am trying to take a string and convert into 24 hour time. For example, if I am given the string "07:05:45PM", it should return "19:05:45". I have completed all the necessary conversions, I am just not sure how I am supposed to put them all together and have it so that if there were only 5 minutes or 5 seconds, it would place a zero like "xx:05:06".

def timeConversion(s):
    nums = s[:8]
    hh,mm,ss = [v for v in nums.split(":")]
    time = s[8:]
    if time == 'AM':
        return nums
    else:
        total = (int(hh) * 3600 + int(mm) * 60 + int(ss)) + 43200
        if s == "12:00:00PM":
            return nums
        hh = total // 3600
        mm = total // 60 % 60
        ss = total % 60

print(timeConversion("07:05:45PM"))
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Krish
  • 415
  • 2
  • 11
  • 1
    Did you have a look at the [datetime](https://docs.python.org/3/library/datetime.html) module? It can help you with time calculations, formats and conversions. Have a look here for example: https://stackoverflow.com/questions/1759455/how-can-i-account-for-period-am-pm-with-datetime-strptime – Valentino May 10 '19 at 00:48
  • 1
    `[v for v in nums.split(":")]` is just `nums.split(':')` – Mad Physicist May 10 '19 at 01:21

3 Answers3

2

Only the hours and suffix have any significance in your output. The key is that 12AM is 00, and 12PM is 12. 12:00 is not the only time that deserves this treatment: 12:01 does too. Use the modulo operator to avoid any special cases at all:

def time_conversion(s):
    hh = int(s[:2]) % 12  # this is the magical part 12->0
    mmss = s[2:8]
    suffix = s[8:].strip().upper()

    if suffix == 'PM':
        hh += 12
    return f'{hh:02d}{mmss}'
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
0

Here is the working code. For the input "07:05:45PM", it returns "19:05:45". You just need to add a '0' if the minutes or seconds values are less than 10.

def timeConversion(s):
    nums = s[:8]
    hh,mm,ss = [v for v in nums.split(":")]
    time = s[8:]
    if time == 'AM':
        return nums
    else:
        total = (int(hh) * 3600+int(mm)*60+int(ss))+43200
        if s == "12:00:00PM":
            return nums
        hh = str(total//3600)
        mm = total//60 % 60
        ss = total % 60

        mm_str = '0'+ str(mm) if mm <= 9 else str(mm) 
        ss_str = '0'+ str(ss) if ss <= 9 else str(ss)
        time_str = str(hh) + ':' + mm_str + ":" + ss_str

        return time_str 

print(timeConversion("07:05:45PM"))
0

Since you are dealing with a string, perhaps most of the code could deal with the string.

EDIT: Revised code as per Mad Physicist's comments

def convert(s):
    if s[8:] == 'AM':
        if s[:2] == '12':
            return '00' + s[2:8]
        else:
            return s[:8]
    elif s[8:] == 'PM':
        if s[:2] == '12':
            return s[:8]
        else:
            return str(int(s[:2])+ 12) + s[2:8]

for t in ['12:00:00AM', '01:01:01AM', '12:01:01PM', '01:10:11PM', '11:20:20PM']:
    print(t, convert(t))

Prints:

12:00:00AM 00:00:00
01:01:01AM 01:01:01
12:01:01PM 12:01:01
01:10:11PM 13:10:11
11:20:20PM 23:20:20
Chris Charley
  • 6,403
  • 2
  • 24
  • 26