【问题标题】:change timestamp in bind logfile更改绑定日志文件中的时间戳
【发布时间】:2010-09-02 21:32:23
【问题描述】:

我需要更改绑定日志文件中的时间戳,因为现在我更新了系统时间,其中一半不正确...

文件中的每一行都遵循以下格式:

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

所有时间戳都在 8 小时前结束。这是我到目前为止所拥有的:

#!/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

任何想法都将不胜感激!

【问题讨论】:

    标签: python timestamp bind


    【解决方案1】:
    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:]    
    

    输出:

    >>> 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 + (...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      • 2018-05-10
      • 1970-01-01
      相关资源
      最近更新 更多