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

Add support for timezone-aware datetimes to format_timedelta #233

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions flask_babel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import os
from dataclasses import dataclass
from types import SimpleNamespace
from datetime import datetime
from datetime import datetime, timezone as stdlib_timezone
from contextlib import contextmanager
from typing import List, Callable, Optional, Union

Expand Down Expand Up @@ -453,7 +453,16 @@ def format_timedelta(datetime_or_timedelta, granularity: str = 'second',
named `timedeltaformat`.
"""
if isinstance(datetime_or_timedelta, datetime):
datetime_or_timedelta = datetime.utcnow() - datetime_or_timedelta
is_aware = (
datetime_or_timedelta.tzinfo is not None
and datetime_or_timedelta.tzinfo.utcoffset(datetime_or_timedelta)
is not None
)
if is_aware:
now = datetime.now(stdlib_timezone.utc)
else:
now = datetime.utcnow()
datetime_or_timedelta = now - datetime_or_timedelta
return dates.format_timedelta(
datetime_or_timedelta,
granularity,
Expand Down
29 changes: 28 additions & 1 deletion tests/test_date_formatting.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,57 @@
from datetime import datetime, timedelta
import pytz
from datetime import datetime, timedelta, timezone

import flask

import flask_babel as babel
from flask_babel import get_babel
import time_machine


def test_basics():
london = pytz.timezone("Europe/London")
app = flask.Flask(__name__)
babel.Babel(app)
d = datetime(2010, 4, 12, 13, 46)
d_tzinfo_non_dst = london.localize(datetime(2010, 1, 16, 23, 23)) # non-DST
d_tzinfo_dst = london.localize(datetime(2010, 8, 12, 5, 13)) # DST
delta = timedelta(days=6)

with app.test_request_context():
assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00\u202fPM'
assert babel.format_datetime(d_tzinfo_non_dst) == \
"Jan 16, 2010, 11:23:00\u202fPM"
assert babel.format_datetime(d_tzinfo_dst) == \
"Aug 12, 2010, 4:13:00\u202fAM"
assert babel.format_date(d) == 'Apr 12, 2010'
assert babel.format_time(d) == '1:46:00\u202fPM'
assert babel.format_timedelta(delta) == '1 week'
assert babel.format_timedelta(delta, threshold=1) == '6 days'

with time_machine.travel(datetime(2010, 4, 12, 14, tzinfo=timezone.utc)):
assert babel.format_timedelta(d) == "14 minutes"
with time_machine.travel(datetime(2010, 1, 16, 23, 30, tzinfo=timezone.utc)):
assert babel.format_timedelta(d_tzinfo_non_dst) == "7 minutes"
with time_machine.travel(datetime(2010, 8, 12, 4, 30, tzinfo=timezone.utc)):
assert babel.format_timedelta(d_tzinfo_dst) == "17 minutes"

with app.test_request_context():
get_babel(app).default_timezone = 'Europe/Vienna'
assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00\u202fPM'
assert babel.format_datetime(d_tzinfo_non_dst) == \
"Jan 17, 2010, 12:23:00\u202fAM"
assert babel.format_datetime(d_tzinfo_dst) == \
"Aug 12, 2010, 6:13:00\u202fAM"
assert babel.format_date(d) == 'Apr 12, 2010'
assert babel.format_time(d) == '3:46:00\u202fPM'

with time_machine.travel(datetime(2010, 4, 12, 14, tzinfo=timezone.utc)):
assert babel.format_timedelta(d) == "14 minutes"
with time_machine.travel(datetime(2010, 1, 16, 23, 30, tzinfo=timezone.utc)):
assert babel.format_timedelta(d_tzinfo_non_dst) == "7 minutes"
with time_machine.travel(datetime(2010, 8, 12, 4, 30, tzinfo=timezone.utc)):
assert babel.format_timedelta(d_tzinfo_dst) == "17 minutes"

with app.test_request_context():
get_babel(app).default_locale = 'de_DE'
assert babel.format_datetime(d, 'long') == \
Expand Down