Skip to content

Commit 479df87

Browse files
committed
fix: precise_diff inconsistent with pendulum.DateTime subclasses
The Rust helper used for dt1 but for dt2. Since is a subclass of , returns False for it, causing dt2's time components to be left at zero. This led to incorrect interval calculations when using native objects (which get converted to in ). Fixes #906
1 parent ae4c405 commit 479df87

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

rust/src/python/helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ pub fn precise_diff<'py>(
184184
total_seconds: 0,
185185
tz: dt2_tz.as_str(),
186186
offset: get_offset(dt2)?,
187-
is_datetime: PyDateTime::is_exact_type_of(dt2),
187+
is_datetime: PyDateTime::is_type_of(dt2),
188188
};
189189
let in_same_tz: bool = dtinfo1.tz == dtinfo2.tz && !dtinfo1.tz.is_empty();
190190
let mut total_days = helpers::day_number(dtinfo2.year, dtinfo2.month as u8, dtinfo2.day as u8)

tests/interval/test_construct.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,29 @@ def test_with_datetimes():
1818
assert_datetime(p.end, 2000, 1, 31)
1919

2020

21+
def test_native_datetime_interval_consistency():
22+
"""Native datetime inputs should produce the same results as pendulum datetimes.
23+
24+
Regression test for https://github.com/python-pendulum/pendulum/issues/906
25+
"""
26+
# With pendulum datetimes
27+
start_p = pendulum.datetime(2025, 7, 25, 19, 26, 34)
28+
end_p = pendulum.datetime(2025, 7, 29, 19, 26, 34)
29+
interval_p = pendulum.interval(start_p, end_p)
30+
31+
# With native datetimes
32+
start_n = datetime(2025, 7, 25, 19, 26, 34)
33+
end_n = datetime(2025, 7, 29, 19, 26, 34)
34+
interval_n = pendulum.interval(start_n, end_n)
35+
36+
# Both should produce identical results
37+
assert interval_p.in_words() == interval_n.in_words()
38+
assert interval_p.in_words() == "4 days"
39+
assert interval_p.days == interval_n.days == 4
40+
assert interval_p.hours == interval_n.hours == 0
41+
assert interval_p.minutes == interval_n.minutes == 0
42+
43+
2144
def test_with_pendulum():
2245
dt1 = pendulum.DateTime(2000, 1, 1)
2346
dt2 = pendulum.DateTime(2000, 1, 31)

0 commit comments

Comments
 (0)