Skip to content

Support remaining time in format_timedelta #246

Description

@homeworkprod

Babel supports positive and negative timedeltas. From their code (babel/dates.py):

>>> format_timedelta(timedelta(hours=1), add_direction=True, locale='en')
'in 1 hour'
>>> format_timedelta(timedelta(hours=-1), add_direction=True, locale='en')
'1 hour ago'

Flask-Babel, however, only supports elapsed time. It subtracts the timedelta value from the current time:

if isinstance(datetime_or_timedelta, datetime):
    datetime_or_timedelta = datetime.utcnow() - datetime_or_timedelta

It would be great to properly handle dates in the future as well.

How about something like this?

if isinstance(datetime_or_timedelta, datetime):
    now = datetime.utcnow()
    if datetime_or_timedelta > now:
        datetime_or_timedelta = datetime_or_timedelta - now
    else:
        datetime_or_timedelta = now - datetime_or_timedelta

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