uranusjr commented on a change in pull request #19921:
URL: https://github.com/apache/airflow/pull/19921#discussion_r759944530
##########
File path: airflow/utils/timezone.py
##########
@@ -184,3 +184,36 @@ def coerce_datetime(v: Union[None, dt.datetime, DateTime])
-> Optional[DateTime]
if isinstance(v, DateTime):
return v
return pendulum.instance(v)
+
+
+def td_format(td_object: Union[None, dt.timedelta, float]):
+ """
+ Format a timedelta object or float into a readable string for time
duration.
+ For example timedelta(seconds=3602) would become `1 hour 2 seconds`
+ """
Review comment:
You can use `dateutil` instead:
```python
from dateutil.relativedelta import relativedelta
def td_format(td_object: Union[None, dt.timedelta, float]) -> str:
if not td_object:
return None
if isinstance(td_object, dt.timedelta):
delta = relativedelta() + td_object
else:
delta = relativedelta(seconds=td_object)
delta = delta.normalized() # This does all the conversions for you.
def _format_part(key: str) -> str:
value = int(getattr(delta, key))
if value < 1:
return ""
if value < 2:
key = key[:-1] # Remove 's'.
return f"{value} {key}"
parts = map(_format_part, ("years", "months", "days", "hours",
"minutes", "seconds"))
joined = ", ".join(part for part in parts if part)
if not joined:
return "< 1 second"
return joined
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]