【Python】时间转换(可用于计算耗时场景)
2022/4/1 1:19:33
本文主要是介绍【Python】时间转换(可用于计算耗时场景),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @Time :2022/3/31 23:55 @Author : @File :TimeCalc.py @Version :1.0 @Function: """ class TimeCalc: @staticmethod def calc(timeNumber: int) -> str: """ 将指定数字转换为时间(格式: 00:00:00,000) :param timeNumber: :return: 字符串 如: 60:00:59,999 """ hUnit = 60 * 60 * 1000 mUnit = 60 * 1000 sUnit = 1000 h = int(timeNumber / hUnit) m = int((timeNumber % hUnit) / mUnit) s = int(((timeNumber % hUnit) % mUnit) / sUnit) ms = ((timeNumber % hUnit) % mUnit) % sUnit calcResult = f'{str(h).zfill(2)}:{str(m).zfill(2)}:{str(s).zfill(2)},{str(ms).zfill(3)}' return calcResult if __name__ == '__main__': print(TimeCalc.calc(3599999)) # 00:59:59,999 print(TimeCalc.calc(216059999)) # 60:00:59,999 print(TimeCalc.calc(0)) # 00:00:00,000 print(TimeCalc.calc(360012000)) # 100:00:12,000
这篇关于【Python】时间转换(可用于计算耗时场景)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python