Kernel log时间转换

https://www.cnblogs.com/atlas2016/p/7235095.html
对上面网页的转换做了修改,增加每次读到行android time就刷新基础计算时间的功能,保证以上次最近的android time时间为基础。

import time
import datetime
import sys
import getopt
import os

a_time = None
s_second = None
s_microsecond = None
abs_time = 0.0
inputfile = None
outputfile = None

def usage():
    print('''Help Information:
             -h, --help:        Show help information
             -i, --inputfile:   input file  to parse
             -o, --outputfile:  output file parsed
                        ''')

def calc_delta(stream):
    global s_second
    global s_microsecond
    global a_time
    global outfile
    begin_index = None
    end_index = None
    delta_second = 0
    delta_mircosecond = 0
    delta_time = 0
    d_time = None
    new_line = None
    for line in stream:
        if line:
            if 'android time' in line:
                update_time_reference(line)
            try:
                begin_index =  line.index('[')
                end_index = line[begin_index+1:].index(']')+begin_index+1
                time_string = line[begin_index + 1 :end_index]
                [d_second, d_microsecond] = time_string.split('.')
                delta_second = int(d_second) - int(s_second)
                delta_microsecond = int(d_microsecond) - int(s_microsecond)
                delta_time = datetime.timedelta(seconds=delta_second, microseconds=delta_microsecond)
                d_time = a_time + delta_time
                new_line = d_time.strftime("%m-%d %H:%M:%S.%f") + ' ' + line
                outputfile.write(new_line)
            except:
                outputfile.write(line)

def update_time_reference(line):
    global s_second
    global s_microsecond
    global a_time
    begin_index = line.index('[')
    end_index = line[begin_index+1:].index(']')+begin_index+1
    date_string = line[line.find('android time')+13:].strip()
    abs_time = line[begin_index + 1 :end_index]
    [s_second, s_microsecond] = abs_time.split('.')
    a_time = datetime.datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S.%f")

def main(argv):
    global inputfile
    global outputfile
    inputpath = None
    outputpath = None
    try:
        opts, args = getopt.getopt(argv,"hi:o:",["help","inputfile=","outputfile="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        if opt in ("-i", "--inputfile"):
            inputpath = arg
        if opt in ("-o", "outputfile"):
            outputpath = arg
    if inputpath == None:
        usage()
        sys.exit()
    if outputpath == None:
        outputpath = os.getcwd() + "/out.txt"

    inputfile = open(inputpath, 'r')
    outputfile = open(outputpath, 'w')
    calc_delta(inputfile)
    inputfile.close()
    outputfile.close()

if __name__ == "__main__":
    main(sys.argv[1:])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CCPigSnail

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值