Time floor function (Python)

Hello. I made a floor function for time (`dt_floor ()`). In the example below, 15 minutes, which is 1 hour divided by an integer, is used as the unit of account.

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)

Recommended Posts

Time floor function (Python)
Function execution time (Python)
python function ①
[Python] function
python function ②
Measure function execution time in Python
First time python
python enumerate function
Python> function> Closure
[Python] Generator function
python time measurement
First time python
Python> function> Inner function
Python function decorator
Python: Time Series Analysis
About function arguments (python)
Python function argument summary
Python time series question
Python print function (sequel)
Output python execution time
Python: About function arguments
python get current time
Python execution time measurement memo
Create a function in Python
Use callback function in Python
[python] Value of function object (?)
ntile (decile) function in python
[Python] Etymology of python function names
About python beginner's memorandum function
About the enumerate function (python)
python url setting include function
Python #function 2 for super beginners
Execution time measurement with Python With
Nonlinear function modeling in Python
Draw implicit function in python
Python higher-order function (decorator) sample
Immediate function in python (lie)
[Python] Plot time series data
Time synchronization (Windows) with Python
[Python] What is a zip function?
Call a Python function from p5.js.
[python] Callback function (pass function as argument)
Implement R's power.prop.test function in python
Function argument type definition in python
Python
Included notation in Python function arguments
Python: Time Series Analysis: Preprocessing Time Series Data
Write AWS Lambda function in Python
[Python] Execution time when a function is entered in a dictionary value
python sql statement extracted by time
A function that measures the processing time of a method in python
[Python] Make the function a lambda function
[Python3] Define a decorator to measure the execution time of a function
Time series plot started ~ python edition ~
Python #len function for super beginners
Function synthesis and application in Python
See python for the first time
Basic Python operation 2nd: Function (argument)
How to use python zip function
[Python] Difference between function and method
Code tests around time in Python