I’m working on a proposal to add a new function to the datetime module, which functions similarly to the built-in range function, but for datetime objects.
Function Overview
The datetimewalker function would allow users to generate a sequence of datetime objects between a start and end date with a specified timedelta interval.
Example:
from datetime import datetime, timedelta, datetimewalker
start = datetime(2024, 8, 25)
end = datetime(2024, 8, 30)
delta = timedelta(days=1)
for i in datetimewalker(start, end, delta):
print(i)
I have implemented this function and am considering creating an issue to start the contribution process. Before I do that, I wanted to get feedback from the community. Any advice or suggestions would be greatly appreciated!
Thank you all for your feedback and suggestions. I appreciate the references to existing solutions using packages like numpy, pandas, more_itertools, and others.
While it’s true that these packages offer powerful tools for working with date ranges and many other tasks, I feel that the functionality provided by datetimewalker is simple and basic enough that it could be beneficial to have it directly available in the datetime module without needing to install any additional packages.
The idea is to provide a straightforward, built-in solution that could be useful in many everyday scenarios, especially when users might not want to bring in a whole library just for a simple date range iteration. I believe that integrating this into the standard library would make this common task more accessible and convenient for everyone.
Again, thank you for the insights—it’s great to hear different perspectives on this!
What should be the correct behavior be with regards to stuff like daylight saving times, leap seconds or a timezone being moved around? Should the difference always be exactly one day? Should the lower parts of the datetime stay the same?
I don’t think we should add a naive version to the stdlib version - Someone should make a survey of various real world usecases and write a PEP suggesting a solution that covers as many of these as possible. I don’t really know what this solution would be, but I am pretty sure you did not do the research for this (if you did, please show it).