1
私は最も近い15分に丸めたいtimeleltaを持っています。例えば、8h 40mは8h 45mに、8h 50mは8h 45mに丸めます。タイムディスタを最も近い15分に丸める
これを達成するにはどうすればよいでしょうか?
私は最も近い15分に丸めたいtimeleltaを持っています。例えば、8h 40mは8h 45mに、8h 50mは8h 45mに丸めます。タイムディスタを最も近い15分に丸める
これを達成するにはどうすればよいでしょうか?
私はiPythonで使用
def round_timedelta(td, period):
"""
Rounds the given timedelta by the given timedelta period
:param td: `timedelta` to round
:param period: `timedelta` period to round by.
"""
period_seconds = period.total_seconds()
half_period_seconds = period_seconds/2
remainder = td.total_seconds() % period_seconds
if remainder >= half_period_seconds:
return timedelta(seconds=td.total_seconds() + (period_seconds - remainder))
else:
return timedelta(seconds=td.total_seconds() - remainder)
別の方法で見つけることができませんでしたように私独自のソリューションを作成することになった:
In [4]: print round_timedelta(timedelta(hours=1, minutes=27), timedelta(minutes=15))
1:30:00
In [5]: print round_timedelta(timedelta(hours=1, minutes=30), timedelta(minutes=15))
1:30:00
In [6]: print round_timedelta(timedelta(hours=1, minutes=31), timedelta(minutes=15))
1:30:00
In [7]: print round_timedelta(timedelta(hours=1, minutes=37), timedelta(minutes=15))
1:30:00
In [8]: print round_timedelta(timedelta(hours=1, minutes=38), timedelta(minutes=15))
1:45:00
をあなたが負のデルタをチェックしましたか? –