Skip to content

format_date needs to be timezone-aware #805

Open

Description

@homeworkprod

I'm using Babel's format_date directly in Flask as Flask-Babel is in the process of removing their wrappers (since 1.5 years, but still).

I also tend to use format_date and format_time in tandem instead of just format_datetime as that gives me more flexibility in formatting (e.g. formats medium and short, respectively, to gain 17.03.2021, 14:09, which isn't possible without a custom–and no longer locale-independent–format).

The issue I stumbled upon is that given UTC date/time 2021-03-17T23:00:00, format_datetime will display the day in time zone "Europe/Berlin" (+01:00, at that date) correctly as 2021-03-18 [time here] while format_date will give 2021-03-17.

Since format_date does not get and consider the time zone, the output is wrong (correct me if I made a mistake somewhere). I would actually go as far as saying that this can be considered a (subtle) bug (okay, I'm being dramatic), or at least a serious limitation.

The reason I'm mentioning Flask-Babel is that it's format_date wrapper indeed does rebase according to the time zone (at least for the time being [no pun intended]). Due to the aforementioned removal of its formatters, Flask-Babel users will probably run into this trap (and might not even learn about it).

My suggestion is to add an optional tzinfo argument to Babel's format_date, so that one can create their own wrapper:

def format_date(*args) -> str:
    return babel.dates.format_date(
        *args, locale=get_locale(), tzinfo=get_timezone()
    )

I also understand the perspective that a date formatter only has to know about dates, not times. However, I think I made a case or two here while an (optional) broader view might make sense.

Thanks for reading! Please let me know what you think. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions