1

I have an apache log which has timestamps like:

[03/Feb/2013:02:52:05 +0000]

As a string. for my purposes the trailing +0000 isn't needed. My end goal is to get the time-difference between two time stamps in minutes. Is there a more elegant, less verbose approach than just hacking up and comparing the elements in a piecemeal, ripple-adding fashion?

Ideally I'd like to do as little as possible to the string before using it to initialize a time object of some sort, which has a 'compare_to( )' type method.

I'm relatively new to python, so I'm sorry if there's an obvious library or approach for this, but I haven't been able to find one.

4

1 回答 1

1

如果您不需要时区偏移量,您可以使用 datetime 模块来解析数据:

from datetime import datetime

line = '[03/Feb/2013:02:52:05 +0000]'
date = line[1:line.find(' ')]   #  take the string from the second character to the first blank
line_timestamp = datetime.strptime(date, "%d/%b/%Y:%H:%M:%S")

print repr(line_timestamp)
# datetime.datetime(2013, 2, 3, 2, 52, 5)

然后,您可以减去这些值。

于 2013-05-13T21:07:36.770 回答