-
Notifications
You must be signed in to change notification settings - Fork 11
/
time_operations.py
executable file
·69 lines (60 loc) · 1.6 KB
/
time_operations.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def time_difference(a,b): # b - a
hr = int(b[0:2])-int(a[0:2])
mn = int(b[3:5])-int(a[3:5])
sec = int(b[6:8])-int(a[6:8])
if mn < 0 and sec < 0:
hr = hr - 1
mn = 60 + mn - 1
sec = 60 + sec
if hr < 0:
hr = hr + 24
elif mn < 0 and sec >= 0:
hr = hr - 1
mn = 60 + mn
if hr < 0:
hr = hr + 24
elif sec < 0 and mn > 0:
sec = 60 + sec
mn = mn - 1
if hr < 0:
hr = hr + 24
elif sec < 0 and mn == 0:
hr = hr - 1
mn = 59
sec = 60 + sec
hr = str(hr).zfill(2)
mn = str(mn).zfill(2)
sec = str(sec).zfill(2)
result = hr + ":" + mn + ":" + sec
return result
def time_addition(a,b):
hr = int(b[0:2]) + int(a[0:2])
mn = int(b[3:5]) + int(a[3:5])
sec = int(b[6:8]) + int(a[6:8])
if mn >= 60 and sec >= 60:
hr = hr + 1
mn = mn - 60 + 1
sec = sec - 60
elif mn >= 60:
hr = hr + 1
mn = mn - 60
elif sec >= 60:
mn = mn + 1
sec = sec - 60
hr = str(hr).zfill(2)
mn = str(mn).zfill(2)
sec = str(sec).zfill(2)
result = hr + ":" + mn + ":" + sec
return result
def format_time(t):
result = t[0:2] + 'h ' + t[3:5] + 'm ' + t[6::] + 's'
#if int(t[0:2]) == 0:
# result = t[3:5] + 'm ' + t[6::] + 's'
# if int(t[3:5]) == 0:
# result = t[6::] + 's'
#else:
# result = t[0:2] + 'h ' + t[3:5] + 'm ' + t[6::] + 's'
return result
def convert_into_sec(t):
sec = int(t[0:2])*3600 + int(t[3:5])*60 + int(t[6::])
return sec