From 1f9b92eaf3adfd58e7ebe91cdc501cf780bd203b Mon Sep 17 00:00:00 2001 From: Boris Staletic Date: Tue, 23 Feb 2021 16:22:02 +0100 Subject: [PATCH 1/2] Use correct duration representation when casting from datetime.timdelta to std::chrono::duration --- include/pybind11/chrono.h | 2 +- tests/test_chrono.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index 551a802ad3..317afd1837 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -35,7 +35,7 @@ template class duration_caster { using rep = typename type::rep; using period = typename type::period; - using days = std::chrono::duration>; + using days = std::chrono::duration>; // signed 25 bits required by the standard. bool load(handle src, bool) { using namespace std::chrono; diff --git a/tests/test_chrono.py b/tests/test_chrono.py index e9e24e0826..565d7eceed 100644 --- a/tests/test_chrono.py +++ b/tests/test_chrono.py @@ -140,9 +140,13 @@ def test_chrono_duration_roundtrip(): cpp_diff = m.test_chrono3(diff) - assert cpp_diff.days == diff.days - assert cpp_diff.seconds == diff.seconds - assert cpp_diff.microseconds == diff.microseconds + assert cpp_diff == diff + + # Negative timedelta roundtrip + diff = datetime.timedelta(microseconds=-1) + cpp_diff = m.test_chrono3(diff) + + assert cpp_diff == diff def test_chrono_duration_subtraction_equivalence(): From b3b3fd45baa04fcfe711c5484295043fcaa2333c Mon Sep 17 00:00:00 2001 From: Boris Staletic Date: Wed, 24 Feb 2021 11:31:32 +0100 Subject: [PATCH 2/2] When asserting datetime/timedelta/date/time we can equality-compare whole objects --- tests/test_chrono.py | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/tests/test_chrono.py b/tests/test_chrono.py index 565d7eceed..12312c7207 100644 --- a/tests/test_chrono.py +++ b/tests/test_chrono.py @@ -39,9 +39,7 @@ def test_chrono_system_clock_roundtrip(): # They should be identical (no information lost on roundtrip) diff = abs(date1 - date2) - assert diff.days == 0 - assert diff.seconds == 0 - assert diff.microseconds == 0 + assert diff == datetime.timedelta(0) def test_chrono_system_clock_roundtrip_date(): @@ -64,9 +62,7 @@ def test_chrono_system_clock_roundtrip_date(): assert diff.microseconds == 0 # Year, Month & Day should be the same after the round trip - assert date1.year == date2.year - assert date1.month == date2.month - assert date1.day == date2.day + assert date1 == date2 # There should be no time information assert time2.hour == 0 @@ -117,10 +113,7 @@ def test_chrono_system_clock_roundtrip_time(time1, tz, monkeypatch): assert isinstance(time2, datetime.time) # Hour, Minute, Second & Microsecond should be the same after the round trip - assert time1.hour == time2.hour - assert time1.minute == time2.minute - assert time1.second == time2.second - assert time1.microsecond == time2.microsecond + assert time1 == time2 # There should be no date information (i.e. date = python base date) assert date2.year == 1970 @@ -157,9 +150,7 @@ def test_chrono_duration_subtraction_equivalence(): diff = date2 - date1 cpp_diff = m.test_chrono4(date2, date1) - assert cpp_diff.days == diff.days - assert cpp_diff.seconds == diff.seconds - assert cpp_diff.microseconds == diff.microseconds + assert cpp_diff == diff def test_chrono_duration_subtraction_equivalence_date(): @@ -170,9 +161,7 @@ def test_chrono_duration_subtraction_equivalence_date(): diff = date2 - date1 cpp_diff = m.test_chrono4(date2, date1) - assert cpp_diff.days == diff.days - assert cpp_diff.seconds == diff.seconds - assert cpp_diff.microseconds == diff.microseconds + assert cpp_diff == diff def test_chrono_steady_clock(): @@ -187,9 +176,7 @@ def test_chrono_steady_clock_roundtrip(): assert isinstance(time2, datetime.timedelta) # They should be identical (no information lost on roundtrip) - assert time1.days == time2.days - assert time1.seconds == time2.seconds - assert time1.microseconds == time2.microseconds + assert time1 == time2 def test_floating_point_duration():