Commit 8d92b2ff authored by Vilem Ded's avatar Vilem Ded
Browse files

Merge branch '8-date-is-not-parsed-correctly' into 'master'

fix: validate date

Closes #8

See merge request pinar.alper/metadata-tools!14
parents 460bd82d 0716e5fa
...@@ -3,6 +3,8 @@ import json ...@@ -3,6 +3,8 @@ import json
import logging import logging
import urllib import urllib
import re
from datetime import datetime from datetime import datetime
from json import dumps from json import dumps
from socket import timeout from socket import timeout
...@@ -68,14 +70,26 @@ def process_possible_date(possible_date): ...@@ -68,14 +70,26 @@ def process_possible_date(possible_date):
if isinstance(possible_date, if isinstance(possible_date,
return possible_date.strftime("%Y-%m-%d") return possible_date.strftime("%Y-%m-%d")
elif isinstance(possible_date, int): elif isinstance(possible_date, int):
return "" logging.warning("Date '{possible_date}' is of type Int. Casting to 'YYYY-MM-DD'")
d = datetime.fromordinal(datetime(1900, 1, 1).toordinal() + possible_date - 2)
return d.strftime("%Y-%m-%d")
else: else:
try: try:
d = datetime.strptime(possible_date.replace('/','.'), '%d.%m.%Y') d = process_possible_date_str(possible_date)
return d.strftime("%Y-%m-%d") return d
except ValueError as e: except ValueError as e:
return "" logging.error(f"Date '{possible_date}' is not a valid date of standard format ('YYYY-MM-DD' or 'DD/MM/YYYY')")
return None
def process_possible_date_str(possible_date):
if possible_date == '':
return None
elif re.compile(r'^([0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])$').match(possible_date):
d = datetime.strptime(possible_date.replace('/','.'), '%d.%m.%Y')
return possible_date
d = datetime.strptime(possible_date.replace('/','.'), '%d.%m.%Y')
return d.strftime("%Y-%m-%d")
def get_names_from_string(full_name): def get_names_from_string(full_name):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment