Skip to content

Commit

Permalink
I realized that humanize does not support datetime.time at all,
Browse files Browse the repository at this point in the history
so I removed the ability for _isoparse() to parse into datetime.time.
I edited inline documentation and README.md accordingly.
  • Loading branch information
velle committed Jul 9, 2023
1 parent 67a8a6b commit 6259556
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ python3 -m pip install -e .
'Jun 05 2007'
>>> humanize.naturaldate('2007-06-05T22:00:10+02:00')
'Jun 05 2007'
>>> humanize.naturaltime('22:00:10') # run at 22:00:00
'10 seconds from now'

Relies on the native `fromisoformat` function that exists on date, datetime and time objects.
```
Expand Down
25 changes: 8 additions & 17 deletions src/humanize/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,20 @@ def _now() -> dt.datetime:


def _parseiso(
value: dt.time | dt.date | dt.datetime | float | str,
) -> dt.time | dt.date | dt.datetime | float | str:
"""If string attempts to parse as iso8601 into date, datetime or time.
value: dt.date | dt.datetime | float | str,
) -> dt.date | dt.datetime | float | str:
"""If string attempts to parse as iso8601 date or datetime.
Args:
value (str or any object): String to be parsed.
Returns:
str (str or any): If string, it will attempt parsing as
datetime.date, datetime.datetime or datetime.time, in that order,
and return the result of the first succesful parsing, if any.
str (str or any): If string, it will first attempt parsing with
date.fromisoformat. If that fails it will attempt parsing with
datetime.isoformat.
Parsing is attempted with the iso8601 function for each of the classes.
If `value` is not a string or if all attempts at parsing fail,
If `value` is not a string or both attempts at parsing fail,
`value` is returned as is.
"""
if isinstance(value, str):
try:
Expand All @@ -79,12 +76,6 @@ def _parseiso(
except ValueError:
pass

try:
# catches eg '20:00', '20:00:16.123456', 'T20:00Z'
return dt.time.fromisoformat(value)
except ValueError:
pass

return value


Expand Down Expand Up @@ -261,7 +252,7 @@ def naturaldelta(


def naturaltime(
value: dt.datetime | dt.timedelta | float,
value: dt.datetime | dt.timedelta | float | str,
future: bool = False,
months: bool = True,
minimum_unit: str = "seconds",
Expand Down

0 comments on commit 6259556

Please sign in to comment.