Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

range and span_range with interval size #362

Closed
richard-engineering opened this issue Oct 11, 2016 · 8 comments
Closed

range and span_range with interval size #362

richard-engineering opened this issue Oct 11, 2016 · 8 comments
Labels

Comments

@richard-engineering
Copy link

It would be good to have a parameter for interval size for arrow.Arrow.range and arrow.Arrow.span_range.

Eg I want a range across 5 days between some start and end date, or 2 weeks, etc.

@andrewelkins
Copy link
Contributor

Can you give us a code example?

@richard-engineering
Copy link
Author

richard-engineering commented Dec 1, 2016

Here's an example of how I ended up implementing the code for my own use. Uses funcy. Also I ended up returning a dictionary instead of a list of tuples but that can be easily changed.

    from funcy import flatten, chunks
    import arrow

    def _split_time_range(span_type, start_time, end_time, **kwargs):
        time_range = arrow.Arrow.range(span_type, arrow.get(start_time), arrow.get(end_time))
        start_times = flatten(chunks(1, kwargs.get('interval', 1), time_range))
        start_end_times = {
            c_start_time: start_times[index + 1]
            if index + 1 < len(start_times) else end_time
            for index, c_start_time in enumerate(start_times)
        }
        return start_end_times

The idea is for me to be able to do something like this:

for r in arrow.Arrow.span_range('hour', start, end, interval=2):
    print r
(<Arrow [2013-05-05T12:00:00+00:00]>, <Arrow [2013-05-05T13:59:59.999999+00:00]>)
(<Arrow [2013-05-05T14:00:00+00:00]>, <Arrow [2013-05-05T15:59:59.999999+00:00]>)
(<Arrow [2013-05-05T16:00:00+00:00]>, <Arrow [2013-05-05T17:59:59.999999+00:00]>)
(<Arrow [2013-05-05T18:00:00+00:00]>, <Arrow [2013-05-05T19:59:59.999999+00:00]>)
(<Arrow [2013-05-05T20:00:00+00:00]>, <Arrow [2013-05-05T21:59:59.999999+00:00]>)

@andrewelkins
Copy link
Contributor

Got it appreciate the example. I wonder if it would be more expressive to use a new method called "interval" for this purpose.

@richard-engineering
Copy link
Author

Having a new method called interval sounds fine. Could probably use arrow.range and arrow.span_range underneath like the example.

@andrewelkins
Copy link
Contributor

andrewelkins commented Dec 1, 2016 via email

@systemcatch
Copy link
Collaborator

Done in #386 so closing.

@dongli
Copy link

dongli commented Mar 5, 2020

It seems interval corresponds to span_range. What about range, when I just need one single value of time, not a tuple?

@systemcatch
Copy link
Collaborator

@dongli something like this?

(arrow) chris@ThinkPad:~/arrow$ python
Python 3.7.4 (default, Sep 19 2019, 11:01:37) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import arrow
>>> right_now=arrow.utcnow()
>>> five_hours_ago=right_now.shift(hours=-5)
>>> right_now
<Arrow [2020-03-06T20:12:27.474979+00:00]>
>>> five_hours_ago
<Arrow [2020-03-06T15:12:27.474979+00:00]>
>>> list(arrow.Arrow.range("minute", five_hours_ago, right_now, limit=1))
[<Arrow [2020-03-06T15:12:27.474979+00:00]>]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants