From 64c70cd20f9042af6475574c21b600fa2b4be309 Mon Sep 17 00:00:00 2001 From: Valentin Zickner Date: Mon, 23 Sep 2024 17:57:43 +0200 Subject: [PATCH] fix date parsing bug with missing milliseconds --- .../flowable/external_worker_client/utils.py | 5 ++++- external-worker/tests/test_utils.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 external-worker/tests/test_utils.py diff --git a/external-worker/flowable/external_worker_client/utils.py b/external-worker/flowable/external_worker_client/utils.py index 2646320..ad90680 100644 --- a/external-worker/flowable/external_worker_client/utils.py +++ b/external-worker/flowable/external_worker_client/utils.py @@ -1,4 +1,7 @@ from datetime import datetime def parse_date_time(date): - return datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ") if date is not None else None + try: + return datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ") if date is not None else None + except ValueError: + return datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ") if date is not None else None \ No newline at end of file diff --git a/external-worker/tests/test_utils.py b/external-worker/tests/test_utils.py new file mode 100644 index 0000000..b205684 --- /dev/null +++ b/external-worker/tests/test_utils.py @@ -0,0 +1,15 @@ +import datetime +import unittest + +from flowable.external_worker_client.utils import parse_date_time + + +class TestUtils(unittest.TestCase): + + def test_parse_date_time_with_millis(self): + date = parse_date_time('2024-09-23T15:23:08.123Z') + self.assertEqual(datetime.datetime(2024, 9, 23, 15, 23, 8, 123000), date) + + def test_parse_date_time_without_millis(self): + date = parse_date_time('2024-09-23T15:23:08Z') + self.assertEqual(datetime.datetime(2024, 9, 23, 15, 23, 8, 0), date)