python秒转化为时间格式_Python耗费时间秒转 天小时分钟秒 时间格式美化
在工作中經常會遇到將耗時 轉換為天小時分秒的情況
本Demo 中divmod默認返回元組,同時利用遞歸的思想
# -*- coding: utf-8 -*-
def seconds_format(time_cost: int):
"""
耗費時間格式轉換
:param time_cost:
:return:
"""
min = 60
hour = 60 * 60
day = 60 * 60 * 24
if not time_cost or time_cost < 0:
return ''
elif time_cost < min:
return '%s秒' % time_cost
elif time_cost < hour:
return '%s分%s秒' % (divmod(time_cost, min))
elif time_cost < day:
cost_hour, cost_min = divmod(time_cost, hour)
return '%s小時%s' % (cost_hour, seconds_format(cost_min))
else:
cost_day, cost_hour = divmod(time_cost, day)
return '%s天%s' % (cost_day, seconds_format(cost_hour))
if __name__ == '__main__':
seconds_format(1)
seconds_format(61)
總結
以上是生活随笔為你收集整理的python秒转化为时间格式_Python耗费时间秒转 天小时分钟秒 时间格式美化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mysql中的FOREIGN_KEY_C
- 下一篇: micropython按键控制流水灯_【