Skip to content

Missing option to display years and months when years > 1 #108

@adonig

Description

@adonig

What did you do?

In [18]: humanize.naturaldelta(timedelta(days=4.6*365), months=True)
Out[18]: '4 years'

In [19]: humanize.naturaldelta(timedelta(days=4.6*365), months=False)
Out[19]: '4 years'

What did you expect to happen?

In [18]: humanize.naturaldelta(timedelta(days=4.6*365), months=True)
Out[18]: '4 years, 7 months'

In [19]: humanize.naturaldelta(timedelta(days=4.6*365), months=False)
Out[19]: '4 years'

What actually happened?

see above

What versions are you using?

  • OS: x86_64 GNU/Linux
  • Python: 3.11.3
  • Humanize: 4.6.0

How to fix?

    # Excerpt from humanize/time.py 

    # ... more code here
    elif years == 1:
        if not num_months and not days:
            return _("a year")

        if not num_months:
            return _ngettext("1 year, %d day", "1 year, %d days", days) % days

        if use_months:
            if num_months == 1:
                return _("1 year, 1 month")

            return (
                _ngettext("1 year, %d month", "1 year, %d months", num_months)
                % num_months
            )

        return _ngettext("1 year, %d day", "1 year, %d days", days) % days

    # TODO: Here we should check whether use_months is true and format accordingly.
    return _ngettext("%d year", "%d years", years).replace("%d", "%s") % intcomma(years)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions