question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A complex generator of a lot of dates.

See original GitHub issue

Sometimes 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:closed
  • Created 5 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
sobolevncommented, Aug 29, 2018

Why not both? We can have two methods that do A and B.

0reactions
lk-geimfaricommented, Oct 16, 2018

Fixed in #539

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found