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

Disambiguate naturaldate return: only append year if more than ~5 months away #107

Merged
merged 1 commit into from
Feb 24, 2020
Merged

Disambiguate naturaldate return: only append year if more than ~5 months away #107

merged 1 commit into from
Feb 24, 2020

Conversation

hugovk
Copy link
Collaborator

@hugovk hugovk commented Feb 13, 2020

Fixes #26.

Before

Previously, naturaldate appended the year if the input was more than about a year away (>= 365 days).

That led to ambiguity:

>>> import datetime as dt
>>> import humanize
>>> x = dt.datetime.now() - dt.timedelta(3)  # 3 days ago
>>> x
datetime.datetime(2020, 2, 10, 22, 10, 4, 439522)
>>> humanize.naturaldate(x)
'Feb 10'
>>> x = dt.datetime.now() + dt.timedelta(363)  # 363 days forward
>>> x
datetime.datetime(2021, 2, 10, 22, 10, 44, 352492)
>>> humanize.naturaldate(x)
'Feb 10'

And, assuming today is Feb 02 2020, the inputs on the left produced the outputs on the right:

        (dt.date(2019, 2, 2), "Feb 02 2019"),
        (dt.date(2019, 3, 2), "Mar 02"),
        (dt.date(2019, 4, 2), "Apr 02"),
        (dt.date(2019, 5, 2), "May 02"),
        (dt.date(2019, 6, 2), "Jun 02"),
        (dt.date(2019, 7, 2), "Jul 02"),
        (dt.date(2019, 8, 2), "Aug 02"),
        (dt.date(2019, 9, 2), "Sep 02"),
        (dt.date(2019, 10, 2), "Oct 02"),
        (dt.date(2019, 11, 2), "Nov 02"),
        (dt.date(2019, 12, 2), "Dec 02"),
        (dt.date(2020, 1, 2), "Jan 02"),
        (dt.date(2020, 2, 2), "today"),
        (dt.date(2020, 3, 2), "Mar 02"),
        (dt.date(2020, 4, 2), "Apr 02"),
        (dt.date(2020, 5, 2), "May 02"),
        (dt.date(2020, 6, 2), "Jun 02"),
        (dt.date(2020, 7, 2), "Jul 02"),
        (dt.date(2020, 8, 2), "Aug 02"),
        (dt.date(2020, 9, 2), "Sep 02"),
        (dt.date(2020, 10, 2), "Oct 02"),
        (dt.date(2020, 11, 2), "Nov 02"),
        (dt.date(2020, 12, 2), "Dec 02"),
        (dt.date(2021, 1, 2), "Jan 02"),
        (dt.date(2021, 2, 2), "Feb 02 2021"),

After

Only append the year if the input is more than about 5 months away (>= 5 * 30.5 days).

>>> import datetime as dt
>>> import humanize
>>> x = dt.datetime.now() - dt.timedelta(3)  # 3 days ago
>>> x
datetime.datetime(2020, 2, 10, 22, 13, 56, 74705)
>>> humanize.naturaldate(x)
'Feb 10'
>>> x = dt.datetime.now() + dt.timedelta(363)  # 363 days forward
>>> x
datetime.datetime(2021, 2, 10, 22, 14, 5, 816049)
>>> humanize.naturaldate(x)
'Feb 10 2021'
        (dt.date(2019, 2, 2), "Feb 02 2019"),
        (dt.date(2019, 3, 2), "Mar 02 2019"),
        (dt.date(2019, 4, 2), "Apr 02 2019"),
        (dt.date(2019, 5, 2), "May 02 2019"),
        (dt.date(2019, 6, 2), "Jun 02 2019"),
        (dt.date(2019, 7, 2), "Jul 02 2019"),
        (dt.date(2019, 8, 2), "Aug 02 2019"),
        (dt.date(2019, 9, 2), "Sep 02 2019"),
        (dt.date(2019, 10, 2), "Oct 02"),
        (dt.date(2019, 11, 2), "Nov 02"),
        (dt.date(2019, 12, 2), "Dec 02"),
        (dt.date(2020, 1, 2), "Jan 02"),
        (dt.date(2020, 2, 2), "today"),
        (dt.date(2020, 3, 2), "Mar 02"),
        (dt.date(2020, 4, 2), "Apr 02"),
        (dt.date(2020, 5, 2), "May 02"),
        (dt.date(2020, 6, 2), "Jun 02"),
        (dt.date(2020, 7, 2), "Jul 02"),
        (dt.date(2020, 8, 2), "Aug 02 2020"),
        (dt.date(2020, 9, 2), "Sep 02 2020"),
        (dt.date(2020, 10, 2), "Oct 02 2020"),
        (dt.date(2020, 11, 2), "Nov 02 2020"),
        (dt.date(2020, 12, 2), "Dec 02 2020"),
        (dt.date(2021, 1, 2), "Jan 02 2021"),
        (dt.date(2021, 2, 2), "Feb 02 2021"),

@hugovk hugovk added the enhancement New feature or request label Feb 13, 2020
@hugovk hugovk added the changelog: Changed For changes in existing functionality label Feb 19, 2020
@hugovk hugovk merged commit b28d9ad into jmoiron:master Feb 24, 2020
@hugovk hugovk deleted the 26-disambiguate-naturaldate-returns branch February 24, 2020 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Changed For changes in existing functionality enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

naturaldate returns ambiguous values
1 participant