環境
Python version:Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
System version:Windows 10
程式
def secToHMS(secIn):
h = int(secIn / 3600)
m = int(( secIn - h*3600 ) / 60)
s = float(secIn) - h*3600 - m*60
return '花費%d時,%d分,%.1f秒' % (h,m,s)
def secToHMS_v2(secIn):
m, s = divmod(secIn, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
return 'Spend - days:hours:minutes:seconds = '+'%02d:%02d:%02d:%02d' % (d, h, m, s)
使用方式
import time
start_time = time.time()
print('start_time:',time.strftime("%H:%M:%S"))
time.sleep(3)
end_time = time.time()
print('end_time:',time.strftime("%H:%M:%S"))
print(secToHMS(end_time-start_time))
print(secToHMS_v2(end_time-start_time))
執行結果
start_time: 16:39:20
end_time: 16:39:23
花費0時,0分,3.0秒
Spend - days:hours:minutes:seconds = 00:00:00:03
沒有留言:
張貼留言