summaryrefslogtreecommitdiff
path: root/perf_tools
diff options
context:
space:
mode:
authorXiaoqin Ma <xiaoqinma@google.com>2022-09-16 17:42:16 +0000
committerXiaoqin Ma <xiaoqinma@google.com>2022-09-16 17:42:16 +0000
commite464a2f1cddd6c937dfe6ecd004b6133ac7ef5c5 (patch)
tree918658f5e09b101d52125fb07889179b13c9dced /perf_tools
parent51cbaa40fab82382a0916bfbc60feabbcbf61a05 (diff)
downloadextras-e464a2f1cddd6c937dfe6ecd004b6133ac7ef5c5.tar.gz
Calculate and replace timestamp with relative timestamp to given timestamp string.
Test: manually. Change-Id: I11b59005595bf709125c4e4fc484140d83e0da71
Diffstat (limited to 'perf_tools')
-rw-r--r--perf_tools/parse_timestamp.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/perf_tools/parse_timestamp.py b/perf_tools/parse_timestamp.py
new file mode 100644
index 00000000..bfac3f71
--- /dev/null
+++ b/perf_tools/parse_timestamp.py
@@ -0,0 +1,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()