Skip to content

Commit 3b40d5f

Browse files
committed
API: Remove when parameter from naturaldelta()
This parameter doesn't make sense when the input should be a timedelta (and not a datetime). The refactoring allow allows using the largest timedelta without causing an OverflowError. Fixes #245 Fixes #247
1 parent 818fd1d commit 3b40d5f

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

src/humanize/time.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,24 +161,26 @@ def naturaldelta(
161161
value,
162162
months=True,
163163
minimum_unit="seconds",
164-
when=None,
165164
) -> str:
166165
"""Return a natural representation of a timedelta or number of seconds.
167166
168167
This is similar to `naturaltime`, but does not add tense to the result.
169168
170169
Args:
171-
value (datetime.timedelta): A timedelta or a number of seconds.
170+
value (datetime.timedelta or int): A timedelta or a number of seconds.
172171
months (bool): If `True`, then a number of months (based on 30.5 days) will be
173172
used for fuzziness between years.
174173
minimum_unit (str): The lowest unit that can be used.
175-
when (datetime.datetime): Point in time relative to which _value_ is
176-
interpreted. Defaults to the current time in the local timezone.
177-
Deprecated in version 3.14; If you need to construct a timedelta,
178-
do it inline as the first argument.
174+
when (datetime.datetime): Removed in version 4.0; If you need to
175+
construct a timedelta, do it inline as the first argument.
179176
180177
Returns:
181-
str: A natural representation of the amount of time elapsed.
178+
str (str or `value`): A natural representation of the amount of time
179+
elapsed unless `value` is not datetime.timedelta or cannot be
180+
converted to int. In that case, a `value` is returned unchanged.
181+
182+
Raises:
183+
OverflowError: If `value` is too large to convert to datetime.timedelta.
182184
183185
Examples
184186
Compare two timestamps in a custom local timezone::
@@ -190,24 +192,21 @@ def naturaldelta(
190192
now = dt.datetime.now(tz=berlin)
191193
later = now + dt.timedelta(minutes=30)
192194
193-
assert naturaldelta(later, when=now) == "30 minutes"
195+
assert naturaldelta(later - now) == "30 minutes"
194196
"""
195-
if when:
196-
warnings.warn(
197-
"The `when` parameter of `naturaldelta()` is deprecated and will be "
198-
"removed in humanize 4.0. If you need to construct a timedelta, "
199-
"do it inline as the first argument.",
200-
DeprecationWarning,
201-
stacklevel=2,
202-
)
203197
tmp = _Unit[minimum_unit.upper()]
204198
if tmp not in (_Unit.SECONDS, _Unit.MILLISECONDS, _Unit.MICROSECONDS):
205199
raise ValueError(f"Minimum unit '{minimum_unit}' not supported")
206200
minimum_unit = tmp
207201

208-
date, delta = _date_and_delta(value, now=when)
209-
if date is None:
210-
return value
202+
if isinstance(value, dt.timedelta):
203+
delta = value
204+
else:
205+
try:
206+
value = int(value)
207+
delta = dt.timedelta(seconds=value)
208+
except (ValueError, TypeError):
209+
return value
211210

212211
use_months = months
213212

@@ -312,7 +311,7 @@ def naturaltime(
312311
future = date > now
313312

314313
ago = _("%s from now") if future else _("%s ago")
315-
delta = naturaldelta(delta, months, minimum_unit, when=when)
314+
delta = naturaldelta(delta, months, minimum_unit)
316315

317316
if delta == _("a moment"):
318317
return _("now")

0 commit comments

Comments
 (0)