A complex generator of a lot of dates.
See original GitHub issueSometimes we need to generate a lot of datetime
objects and at this moment we have not a good solution for this, so I suggest something like this:
import random
from datetime import datetime, timedelta
def bulk_create_datetimes(date_start, date_end, **kwargs):
"""Bulk create datetime objects
:param date_start: Begin of the range.
:param date_end: End of the range.
:param kwargs: Keyword arguments for datetime.timedelta
:return: List of datetime objects
"""
dt_objects = []
assert date_start and date_end
while date_start <= date_end:
date_start += timedelta(**kwargs)
dt_objects.append(date_start)
return dt_objects
How it works:
>>> week_ago = datetime.now() - timedelta(days=7)
>>> now = datetime.now()
>>> datetimes = bulk_create_datetimes(week_ago, now, minutes=60)
>>> for i in datetimes:
.... print(i)
Result:
2018-08-21 12:58:55.687991
2018-08-21 13:58:55.687991
2018-08-21 14:58:55.687991
2018-08-21 15:58:55.687991
2018-08-21 16:58:55.687991
...
2018-08-28 12:58:55.687991
So, I think that it’s pretty simple to understand and there is no need for comments.
That gives extreme flexibility in generating dates because a user can generate dates with any difference between them (including seconds, minutes and etc.).
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (7 by maintainers)
Top Results From Across the Web
Random Date Generator Excel Template
I found a way to generate it. But, I have to exclude some specific dates and this requires additional effort. But, thanks to...
Read more >Time Date generator in Motion
Value is not available when the Animate checkbox is selected. This parameter can be animated using keyframes. Set Current Time: A button that...
Read more >Using Snowflake's Generator Function to Create Date and ...
To achieve this, we use GENERATOR to create a table with enough rows to cover our timeframe, then convert the row number into...
Read more >Make date range generator interface stateful or stateless?
An app has a feature that's much like any calendar application (like the Outlook calendar for example). Consequently, I need to do a...
Read more >Automated Generator for Complex and Realistic Test Data
Some type of tests especially functional tests and stress test requires a large amount of realistic test data. We are proposing a tool...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Why not both? We can have two methods that do
A
andB
.Fixed in #539