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)
Thanks for the help but I found a new problem.... The return I get is Mon Jul 23 13:49:32 2018
– user68537
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
– AlexCiuffa
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 ?
– user68537
Yes. Just change the Return to: Return abs(D2 - D1). Or turn abs((D2 - D1).Seconds) and turn the seconds into days and hours .
– AlexCiuffa
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?
– user68537
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).
– AlexCiuffa
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?
– user68537
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.
– AlexCiuffa