我想让python中的最后10分钟完全可以被10整除。
它不应该舍入到下一个 10 分钟,它必须始终是前 10 分钟。
例如,输出应如下所示:
2019-10-04 20:45:34.903000 -> 2019-10-04 20:40
2019-10-04 20:48:35.403000 -> 2019-10-04 20:40
2019-10-04 20:42:21.903000 -> 2019-10-04 20:40
2019-10-04 20:50:21.204000 -> 2019-10-04 20:50
2019-10-04 20:59:49.602100 -> 2019-10-04 20:50
import datetime
def timeround10(dt):
#...
print timeround10(datetime.datetime.now())
最简单的方法是使用所需的值构造一个新datetime
。
def timeround10(dt):
return datetime.datetime(dt.year, dt.month, dt.day, dt.hour, (dt.minute // 10) * 10))
def timeround10(dt):
return dt.replace(second=dt.second // 10, microsecond=0)