0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

時刻の floor 関数(Python)

Last updated at Posted at 2020-02-06

こんにちは。
時刻に対する floor 関数を作ってみました(dt_floor())。計算単位として、下記例では 1 時間を整数で除算した値の 15 分を使っています。

interval = 900  # in seconds
dt = datetime.datetime.strptime("2020-01-01 00:05:05+09:00", '%Y-%m-%d %H:%M:%S%z')
print(dt_floor(dt, interval))  # => "2020-01-01 00:00:00+09:00"
import datetime

MINUTE = 60  # in seconds
HOUR = 3600  # in seconds
ndiv = 4  # in [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]
interval = HOUR//ndiv  # in seconds

def dt_floor(dt, interval):
    interval_m = interval//MINUTE  # in minutes
    minute_floor = (dt.minute//interval_m)*interval_m
    return dt.replace(minute=minute_floor, second=0)
0
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?