How to generate datetime for every second on a given day in Python
This example code generates a timezone-aware datetime for every second on a given day in a given timezone.
First, install the UliEngineering library and pytz for timezones:
install_uliengineering.sh
pip install --user UliEngineering pytzNow you can use UliEngineering.Utils.Date.yield_seconds_on_day():
yield_seconds_example.py
from UliEngineering.Utils.Date import *
for second in yield_seconds_on_day(year=2022, month=6, day=15, tz=pytz.timezone("Europe/Berlin"):
pass # TODO: Your code goes hereOr, if you want to have a list of datetime instances instead of a generator:
yield_seconds_list_example.py
from UliEngineering.Utils.Date import *
seconds = list(yield_seconds_on_day(year=2022, month=6, day=15, tz=pytz.timezone("Europe/Berlin")))Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow