Skip to content

Conversation

@Eren-Acar
Copy link

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

Closes #906

Description

This pull request fixes #906, where the Interval.in_words() method produced inconsistent output when using native Python datetime objects.

Solution

In calendars variable with native Python, delta attributes such as self._delta.hours are not calculated correctly. These kind of functions have been re-codded to use total_seconds() instead. Now, the Interval.in_words() function works correctly with Python’s native datetime.datetime.

Example

import pendulum
from datetime import datetime

start_time = datetime(2025, 7, 25, 19, 26, 34)
end_time = datetime(2025, 7, 29, 19, 26, 34)

interval = pendulum.interval(start_time, end_time)
print("Native datetime interval: \n")
print(interval.in_words())
print("Interval days", interval.days)
print("Interval hours", interval.hours)
print("Interval minutes", interval.minutes)
print("Interval In Days: ", interval.in_days())
print("Interval In Hours: ", interval.in_hours() / 24)


start_time = pendulum.datetime(2025, 7, 25, 19, 26, 34)
end_time = pendulum.datetime(2025, 7, 29, 19, 26, 34)

interval = pendulum.interval(start_time, end_time)
print("\nPendulum datetime interval:\n")
print(interval.in_words())
print("Interval days", interval.days)
print("Interval hours", interval.hours)
print("Interval minutes", interval.minutes)
print("Interval In Days: ", interval.in_days())
print("Interval In Hours: ", interval.in_hours() / 24)

Before PR's Edit:

before

After PR's Edit:

after

@codspeed-hq
Copy link

codspeed-hq bot commented Oct 19, 2025

CodSpeed Performance Report

Merging #921 will not alter performance

Comparing Eren-Acar:master (1bb428a) with master (3382956)

Summary

✅ 1 untouched

Copy link
Member

@ashb ashb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is self._delta not correct already?

At that least this needs unit tests please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pendulum interval seems incompatible with Python native datetime

2 participants