-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadable duration format.py
More file actions
61 lines (56 loc) · 1.51 KB
/
Copy pathreadable duration format.py
File metadata and controls
61 lines (56 loc) · 1.51 KB
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
def format_duration(seconds):
answer = ""
year = 31_536_000
day = 86_400
hour = 3_600
minute = 60
second = 1
time = {"second": 0,
"minute": 0,
"hour": 0,
"day": 0,
"year": 0}
while seconds > 0:
if seconds >= year:
seconds-=year
time["year"] += 1
elif seconds >= day:
seconds-=day
time["day"] += 1
elif seconds >= hour:
seconds -= hour
time["hour"] += 1
elif seconds >= minute:
time["minute"] += 1
seconds -= minute
elif seconds >= second:
time["second"] = seconds
seconds = 0
i = 0
for d, val in time.items():
if val == 1:
answer = f"{val} {d}" + answer
elif val > 1:
answer = f"{val} {d}s" + answer
if i == 0 and answer != "":
answer = " and " + answer
i += 1
elif i > 0 and val != 0:
answer = ", " + answer
i += 1
if answer.startswith(" and "):
answer = answer[5:]
if answer.startswith(','):
answer = answer[1:]
if answer.startswith(" "):
answer = answer[1:]
if answer.endswith(" and "):
answer = answer[:-5]
if answer.endswith(','):
answer = answer[:-1]
if answer.endswith(" "):
answer = answer[:-1]
if answer == "":
answer = "now"
return answer
print(format_duration(2790120))