views:

21

answers:

1

i need to change the timestamps in a bind logfile because half of them are incorrect now that i have updated the system time...

every line in the file follows this format:

04-Aug-2010 07:32:31.416 client 10.0.0.1#00000: query: google.com IN A + (10.0.0.1)

all the time stamps are out by 8 hours. this is what i have so far:

#!/usr/bin/python
from time import strftime, strptime

f = open("query.log","r")
d = f.readlines()

i = 0
while not d[i].startswith("20-Aug"):
  print strftime('%d-%b-%Y %H:%M:%S', strptime(d[i].split(".")[0], '%d-%b-%Y %H:%M:%S'))
  i+=1

any ideas would be appreciated!!

A: 
from datetime import datetime, timedelta

tfmt = "%d-%b-%Y %H"
tfmtlen = 14

def changestamp(line, **kwargs):
    linetime = datetime.strptime(line[:tfmtlen],tfmt)
    linetime += timedelta(**kwargs)

    return linetime.strftime(tfmt) + line[tfmtlen:]    

Output:

>>> line = "04-Aug-2010 07:32:31.416 client 10.0.0.1#00000: query: google.c...
>>> changestamp(line, hours=8)
'04-Aug-2010 15:32:31.416 client 10.0.0.1#00000: query: google.com IN A + (...
>>> changestamp(line, hours=-8)
'03-Aug-2010 23:32:31.416 client 10.0.0.1#00000: query: google.com IN A + (...
>>> changestamp(line, weeks=52, days=-365+1/3, hours=24)
'04-Aug-2010 07:32:31.416 client 10.0.0.1#00000: query: google.com IN A + (...
Nick T
WOW, thanks! this solution is fantastic!!!