From 9520aeb655b8e97ecd0731399d01f8bd0e7ae6b5 Mon Sep 17 00:00:00 2001 From: Pip Liggins Date: Fri, 16 Jun 2023 15:56:43 +0100 Subject: [PATCH] allow date formats to be specifed in yearsElapsed function --- adtl/transformations.py | 6 +++--- tests/test_transformations.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/adtl/transformations.py b/adtl/transformations.py index 0543a32..a85b8b8 100644 --- a/adtl/transformations.py +++ b/adtl/transformations.py @@ -54,12 +54,12 @@ def getFloat(value, set_decimal=None, separator=None): return value -def yearsElapsed(birthdate, currentdate): +def yearsElapsed(birthdate, currentdate, bd_format="%Y-%m-%d", cd_format="%Y-%m-%d"): if birthdate in [None, ""] or currentdate in [None, ""]: return None - bd = datetime.strptime(birthdate, "%Y-%m-%d") - cd = datetime.strptime(currentdate, "%Y-%m-%d") + bd = datetime.strptime(birthdate, bd_format) + cd = datetime.strptime(currentdate, cd_format) days = cd - bd return pint.Quantity(days.days, "days").to("years").m diff --git a/tests/test_transformations.py b/tests/test_transformations.py index cf2a2ea..7e7ea93 100644 --- a/tests/test_transformations.py +++ b/tests/test_transformations.py @@ -24,6 +24,20 @@ def test_yearsElasped(test_date_birth, test_date_current, expected): ) +@pytest.mark.parametrize( + "test_date_birth, test_date_current, bd_format, cd_format, expected", + [ + ("1950", "2023-01-01 00:00", "%Y", "%Y-%m-%d %H:%M", 73), + ], +) +def test_yearsElasped_format( + test_date_birth, test_date_current, bd_format, cd_format, expected +): + assert transform.yearsElapsed( + test_date_birth, test_date_current, bd_format, cd_format + ) == pytest.approx(expected, 0.001) + + @pytest.mark.parametrize( "test_duration_start, test_duration_current, expected", [