-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_back_or_front.py
50 lines (37 loc) · 1.14 KB
/
push_back_or_front.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
48
49
50
import sys,re
from datetime import timedelta
def splitandconvert(d,outfile):
hr = int(d.split(':')[0])
mint = int(d.split(':')[1])
sec = int(d.split(':')[2].split(',')[0])
mil = int(d.split(',')[1])
# conversion
d = timedelta(hours=hr,minutes=mint,seconds=sec,milliseconds=mil)
# adjustment
d = d+timedelta(seconds=SHIFT_SEC) if FORWARD else d-timedelta(seconds=SHIFT_SEC)
# conversion
d = str(d)
# left padding zero for two digit hours (it'll never exceed 9 hours anyway)
d = '0'+d
# microsecond -> milliseconds (just use what you have already)
d = re.sub("\..*",","+str(mil).zfill(3),d)
return d
oldfile = sys.argv[1]
newfile = sys.argv[2]
FORWARD = sys.argv[3] if sys.argv[3] == "FORWARD" else False
SHIFT_SEC = int(sys.argv[4]) if sys.argv[4] else 5
with open(newfile, 'w') as outfile, open(oldfile, 'r') as infile:
for line in infile:
listofts = re.findall("\d{2}:\d{2}:\d{2},\d{3}",line)
if listofts:
#1
d = splitandconvert(listofts[0],outfile)
str1 = d+" --> "
#2
d = splitandconvert(listofts[1],outfile)
str2 = d
outfile.write(str1+str2+"\n")
else:
outfile.write(line)
outfile.close()
infile.close()