Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions babel/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@ def format_date(
date: datetime.date | None = None,
format: _PredefinedTimeFormat | str = 'medium',
locale: Locale | str | None = None,
*,
tzinfo: datetime.tzinfo | None = None,
) -> str:
"""Return a date formatted according to the given pattern.

Expand All @@ -757,16 +759,26 @@ def format_date(
>>> format_date(d, "EEE, MMM d, ''yy", locale='en')
"Sun, Apr 1, '07"

A datetime can be converted to a different timezone before formatting
its date. A naive datetime is assumed to be in UTC:

>>> from datetime import datetime
>>> format_date(datetime(2021, 3, 17, 23, 30), 'yyyy-MM-dd', locale='en',
... tzinfo=get_timezone('Europe/Berlin'))
'2021-03-18'

:param date: the ``date`` or ``datetime`` object; if `None`, the current
date is used
:param format: one of "full", "long", "medium", or "short", or a custom
date/time pattern
:param locale: a `Locale` object or a locale identifier. Defaults to the system time locale.
:param tzinfo: timezone in which to display a datetime or determine the current
date. Has no effect on a date without a time.
"""
if date is None:
date = datetime.date.today()
date = datetime.datetime.now(tzinfo).date()
elif isinstance(date, datetime.datetime):
date = date.date()
date = _ensure_datetime_tzinfo(date, tzinfo).date()

locale = Locale.parse(locale or LC_TIME)
if format in ('full', 'long', 'medium', 'short'):
Expand Down
2 changes: 1 addition & 1 deletion babel/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def date(
>>> fmt.date(date(2007, 4, 1))
'Apr 1, 2007'
"""
return format_date(date, format, locale=self.locale)
return format_date(date, format, locale=self.locale, tzinfo=self.tzinfo)

def datetime(
self,
Expand Down
21 changes: 21 additions & 0 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,27 @@ def test_format_date():
"Sun, Apr 1, '07")


def test_format_date_in_timezone(timezone_getter):
berlin = timezone_getter('Europe/Berlin')
naive_utc = datetime(2021, 3, 17, 23, 30)
assert dates.format_date(naive_utc, 'yyyy-MM-dd', 'en', tzinfo=berlin) == '2021-03-18'

aware = datetime(2021, 3, 18, 0, 30, tzinfo=FixedOffsetTimezone(120))
assert dates.format_date(aware, 'yyyy-MM-dd', 'en', tzinfo=UTC) == '2021-03-17'
assert dates.format_date(aware, 'yyyy-MM-dd', 'en') == '2021-03-18'


def test_format_date_date_only_in_timezone(timezone_getter):
west = timezone_getter('America/Los_Angeles')
assert dates.format_date(date(2021, 3, 18), 'yyyy-MM-dd', 'en', tzinfo=west) == '2021-03-18'


def test_format_date_today_in_timezone(timezone_getter):
with freeze_time('2021-03-18T00:30:00'):
west = timezone_getter('America/Los_Angeles')
assert dates.format_date(format='yyyy-MM-dd', locale='en', tzinfo=west) == '2021-03-17'


def test_format_datetime(timezone_getter):
dt = datetime(2007, 4, 1, 15, 30)
assert (dates.format_datetime(dt, locale='en_US') ==
Expand Down
5 changes: 5 additions & 0 deletions tests/test_support_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def en_us_format(timezone_getter) -> support.Format:
return support.Format('en_US', tzinfo=timezone_getter('US/Eastern'))


def test_format_date_in_timezone(en_us_format):
when = datetime.datetime(2007, 4, 1, 1, 30)
assert en_us_format.date(when) == 'Mar 31, 2007'


def test_format_datetime(en_us_format):
when = datetime.datetime(2007, 4, 1, 15, 30)
assert en_us_format.datetime(when) == 'Apr 1, 2007, 11:30:00\u202fAM'
Expand Down