Commit a7db2e8c authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add module with utility functions.

For now contains only one function which returns a date string into a
date object.
parent 4fd14124
"""
Various utility functions.
"""
import datetime
import re
def date_string_to_date(p_date):
"""
Given a date in YYYY-MM-DD, returns a Python date object. Returns None
if the date is invalid.
"""
result = None
parsed_date = re.match(r'(\d{4})-(\d{2})-(\d{2})', p_date)
if parsed_date:
try:
date = datetime.date(
int(parsed_date.group(1)), # year
int(parsed_date.group(2)), # month
int(parsed_date.group(3)) # day
)
result = date
except ValueError:
pass # just return None
return result
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