Python Forum

Full Version: How to compare timestamps in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have just playing with python , I am stuck for the following problem :

file1 :
Patching tool version 12.1.0.2.0 Production on Fri Feb 23 01:10:28 2018

Bootstrapping registry and package to current versions...done
statement ERR-2001: table is corrupt check for cause

could not determine the current status.

Patching tool version 12.1.0.2.0 Production on Fri Feb 23 01:10:58 2018

file2 :

LOG file opened at 02/03/18 01:11:05

DUP-05004: statement1
DUP-05007: statement2


LOG file opened at 02/03/18 01:11:14

DUP-05004: statement1

DUP-05007: statement2


LOG file opened at 02/23/18 01:10:33

DUP-05004: statement1

DUP-05007: statement2

I need to look for the ERR-2001 in file1 if it matches then go to file2 and print the message nearest to the timestamp found in file1 within two minutes of range .


1) What regex I use get the timestamp from file 1 and then file 2 ?

2) How to I acheive
so in this case file1 Start: Fri Feb 23 01:10:28 2018
End time : Fri Feb 23 01:10:58 2018
check in file 2 nearest to file1 end time : 02/23/18 01:10:33
I would just make a simple regex to get potential time stamps, and then check them by trying to convert them to datetime objects from the datetime module. Then you can compare the difference of the datetime objects with a timedelta object.
Hi all ,

I tried the following regex :

x = re.findall ( "([A-Z][a-z]{2}\s)([0-9]{2}\s[0-2][0-9](:[0-5][0-9]){2}\s[0-9]{4})", st )
print x
[('Feb ', '23 01:10:28 2018', ':28'), ('Feb ', '23 01:10:36 2018', ':36')]

print x[0][1]

23 01:10:28 2018

y = datetime.datetime.strptime ( x[0][1], '%d %H:%M:%S %y')
ValueError: unconverted data remains: 18