diff options
author | Xiaoqin Ma <xiaoqinma@google.com> | 2022-10-31 22:46:11 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2022-10-31 22:46:11 +0000 |
commit | 4a505107767a850ae15944ec8617ff27cc12f5cd (patch) | |
tree | 0b68fa2afa6b872543127cac5df5095d9c7e603f /perf_tools | |
parent | b50512eb7135bc582b8a7c4fbd65734e083f8e5e (diff) | |
parent | e464a2f1cddd6c937dfe6ecd004b6133ac7ef5c5 (diff) | |
download | extras-4a505107767a850ae15944ec8617ff27cc12f5cd.tar.gz |
Merge "Calculate and replace timestamp with relative timestamp to given timestamp string."
Diffstat (limited to 'perf_tools')
-rw-r--r-- | perf_tools/parse_timestamp.py | 55 |
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() |