kernel log time 转换为对应的年月日时分秒

import time
import datetime
import sys
import getopt
import os
#需要加入 [  691.165602] android time 2017-06-19 17:47:59.765338
#06-19 17:47:59.765338 [  691.165602] android time 2017-06-19 17:47:59.765338
a_time = None
s_second = None
s_microsecone = 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 fiel 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
    if a_time ==None:
        print("Can‘t convert to android time")
        exit(-1)
    for line in stream:
        if 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(int(d_second) - int(s_second))
                delta_microsecond = int(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 get_atime(stream):
    global s_second
    global s_microsecond
    global a_time
    a_time_op = None
    begin_index = None
    end_index = None
    for line in stream:
        if line:
            a_time_op = line.find(‘android time‘)
            if a_time_op>=1:
                begin_index =  line.index(‘[‘)
                end_index = line[begin_index+1:].index(‘]‘)+begin_index+1
                date_string = line[a_time_op+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")
                break

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‘)
    get_atime(inputfile)
    inputfile.seek(0)
    calc_delta(inputfile)
    inputfile.close()
    outputfile.close()
if __name__ == "__main__":
    main(sys.argv[1:])
时间: 2024-07-31 01:20:50

kernel log time 转换为对应的年月日时分秒的相关文章

关于时间的操作(Java版)——将毫秒转换为年月日时分秒

第一种方式: import java.util.Calendar; import java.util.TimeZone; public class Test { /** * 将毫秒转换为年月日时分秒 * * @author GaoHuanjie */ public String getYearMonthDayHourMinuteSecond(long timeMillis) { Calendar calendar = Calendar.getInstance(TimeZone.getTimeZo

C# 版本的 计时器类:精确到微秒 秒后保留一位小数 支持年月日时分秒带单位的输出

class TimeCount { // 临时变量,存放当前类能表示的最大年份值 private static ulong MaxYear = 0; /// <summary> /// 获取毫秒能表示的最大年份数 /// </summary> /// <returns>年份数最大值</returns> public static ulong GetMaxYearCount() { if (TimeCount.MaxYear != 0) return Time

sql server获取当前年月日 时分秒

获取当前年月日(字符串): select CONVERT(varchar(11),GETDATE(),112) 获取当前时间的时分秒(':'隔开): select CONVERT(varchar(12),GETDATE(),108) 将年月日时分秒拼接成一条字符串: select CONVERT(varchar(11),GETDATE(),112)+REPLACE(CONVERT(varchar(12),GETDATE(),108),':','')

计算一个人的年龄(年月日时分秒),有不对的地方希望大家指出!

想想我们可以做一个计时器,记录一下我们走过了多少时光.看了一下网上别人的一些代码,记录年月的都并不科学,甚至很麻烦,自己倒腾了一上午,总算弄出来了一个. 自己觉得还比较科学,暂时没有发现BUG,如果哪里有错,希望大家指出来! 上代码: <!doctype html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&

php实现显示网站运行时间-秒转换年月日时分秒

<?php // 设置时区 date_default_timezone_set('Asia/Shanghai'); /** * 秒转时间,格式 年 月 日 时 分 秒 * * @author [email protected] * @param int $time * @return array|boolean */ function Sec2Time($time){ if(is_numeric($time)){ $value = array( "years" => 0,

生成ID模板:年月日时分秒+6位自增码

由于生成订单ID.商品ID 或者什么什么ID的,不想用自增,又怕重复,于是就用  年与日时分秒 + 6位自增码 (共计20位长度)来当作ID 注意:如果你的ID是Long型,就要注意,Long的最大长度为19位,如果直接转的话会有问题,建议改为年月日时分秒+5位随机数 具体代码: private static int sequence = 0; private static int length = 6; /** * YYYYMMDDHHMMSS+6位自增长码(20位) * @author sh

获取当前时间---年月日时分秒------iOS

方式一:XXXX年-XX月-XX日  XX时:XX分:XX秒的格式 - (IBAction)LoginAction:(UIButton *)sender { NSDate *date = [NSDate date];        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];            [formatter setDateStyle:NSDateFormatterMediumStyle];        [f

jquery 的日期时间控件(年月日时分秒)

<!-- import package --> <script type="text/javascript" src="JS/jquery.js"></script> <script type="text/javascript" src="JS/jquery-ui-1.7.3/ui/jquery-ui-1.7.3.custom.js"></script> <sc

年月日时分秒加星期的即时显示的JS

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ