I want to compare two timestamps and see if there are in 3 seconds. The first format will be like 13:15:19.654
and the second will be like 1:15:20 PM
. How can I implement?
For example:
13:15:19.654
and1:15:20 PM
return True
13:15:19.654
and1:15:25 PM
return False
Note, both timestamps are string from different file. Here is my code:
deviation = 3
timestamp_a = '13:15:19.654'
timestamp_b = '1:15:25 PM'
hour_a = timestamp_a.split(':')[0]
hour_b = timestamp_b.split(':')[0]
if 'PM' in timestamp_b:
hour_b = str(int(hour_b) + 12)
minute_a = timestamp_a.split(':')[1]
minute_b = timestamp_b.split(':')[1]
second_a = timestamp_a.split(':')[2].split('.')[0]
second_b = timestamp_b.split(':')[2].split()[0]
time_a = int(hour_a) * 3600 + int(minute_a) * 60 + int(second_a)
time_b = int(hour_b) * 3600 + int(minute_b) * 60 + int(second_b)
return abs(time_a - time_b) < deviation