lmsherman
lmsherman

Reputation: 57

Getting date and time with regex

I'm trying to extract the date and time from a string establish the delta between that and the current date and time. I tried to convert the regex output from a list to a string and it shows as type=string but is in the following format - ('18:06:39', 'Jan 30 2020').

import re
from datetime import datetime, timedelta, date

string = 'configuration change at 18:06:39 EET Thu Jan 30 2020 by netbrain'
chg_date = re.findall(r"(\d{2}:\d{2}:\d{2}) \w+ \w+ (\w{3} \d{2} \d{4})", string)
chg_date_str = ''.join(map(str, chg_date))
now = datetime.now()
now_format = now.strftime("%H:%M:%S, %b %d %y")
time_difference = now_format - chg_date_str
print(chg_date_str)
print(time_difference)

I get the following error.

Traceback (most recent call last):
  File "C:/Users/MattSherman/Desktop/Python/y.py", line 15, in <module>
    time_difference = now_format - chg_date_str
TypeError: unsupported operand type(s) for -: 'str' and 'str'

Upvotes: 0

Views: 360

Answers (3)

martineau
martineau

Reputation: 123413

If you want to compute a time delta, you need to do arithmetic with datetime instances. You can convert the results of the findall() into a datetime using the datetime.strptime() function as shown:

import re
from datetime import datetime, timedelta, date

string = 'configuration change at 18:06:39 EET Thu Jan 30 2020 by netbrain'
matches = re.findall(r"(\d{2}:\d{2}:\d{2}) \w+ \w+ (\w{3} \d{2} \d{4})", string)
chg_date_str = ' '.join(map(str, matches[0]))
chg_date = datetime.strptime(chg_date_str, "%H:%M:%S %b %d %Y")
now = datetime.now()
time_difference = now - chg_date

print(chg_date_str)
print(time_difference)

Output:

18:06:39 Jan 30 2020
5 days, 16:34:32.661231

Upvotes: 2

javier123454321
javier123454321

Reputation: 61

Others answered it but there are 2 main issues.

You were trying to substract 2 strings from each other, python cannot do that, instead you should substract 2 datetime objects. Also, re.findall() is returning a list of length 1, so when concatenating chg_date into a chg_date_str you actually had to concatenate the 0th item in the returned list, which would be chg_date_str[0]. It also looks cleaner if you concatenate with a ', ' instead of an empty string, of course, updating the datetime parameters accordingly.

import re
from datetime import datetime, timedelta, date

string = 'configuration change at 18:06:39 EET Thu Jan 30 2020 by netbrain'
chg_date = re.findall(r"(\d{2}:\d{2}:\d{2}) \w+ \w+ (\w{3} \d{2} \d{4})", string)


chg_date_str = ', '.join(map(str, chg_date[0]))
datetime_object = datetime.strptime(chg_date_str, '%H:%M:%S, %b %d %Y')

time_difference = datetime.now() - datetime_object
print(chg_date_str)
print(time_difference)

outputs:

18:06:39, Jan 30 2020
5 days, 19:05:05.272112

which I believe is what you want.

Upvotes: 1

LinnTroll
LinnTroll

Reputation: 705

You have many problems in you code.

  • findall returns list of tuples. You should iterate in findall results or use search instead of findall
  • you join parts of data using '', but you need ' '
  • %y is wrong pattern for 4-digit year, should use %Y
  • you converting date to string and trying find difference between two strings...

I think you code should look something like this:

import re
from datetime import datetime

string = 'configuration change at 18:06:39 EET Thu Jan 30 2020 by netbrain'
chg_dates = re.findall(r"(\d{2}:\d{2}:\d{2}) \w+ \w+ (\w{3} \d{2} \d{4})", string)
for chg_date in chg_dates:
    chg_date_str = ' '.join(map(str, chg_date))
    chg_date_date = datetime.strptime(chg_date_str, "%H:%M:%S %b %d %Y")
    now = datetime.now()
    time_difference = now - chg_date_date
    print(time_difference)

Upvotes: 2

Related Questions