Date calculation with Python

Asked

Viewed 2,644 times

0

I have a script that needs to make a difference between 2 collected dates of some files

In this case the script collects the date of a file. txt (dt_log) and the current Systema date (dt_sys) and my difficulty is to find the difference between these 2 dates

Example

The file was created on 20/07/2018 10:05:55 The date of the system is 25/07/2018 11:10:02

The return I would need to be printed is 120 hours (5 days = 120 hours)

It is possible to do this?

Follows excerpt from the code:

# LOG DATE AND SYSTEM DATE
## LOG dt_log = os.stat(log[0]) dt_log = time.ctime(dt_log.st_ctime) dt_log = dt_log.split(' ') print("Hora da Log: ", dt_log[3])

## SYSTEM dt_sys = time.time() dt_sys = time.ctime(dt_sys) dt_sys = dt_sys.split(' ') print("Hora do Sys: ", dt_sys[3])

# CALCULATE DIFFERENCE BETWEEN DATES calc = int(dt_log[3] - dt_sys[3])

Code that takes the file and system date:

    # LOG DATE AND SYSTEM DATE
## LOG
dt_log = os.stat(log[0])
dt_log = time.ctime(dt_log.st_ctime)
#dt_log = dt_log.split(' ')
print("Hora da Log: ", dt_log)

## SYSTEM
dt_sys = time.time()
dt_sys = time.ctime(dt_sys)
#dt_sys = dt_sys.split(' ')
print("Hora do Sys: ",dt_sys)

1 answer

2


Yes, it is possible to do this with the datetime library and the strptime function, which turns a string into a date according to a format.

from datetime import datetime

def diferenca_dias(d1, d2):
    d1 = datetime.strptime(d1, "%d/%m/%Y %H:%M:%S")
    d2 = datetime.strptime(d2, "%d/%m/%Y %H:%M:%S")
    return abs((d2 - d1).days * 24)

Example of use:

>>> d1 = '20/07/2018 10:05:55'
>>> d2 = '25/07/2018 11:10:02'
>>> print(diferenca_dias(d1, d2))
120

Sources:

https://docs.python.org/2/library/datetime.html https://stackoverflow.com/questions/8419564/difference-between-two-dates-in-python

Edit: These "%d/%m/%Y %H:%M:%S" can be seen here: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

The different_days function returns a timedelta. To compare whether returno is greater or less than a number of days, we can compare: if retorno_da_funcao > timedelta(days, seconds). To use timedelta() we must import it (from datetime import timedelta).

  • Thanks for the help but I found a new problem.... The return I get is Mon Jul 23 13:49:32 2018

  • Then change the date format to: datetime.strptime(D1, "%a %b %d %H:%M:%S %Y"). These %a, %b and others can be seen here: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

  • Guy spun! But he only brings me time in days right? And if you want to put for hours (for example the file was created at 16:00 and I pull the report here at 17:30) I do not care much for minutes but rather with the hours, it has as ?

  • Yes. Just change the Return to: Return abs(D2 - D1). Or turn abs((D2 - D1).Seconds) and turn the seconds into days and hours .

  • Guy sorry to be annoying but is that I’m new in python, I’m trying to make a if co the return (that gave 2 days, 02:37:54) but he says that I can not use the '>' has some way to circumvent this?

  • The different_days() function returns a timedelta object. We can compare two timedelts. We can write: if retorno_da_function > timedelta(day, seconds). Just be sure to import timedelta (from datetime import timedelta).

  • Thank you very much man you literally saved me! Is there a book you can recommend for an aspiring to become a Python master? or only time can do so?

  • I like to learn in practice. My recommendation is to read the Python documentation as you encounter problems to learn more features that may be useful and search the internet.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.