python 将秒数换算为小时分钟

记录程序的执行时间,并进行单位换算

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
import datetime


def convert_seconds(seconds):
"""
将给定的秒数转换为小时、分钟和秒。
"""
hour = seconds // 3600
minute = (seconds % 3600) // 60
second = seconds % 60
if hour == 0:
if minute == 0:
run_time = f"{second} 秒"
else:
run_time = f"{minute}{second} 秒"
else:
run_time = f"{hour} 小时 {minute}{second} 秒"

return run_time

def main():
start = datetime.datetime.now()
# do something
end = datetime.datetime.now()

run_time = convert_seconds((end-start).seconds)