简体   繁体   中英

parse time to proper format python

I'm having issue parsing time from a website.

Time is given in this format (9-10:40AM, 11-1:30PM, 6:30-7:20PM)

If the time is not available it will show as TBA

I want to parse that to start and end time in 12H format.

This method doesn't return the correct values For example, if the string is "11:25-12:15PM" I expect to get [11:25AM, 12:15PM] But what I actually get is [11:25PM, 12:15PM]

def insertTime(initialTime):
if "TBA" in initialTime:
    return ["TBA", "TBA"]
startTime,endTime = initialTime.split("-")
try:
    if "PM" in endTime:
        startTimeHours = startTime.split(":")[0]
        if ":" in startTime:
            startTimeMinutes = ":" + startTime.split(":")[1]
        else:
            startTimeMinutes = ":00"
        if int(startTimeHours) in range(9,12):
            startTimeHours += startTimeMinutes + "AM"

    if ":" not in startTime:
        startTime +=":00"
    if "AM" not in startTime:
        startTime += endTime[-2:]

    return [startTime, endTime]
except Exception as e:
    print(f"Error insertTime: Start-> {startTime}, endTime->{endTime}")
    print(e)
    return [0,0]

Thank you

I think it would be easiest if you explicitly handled two possible cases (AM and PM) for the start time:

import datetime

def parse_time(text):
    pm = None  # unknown

    if text[-2:] in ('AM', 'PM'):
        pm = (text[-2:] == 'PM')
        text = text[:-2]

    if ':' not in text:
        text += ':00'

    hours, minutes = text.split(':')
    hours = int(hours)
    minutes = int(minutes)

    if pm and hours < 12:
        hours += 12

    return datetime.time(hours, minutes), pm is not None


def parse_interval(text):
    if 'TBA' in text:
        return None, None

    start, end = text.split('-')

    start, is_exact = parse_time(start)
    end, _ = parse_time(end)  # end time should be exact

    # If both the start and end times are known then there's nothing left to do
    if is_exact:
        return start, end

    # start2 = start + 12 hours
    start2 = start.replace(hour=(start.hour + 12) % 24)
    start_AM, start_PM = sorted([start, start2])

    # Pick the most reasonable option
    if start_AM < end < start_PM:
        return start_AM, end
    elif start_AM < start_PM < end:
        return start_PM, end
    else:
        raise ValueError('This cannot happen')

if __name__ == '__main__':
    for text in [
        '9-10:40AM',
        '11-1:30PM',
        '6:30-7:20PM',
        '11:25-12:15PM',
        '2AM-12:15PM',
    ]:
        print(text.rjust(15), ':', *parse_interval(text))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM