python时长转换模块

duration的Python项目详细描述


python时长转换模块

安装

pip install duration

用法

下面的示例演示如何转换hh:mm:ssmm:ss格式的时间戳 到ISO8601字符串、整数秒、DateTime.TimeDelta对象和 (小时、分钟、秒)增量

fromdurationimport(to_iso8601,to_seconds,to_timedelta,to_tuple,)time='1:23:45'iso8601=to_iso8601(time)# 'PT01H23M45S'seconds=to_seconds(time)# 5025td=to_timedelta(time)# timedelta(hours=1, minutes=23, seconds=45)tuple_=to_tuple(time)# (1, 23, 45,)

上面的例子默认使用严格模式在严格模式下,转换 如果持续时间字符串满足 以下条件:

  1. hh>;23
  2. 59毫米
  3. 第59页

要禁用严格模式,请将strict=False传递给转换函数

fromdurationimport(to_iso8601,to_seconds,to_timedelta,to_tuple,)time='24:83:25'iso8601=to_iso8601(time,strict=False)# 'P1DT01H23M25S'seconds=to_seconds(time,strict=False)# 91405td=to_timedelta(time,strict=False)# timedelta(seconds=91405)tuple_=to_tuple(time,strict=False)# (25, 23, 25,)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将日期字符串解析为整数变量?   java如何在Spring3+WS2+中使用注释、JibxMarshalling或使用xml配置配置JibxMarshalling和unmarshalling?   安装Oracle Jdeveloper时mac中出现java安装错误。   设置自动保存计时器以在java中定期保存文件(但不使用任何JavaFXAPI)的最佳策略是什么?   模板获取handlebar java中迭代器列表的长度   java如何在特定JAX RS调用中排除对象中的字段?   在Struts 2中抛出NullPointerException的java getText()方法   java比较捐献者和接受者的血型   fedora Java向后兼容性   字符串Java替换多个字符   复制BuffereImage时出现java数组索引错误   并行处理Java 8并行流findFirst   java关闭应用程序后如何离开服务?   Java:Tesseractocr:如何查找单词坐标?