Skip to content

dehumanize() silently ignores negative time values, returning wrong results #1278

@amanv0007

Description

@amanv0007

What happened

While using dehumanize() to parse user-provided relative time strings, I noticed that negative values like "in -1 hours" don't raise an error - they silently produce the same result as "in 1 hours". The minus sign is dropped without any warning.

This is because the internal number-extraction regex \d+ only matches unsigned digits, so the - in -1 is never captured. The result is a quietly wrong datetime that's in the opposite direction from what a user might expect.

How to reproduce

import arrow

now = arrow.now()

# These two return the exact same result (+1 hour):
result1 = now.dehumanize("in 1 hours")
result2 = now.dehumanize("in -1 hours")

print((result1 - now).total_seconds())  # 3600.0
print((result2 - now).total_seconds())  # 3600.0  <- should not be the same!

Similarly, "in -2 days" silently becomes +2 days, and "-3 minutes ago" silently becomes -3 minutes (dropping the sign).

Expected behavior

dehumanize() should raise a ValueError when the input contains a negative number, since humanized time strings use "ago"/"in" for direction - not arithmetic signs. Silently swallowing the sign leads to wrong results that are hard to debug.

Version

arrow 1.4.0
Python 3.12
Windows 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions