0

I'm looking for the module ChronoUnit

java.time.temporal.ChronoUnit 

implementation in Python, found in Java 8.

The reason this modules is useful, is because it contains a procedure that computes the Days, Months, Years etc.. between to arbitrary dates.

PS: Implementing the date computation in python, can result in a lot of problems as there are a lot of corner cases that I simply have no time consider at the moment, so please be constructive while answering.

Edit: I think my question is not clear enough, but what I'm trying to accomplish is to be able to actually substract one date from another and as a result to get the months, days, years etc.. between the two.

As per juanpa.arrivillaga comment the arrow library provides a useful method that provides a near similar function, I think that I'll answer my own question now.

Cristofor
  • 2,077
  • 2
  • 15
  • 23
  • 3
    What exactly went wrong when googling for the python [time](https://docs.python.org/3/library/time.html)/[datetime](https://docs.python.org/3/library/datetime.html) api and tutorials like [this](https://www.tutorialspoint.com/python/python_date_time.htm) or [this](https://realpython.com/python-time-module/)? – akuzminykh Feb 22 '20 at 22:51
  • The problem is that most of the advertised methods work only in the inverse way, I'll edit the question to make that clear. – Cristofor Feb 22 '20 at 22:53
  • 1
    The python standard library `datetime` package contains the classes `date`, `time`, `datetime`, and `timedelta` which are probably where you want to start looking. There is also a popular third-party library [`arrow`](https://arrow.readthedocs.io/en/latest/) that many find easier to work with, and it is compatible with the standard lib. – juanpa.arrivillaga Feb 22 '20 at 22:54
  • @juanpa.arrivillaga arrow seems a powerful library! – Cristofor Feb 22 '20 at 22:58

1 Answers1

0

Thanks to @junpa.arrivillaga I figured out that the procedure might be easily implemented in python thanks to the arrow library.

The api to be used it the following:

arrow.Arrow.range('hour', start, end)

The final method in Python would be:

'''
    Computes day, month, year s 
    between two dates.
    frame - time frame 
    start - date start 
    end - date finish
    This method only works if the generated timeframe between to dates 
    is finite, as Arrow.range returns a generator!
'''
def count_frame_between_dates(frame, start, end):
    return len(list(arrow.Arrow.range(frame, start, end)))

Edit: Arrow.range returns a generator, in theory you can't compute the length of a generator, but if you are sure the generator you're using is returning a finite set of elements then you can convert this generato into a list then use len() to compute it's length.

Thanks to everyone.

Cristofor
  • 2,077
  • 2
  • 15
  • 23