Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct error in humanize() granularities for single seconds #748

Merged
merged 7 commits into from Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions arrow/arrow.py
Expand Up @@ -904,7 +904,12 @@ def humanize(
dt = other.astimezone(self._datetime.tzinfo)

else:
raise TypeError()
raise TypeError(
"Invalid 'other' argument of type '{}'. "
"Argument must be of type None, Arrow, or datetime.".format(
type(other).__name__
)
)

if isinstance(granularity, list) and len(granularity) == 1:
granularity = granularity[0]
Expand Down Expand Up @@ -939,7 +944,8 @@ def humanize(
hours = sign * int(max(delta / 3600, 2))
return locale.describe("hours", hours, only_distance=only_distance)

elif diff < 129600:
# anything less than 48 hours should be 1 day
elif diff < 172800:
Comment on lines -942 to +948
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

return locale.describe("day", sign, only_distance=only_distance)
elif diff < 554400:
days = sign * int(max(delta / 86400, 2))
Expand Down Expand Up @@ -1033,17 +1039,20 @@ def humanize(

if len(timeframes) < len(granularity):
raise AttributeError(
"Invalid level of granularity. Please select between 'second', 'minute', 'hour', 'day', 'week', 'month' or 'year'"
"Invalid level of granularity. "
"Please select between 'second', 'minute', 'hour', 'day', 'week', 'month' or 'year'."
)

for index, (_, delta) in enumerate(timeframes):
if trunc(abs(delta)) != 1:
timeframes[index][0] += "s"
for tf in timeframes:
# Make granularity plural if the delta is not equal to 1
if trunc(abs(tf[1])) != 1:
tf[0] += "s"
return locale.describe_multi(timeframes, only_distance=only_distance)

except KeyError as e:
raise ValueError(
"Humanization of the {} granularity is not currently translated in the '{}' locale. Please consider making a contribution to this locale.".format(
"Humanization of the {} granularity is not currently translated in the '{}' locale. "
"Please consider making a contribution to this locale.".format(
e, locale_name
)
)
Expand Down