在Python中解析时间戳

时间:2018-01-23 18:59:19

标签: python datetime timestamp

我有一个应用程序,我需要解析四种不同格式的时间戳。

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

如何编写函数将任何这些格式解析为timedelta对象?

当我看到break时,我尝试逐个遍历字符和:,但我的代码很乱,所以我宁愿不把它作为基线。< / p>

1 个答案:

答案 0 :(得分:2)

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.