Parsing Timestamp in Python

2020-07-24 07:10发布

问题:

I have an application in which I need to parse timestamps of four different formats.

HH:MM:SS
HH:MM:SS.mm
MM:SS
MM:SS.mm

How can I write a function to parse any of these formats into a timedelta object?

I have tried iterating through the characters one by one and breaking when I see the :, but my code is a mess, so I would rather not have it here as a baseline.

回答1:

Here's a way to do it using datetime.datetime.strptime():

If you don't know ahead of time which format your input will be in, you can try all of them wrapped in a try/catch block.

strptime() returns a datetime object, so call the .time() function to get only the time part. See this post for more details.

import datetime

def parse_timestamp(ts):
    formats = [
        "%H:%M:%S", #HH:MM:SS
        "%H:%M:%S.%f", #HH:MM:SS.mm
        "%M:%S", #MM:SS
        "%M:%S.%f" #MM:SS.mm
    ]
    for f in formats:
        try:
            return datetime.datetime.strptime(ts, f).time()
        except ValueError:
            pass
    return None

Examples:

timestamps = [
    "12:34:56",
    "12:34:56.78",
    "34:56",
    "34:56.78"
]
for ts in timestamps:
    print parse_timestamp(ts)

Output:

12:34:56
12:34:56.780000
00:34:56
00:34:56.780000

Or if you know the specific format, you can use datetime.datetime.strptime(ts, f).time() directly.

Update 1

If you want to convert to timedeltas, you can do so using the output of parse_timestamp() and the timedelta constructor:

def time_to_timedelta(t):
    td = datetime.timedelta(
        seconds=t.second,
        microseconds=t.microsecond,
        minutes=t.minute,
        hours=t.hour
    )
    return td

Here is a related post that you may also find useful.