Skip to content

Commit f110889

Browse files
authored
Use correct duration representation when casting from datetime.timdelta to std::chrono::duration (#2870)
* Use correct duration representation when casting from datetime.timdelta to std::chrono::duration * When asserting datetime/timedelta/date/time we can equality-compare whole objects
1 parent 44678e5 commit f110889

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

include/pybind11/chrono.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ template <typename type> class duration_caster {
3535
using rep = typename type::rep;
3636
using period = typename type::period;
3737

38-
using days = std::chrono::duration<uint_fast32_t, std::ratio<86400>>;
38+
using days = std::chrono::duration<int_least32_t, std::ratio<86400>>; // signed 25 bits required by the standard.
3939

4040
bool load(handle src, bool) {
4141
using namespace std::chrono;

tests/test_chrono.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ def test_chrono_system_clock_roundtrip():
3939

4040
# They should be identical (no information lost on roundtrip)
4141
diff = abs(date1 - date2)
42-
assert diff.days == 0
43-
assert diff.seconds == 0
44-
assert diff.microseconds == 0
42+
assert diff == datetime.timedelta(0)
4543

4644

4745
def test_chrono_system_clock_roundtrip_date():
@@ -64,9 +62,7 @@ def test_chrono_system_clock_roundtrip_date():
6462
assert diff.microseconds == 0
6563

6664
# Year, Month & Day should be the same after the round trip
67-
assert date1.year == date2.year
68-
assert date1.month == date2.month
69-
assert date1.day == date2.day
65+
assert date1 == date2
7066

7167
# There should be no time information
7268
assert time2.hour == 0
@@ -117,10 +113,7 @@ def test_chrono_system_clock_roundtrip_time(time1, tz, monkeypatch):
117113
assert isinstance(time2, datetime.time)
118114

119115
# Hour, Minute, Second & Microsecond should be the same after the round trip
120-
assert time1.hour == time2.hour
121-
assert time1.minute == time2.minute
122-
assert time1.second == time2.second
123-
assert time1.microsecond == time2.microsecond
116+
assert time1 == time2
124117

125118
# There should be no date information (i.e. date = python base date)
126119
assert date2.year == 1970
@@ -140,9 +133,13 @@ def test_chrono_duration_roundtrip():
140133

141134
cpp_diff = m.test_chrono3(diff)
142135

143-
assert cpp_diff.days == diff.days
144-
assert cpp_diff.seconds == diff.seconds
145-
assert cpp_diff.microseconds == diff.microseconds
136+
assert cpp_diff == diff
137+
138+
# Negative timedelta roundtrip
139+
diff = datetime.timedelta(microseconds=-1)
140+
cpp_diff = m.test_chrono3(diff)
141+
142+
assert cpp_diff == diff
146143

147144

148145
def test_chrono_duration_subtraction_equivalence():
@@ -153,9 +150,7 @@ def test_chrono_duration_subtraction_equivalence():
153150
diff = date2 - date1
154151
cpp_diff = m.test_chrono4(date2, date1)
155152

156-
assert cpp_diff.days == diff.days
157-
assert cpp_diff.seconds == diff.seconds
158-
assert cpp_diff.microseconds == diff.microseconds
153+
assert cpp_diff == diff
159154

160155

161156
def test_chrono_duration_subtraction_equivalence_date():
@@ -166,9 +161,7 @@ def test_chrono_duration_subtraction_equivalence_date():
166161
diff = date2 - date1
167162
cpp_diff = m.test_chrono4(date2, date1)
168163

169-
assert cpp_diff.days == diff.days
170-
assert cpp_diff.seconds == diff.seconds
171-
assert cpp_diff.microseconds == diff.microseconds
164+
assert cpp_diff == diff
172165

173166

174167
def test_chrono_steady_clock():
@@ -183,9 +176,7 @@ def test_chrono_steady_clock_roundtrip():
183176
assert isinstance(time2, datetime.timedelta)
184177

185178
# They should be identical (no information lost on roundtrip)
186-
assert time1.days == time2.days
187-
assert time1.seconds == time2.seconds
188-
assert time1.microseconds == time2.microseconds
179+
assert time1 == time2
189180

190181

191182
def test_floating_point_duration():

0 commit comments

Comments
 (0)