Python中的舍入时间 [英] Rounding time in Python
本文介绍了Python中的舍入时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中对舍入分辨率进行控制的,与时间相关的类型执行h/m/s舍入运算的一种优雅,有效且Pythonic的方式是什么?
What would be an elegant, efficient and Pythonic way to perform a h/m/s rounding operation on time related types in Python with control over the rounding resolution?
我的猜测是,这将需要时间模运算.说明性示例:
My guess is that it would require a time modulo operation. Illustrative examples:
- 20:11:13%(10秒)=>(3秒)
- 20:11:13%(10分钟)=>(1分钟13秒)
我能想到的与时间相关的类型:
Relevant time related types I can think of:
-
datetime.datetime
\datetime.time
-
struct_time
datetime.datetime
\datetime.time
struct_time
推荐答案
有关datetime.datetime的取整,请参见以下函数: https://stackoverflow.com/a/10854034/1431079
For a datetime.datetime rounding, see this function: https://stackoverflow.com/a/10854034/1431079
使用示例:
print roundTime(datetime.datetime(2012,12,31,23,44,59,1234),roundTo=60*60)
2013-01-01 00:00:00
这篇关于Python中的舍入时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文