From 97cb71050d6050556fe984138eeec38cfa547b46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Schwartz?= <40674593+3schwartz@users.noreply.github.com> Date: Thu, 9 Jan 2025 08:36:21 +0100 Subject: [PATCH 1/2] make indention --- deepdiff/helper.py | 8 ++++---- tests/test_diff_datetime.py | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/deepdiff/helper.py b/deepdiff/helper.py index e0be6a19..c2bdd3a0 100644 --- a/deepdiff/helper.py +++ b/deepdiff/helper.py @@ -623,10 +623,10 @@ def datetime_normalize(truncate_datetime, obj): obj = obj.replace(minute=0, second=0, microsecond=0) elif truncate_datetime == 'day': obj = obj.replace(hour=0, minute=0, second=0, microsecond=0) - if isinstance(obj, datetime.datetime): - obj = obj.replace(tzinfo=datetime.timezone.utc) - elif isinstance(obj, datetime.time): - obj = time_to_seconds(obj) + if isinstance(obj, datetime.datetime): + obj = obj.replace(tzinfo=datetime.timezone.utc) + elif isinstance(obj, datetime.time): + obj = time_to_seconds(obj) return obj diff --git a/tests/test_diff_datetime.py b/tests/test_diff_datetime.py index 54555e6a..99957472 100644 --- a/tests/test_diff_datetime.py +++ b/tests/test_diff_datetime.py @@ -1,8 +1,28 @@ -from datetime import date, datetime, time +from datetime import date, datetime, time, timezone from deepdiff import DeepDiff class TestDiffDatetime: + def test_datetime_within_array_with_timezone_diff(self): + """Testing for the correct setting and usage of epsilon.""" + d1 = [datetime(2020, 8, 31, 13, 14, 1)] + d2 = [datetime(2020, 8, 31, 13, 14, 1, tzinfo=timezone.utc)] + + res = DeepDiff(d1, d2) + expected = { + "values_changed": { + "root[0]": { + "new_value": datetime(2020, 8, 31, 13, 14, 1, tzinfo=timezone.utc), + "old_value": datetime(2020, 8, 31, 13, 14, 1), + } + } + } + assert res == expected + + res_ignore = DeepDiff(d1, d2, ignore_order=True) + assert res_ignore == expected + + def test_datetime_diff(self): """Testing for the correct setting and usage of epsilon.""" d1 = {"a": datetime(2023, 7, 5, 10, 11, 12)} From 037b73d5a8930205565bc624aae0a9b2fb544ba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Schwartz?= <40674593+3schwartz@users.noreply.github.com> Date: Thu, 9 Jan 2025 08:42:30 +0100 Subject: [PATCH 2/2] Add test case for truncate_datetime --- tests/test_diff_datetime.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_diff_datetime.py b/tests/test_diff_datetime.py index 99957472..da257fa9 100644 --- a/tests/test_diff_datetime.py +++ b/tests/test_diff_datetime.py @@ -22,6 +22,9 @@ def test_datetime_within_array_with_timezone_diff(self): res_ignore = DeepDiff(d1, d2, ignore_order=True) assert res_ignore == expected + res_truncate = DeepDiff(d1, d2, truncate_datetime='second') + assert res_truncate == {} + def test_datetime_diff(self): """Testing for the correct setting and usage of epsilon."""