I want to compare timestamps from an apache log file to see whether one request has been made less than five minutes after the previous one. Timestamp looks like this [05/Feb/2007:00:31:59 +0100]. How do I make the comparison?
comparing apache timestamps
Collapse
X
-
Does anyone have a better way?[code=Python]import time, datetimeOriginally posted by lihunI want to compare timestamps from an apache log file to see whether one request has been made less than five minutes after the previous one. Timestamp looks like this [05/Feb/2007:00:31:59 +0100]. How do I make the comparison?
def date_compare(ts 1, ts2, **compkw):
t1 = time.strptime(t s1.strip('[]').split()[0], '%d/%b/%Y:%H:%M:%S')
d1 = datetime.dateti me(*t1[:6])
t2 = time.strptime(t s2.strip('[]').split()[0], '%d/%b/%Y:%H:%M:%S')
d2 = datetime.dateti me(*t2[:6])
delta = d2-d1
orderList = ['days', 'hours', 'minutes', 'seconds']
outList = []
for item in orderList:
if compkw.has_key( item):
outList.append( '%s %s' % (compkw[item], item))
outStr = ', '.join(outList)
if delta <= datetime.timede lta(**compkw):
return 'Less than or equal to %s' % (outStr)
return 'Greater than %s' % (outStr)[/code]
Example:
[code=Python]
>>> ts1 = '[05/Feb/2007:00:31:59 +0100]'
>>> ts2 = '[09/Feb/2007:00:37:00 +0100]'
>>> print date_compare(ts 1, ts2, minutes=5, days=2)
Greater than 2 days, 5 minutes
>>> ts1 = '[05/Feb/2007:00:31:59 +0100]'
>>> ts2 = '[05/Feb/2007:00:36:59 +0100]'
>>> print date_compare(ts 1, ts2, minutes=5)
Less than or equal to 5 minutes
>>> [/code]
Comment