@@ -87,24 +87,26 @@ def naturaldelta(
8787 value ,
8888 months = True ,
8989 minimum_unit = "seconds" ,
90- when = None ,
9190) -> str :
9291 """Return a natural representation of a timedelta or number of seconds.
9392
9493 This is similar to `naturaltime`, but does not add tense to the result.
9594
9695 Args:
97- value (datetime.timedelta): A timedelta or a number of seconds.
96+ value (datetime.timedelta or int ): A timedelta or a number of seconds.
9897 months (bool): If `True`, then a number of months (based on 30.5 days) will be
9998 used for fuzziness between years.
10099 minimum_unit (str): The lowest unit that can be used.
101- when (datetime.datetime): Point in time relative to which _value_ is
102- interpreted. Defaults to the current time in the local timezone.
103- Deprecated in version 3.14; If you need to construct a timedelta,
104- do it inline as the first argument.
100+ when (datetime.datetime): Removed in version 4.0; If you need to
101+ construct a timedelta, do it inline as the first argument.
105102
106103 Returns:
107- str: A natural representation of the amount of time elapsed.
104+ str (str or `value`): A natural representation of the amount of time
105+ elapsed unless `value` is not datetime.timedelta or cannot be
106+ converted to int. In that case, a `value` is returned unchanged.
107+
108+ Raises:
109+ OverflowError: If `value` is too large to convert to datetime.timedelta.
108110
109111 Examples
110112 Compare two timestamps in a custom local timezone::
@@ -116,24 +118,21 @@ def naturaldelta(
116118 now = dt.datetime.now(tz=berlin)
117119 later = now + dt.timedelta(minutes=30)
118120
119- assert naturaldelta(later, when= now) == "30 minutes"
121+ assert naturaldelta(later - now) == "30 minutes"
120122 """
121- if when :
122- warnings .warn (
123- "The `when` parameter of `naturaldelta()` is deprecated and will be "
124- "removed in humanize 4.0. If you need to construct a timedelta, "
125- "do it inline as the first argument." ,
126- DeprecationWarning ,
127- stacklevel = 2 ,
128- )
129123 tmp = Unit [minimum_unit .upper ()]
130124 if tmp not in (Unit .SECONDS , Unit .MILLISECONDS , Unit .MICROSECONDS ):
131125 raise ValueError (f"Minimum unit '{ minimum_unit } ' not supported" )
132126 minimum_unit = tmp
133127
134- date , delta = _date_and_delta (value , now = when )
135- if date is None :
136- return value
128+ if isinstance (value , dt .timedelta ):
129+ delta = value
130+ else :
131+ try :
132+ value = int (value )
133+ delta = dt .timedelta (seconds = value )
134+ except (ValueError , TypeError ):
135+ return value
137136
138137 use_months = months
139138
@@ -238,7 +237,7 @@ def naturaltime(
238237 future = date > now
239238
240239 ago = _ ("%s from now" ) if future else _ ("%s ago" )
241- delta = naturaldelta (delta , months , minimum_unit , when = when )
240+ delta = naturaldelta (delta , months , minimum_unit )
242241
243242 if delta == _ ("a moment" ):
244243 return _ ("now" )
0 commit comments