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

naturaldate returns ambiguous values #26

Closed
dingens opened this issue Jan 18, 2015 · 3 comments · Fixed by #107
Closed

naturaldate returns ambiguous values #26

dingens opened this issue Jan 18, 2015 · 3 comments · Fixed by #107

Comments

@dingens
Copy link

dingens commented Jan 18, 2015

>>> import humanize
>>> from datetime import datetime, timedelta
>>> humanize.naturaldate(datetime.now()-timedelta(3))
'Jan 15'
>>> humanize.naturaldate(datetime.now()+timedelta(362))
'Jan 15'

I'd suggest to add the year for all dates that are more than about five months away. This would lead to the following output:

date naturaldate (executed in Jan 2015)
2014-08-20 Aug 20
2015-08-20 Aug 20 2015
2014-06-20 Jun 20 2014
2015-06-20 Jun 20

(Even when only used for dates that are necessarily in the past, like "comment posted on ...", the current behaviour is not perfect: If I read "Jan 19" on Jan 18, 2015, I won't not assume it means Jan 19, 2014.)

@ncouture
Copy link

Following the same logic I would like to suggest deprecating month name abbreviations that are the current default.

This can be implemented with backward compatibility option that would be to include an argument that allows passing the desired format string when calling the function:

humanize.naturaldate(format="%b %d %Y)

The default should be replaced by "%B %d %Y".

@dingens
Copy link
Author

dingens commented Jan 26, 2016

I don't think this has anything to do with this bug, does it?

(btw, I spotted a little typo in my text above. fixed)

@hugovk
Copy link
Collaborator

hugovk commented Feb 13, 2020

@dingens @ludstuen90 @tbille Right now in humanize 1.0, assuming today is Feb 02 2020, the inputs on the left produce 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"),

Would something like PR #107 be better?

        (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"),

It uses 5 * 30.5 as "about 5 months" to decide whether to append the year.

So in this case, gives 4 months before without a year (Oct, Nov, Dev, Jan), and 5 months after without a year (Mar, Apr, May, Jun, Jul).

Would that good enough or should it be more accurate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants