From 3bff2a72437a11f65d89575cd87646a4e82f88e0 Mon Sep 17 00:00:00 2001 From: Antonio Golfari Date: Tue, 20 Jun 2023 12:00:38 +0200 Subject: [PATCH] get_date optimised --- airscore/core/calcUtils.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/airscore/core/calcUtils.py b/airscore/core/calcUtils.py index 4386fdc2..a01809a6 100644 --- a/airscore/core/calcUtils.py +++ b/airscore/core/calcUtils.py @@ -130,14 +130,18 @@ def get_datetime(t): return t -def get_date(t): +def get_date(t) -> datetime.date or any: """ Transform string in datetime.date Gets first 10 positions in string ('YYYY-mm-dd') + It should work with any separator """ try: - return datetime.strptime(t[:10], '%Y-%m-%d').date() - except (ValueError, TypeError) as e: + d = t[:10] + s = d[4] + f = f"%Y{s}%m{s}%d" + return datetime.strptime(d, f).date() + except (ValueError, TypeError, IndexError) as e: return t