Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions arrow/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,17 @@ def dehumanize(self, input_string: str, locale: str = "en_us") -> "Arrow":

current_time = self.fromdatetime(self._datetime)

# Humanized strings express direction with words ("ago"/"in"), never
# with an arithmetic sign, so ``humanize`` itself never emits one. A
# minus sign attached to a number (e.g. "in -1 hours") would otherwise
# be silently dropped by the unsigned ``\d+`` extraction below and yield
# a result in the opposite direction from what was written, so reject it.
if re.search(r"-\d", input_string):
raise ValueError(
f"Invalid input string: {input_string!r}. Negative numbers are "
"not supported; use 'ago' or 'in' to indicate direction."
)

# Create an object containing the relative time info
time_object_info = dict.fromkeys(
["seconds", "minutes", "hours", "days", "weeks", "months", "years"], 0
Expand Down
9 changes: 9 additions & 0 deletions tests/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2868,6 +2868,15 @@ def test_normalized_locale(self):
assert arw.dehumanize(second_ago_string, locale="zh_hk") == second_ago
assert arw.dehumanize(second_future_string, locale="zh_hk") == second_future

# Ensures a negative number is rejected rather than silently dropping the
# sign and producing a result in the opposite direction (see gh-1278).
def test_negative_number(self):
arw = arrow.Arrow(2000, 6, 18, 5, 55, 0)

for input_string in ("in -1 hours", "in -2 days", "-3 minutes ago"):
with pytest.raises(ValueError):
arw.dehumanize(input_string)

# Ensures relative units are required in string
def test_require_relative_unit(self, locale_list_no_weeks: List[str]):
for lang in locale_list_no_weeks:
Expand Down
Loading