-
Notifications
You must be signed in to change notification settings - Fork 7
/
time_diffs.py
executable file
·47 lines (36 loc) · 1.25 KB
/
time_diffs.py
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
#!/usr/bin/python3
# Copyright (c) 2023 Kim Hendrikse
import sys
import argparse
from datetime import datetime
def main():
# Initialize argparse
parser = argparse.ArgumentParser(description="Calculate time differences from the earliest time.")
parser.add_argument("-p", "--python_array", action="store_true",
help="Output the time differences as a Python array of floats.")
args = parser.parse_args()
# Initialize variables
input_times = []
time_differences = []
while True:
try:
line = input().strip()
if not line:
break
input_times.append(datetime.strptime(line, "%Y-%m-%d_%H-%M-%S.%f"))
except ValueError as e: # Catch only value errors (i.e., formatting issues)
print(f"Error reading input: {e}")
# Find the earliest time
earliest_time = min(input_times)
# Calculate the time differences
for t in input_times:
delta = t - earliest_time
time_diff = delta.total_seconds()
time_differences.append(time_diff)
# Output the results
if args.python_array:
print(time_differences)
else:
for time_diff in time_differences:
print(f"{time_diff:.6f}")
main()