如何在Python中将时间缩短5分钟?
我有这个脚本,但我认为这可以更容易,并且计算出错的整个小时.当它的22:03它返回21:95而不是21:55.
import datetime
from datetime import date
import time
utc_datetime = datetime.datetime.utcnow()
jaar = date.today().year
maand = time.strftime("%m")
dag = time.strftime("%d")
uurutc = utc_datetime.strftime("%H")
autc = utc_datetime.strftime("%M")
minuututc = int(5 * round(float(autc)/5)-5)
uur = time.strftime("%H")
a = time.strftime("%M")
minuut = int(5 * round(float(a)/5)-5)
timestamp = ''.join(str(x) for x in [jaar, maand, dag, uurutc, minuututc])
print timestamp
Run Code Online (Sandbox Code Playgroud)
代码实际上应该做什么:
我们当地时间是UTC + 2,但我只需要UTC时间,因此输出必须是UTC.其次,时间需要在当前时间之前5分钟然后向下舍入.字符串的输出格式应为:YYYYMMDDHHMM.
例:
当地时间:12:53>输出脚本:10:45
当地时间:17:07>输出脚本:15:00
当地时间:08:24>输出脚本:06:15
谁可以帮我解决这个问题?
谢谢!
from datetime import datetime, timedelta
now = datetime.utcnow()
rounded = now - timedelta(minutes=now.minute % 5 + 5,
seconds=now.second,
microseconds=now.microsecond)
print rounded
# -> 2014-04-12 00:05:00
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4528 次 |
| 最近记录: |