summaryrefslogtreecommitdiff
path: root/perf_tools/parse_timestamp.py
blob: bfac3f716f24c886530d39d312cbe7f9d947d55b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
import os
from datetime import datetime

# Usage:
# replace_timestamp.py input.txt output.txt timestamp_string
#
# Description:
# Replace timestamp in the input.txt with the difference timestamp to timestamp_string.
#
# Example: replace_timestamp.py input.txt output.txt "01-28 18:12:30.339".
#
def main():
    filepath = sys.argv[1]
    if not os.path.isfile(filepath):
        print("File path {} does not exist. Exiting...".format(filepath))
        sys.exit()

    output_filepath = sys.argv[2]

    timestamp_str = sys.argv[3]
    date_time_obj = datetime.strptime(timestamp_str, '%m-%d %H:%M:%S.%f')

    output_fp = open(output_filepath, 'w')
    i = 1
    with open(filepath, 'r', errors = 'ignore') as fp:
        for line in fp:
            newline = replace_timestamp_abs(line, timestamp_str, date_time_obj)
            output_fp.write(newline)
            i = i + 1
    fp.close()
    output_fp.close()


def replace_timestamp_abs(line, timestamp_str, date_time_obj0):
    if line[:5] != timestamp_str[:5]:
        return line

    index = line.find(" ", 6)
    if index <= 0:
        return line
    substr0 = line[:index]
    substr1 = line[index:]

    try:
        date_time_obj = datetime.strptime(substr0, '%m-%d %H:%M:%S.%f')
    except ValueError:
        return line

    date_time_delta = date_time_obj - date_time_obj0
    date_time_delta_str = str(date_time_delta)
    return date_time_delta_str + substr1

if __name__ == '__main__':
    main()