thombashi / DateTimeRange

DateTimeRange is a Python library to handle a time range. e.g. check whether a time is within the time range, get the intersection of time ranges, truncate a time range, iterate through a time range, and so forth.
https://datetimerange.rtfd.io/
MIT License
106 stars 16 forks source link

Substraction from `end_datetime` with negative `relativedelta(days=-3, weeks=-2, ... )` ? #44

Closed 4l1fe closed 1 year ago

4l1fe commented 1 year ago

Hey. I'm looking for a way to create a range where a few points left that are computed starting from an end datetime, not start datetime.

Something like this

dr = DateTimeRange(datetime.now(timezone.utc), datetime.now(timezone.utc) + timedelta(days=60))
dr.range(relativedelta(days=-7))

Now, obviously, i get the error

ValueError: invalid step: expect greater than 0, actual=relativedelta(days=-7)

Is there a workaround for such the case? What do you think about implementing the substraction from end_datetime?

thombashi commented 1 year ago

DateTimeRange supported reverse traversing range as follows:

start = datetime.now(timezone.utc)
end = start + timedelta(days=60)
dr = DateTimeRange(end, start)
dr.range(relativedelta(days=-7))

However, this is not correctly worked at DateTimeRange<2. You can use that DateTimeRange 2.0.0 or newer.

4l1fe commented 1 year ago

That's great. I tested it out with version 2.0 and it works.

Good job! 👍🏻