Babel
Babel
The date and time functionality provided by Babel lets you format standard Python
datetime, date and time objects and work with timezones.
Documentation overview If you don’t want to use the locale default formats, you can specify a custom date
API Reference pattern:
Previous: Core
Functionality >>> format_date(d, "EEE, MMM d, ''yy", locale='en')
Next: Languages u"Sun, Apr 1, '07"
Quick search Parameters: date – the date or datetime object; if None, the current date is
used
Go format – one of “full”, “long”, “medium”, or “short”, or a custom
date/time pattern
locale – a Locale object or a locale identifier
babel.dates.format_time(time=None, format='medium', tzinfo=None,
locale=default_locale('LC_TIME'))
Return a time formatted according to the given pattern.
If you don’t want to use the locale default formats, you can specify a custom time
pattern:
For any pattern requiring the display of the time-zone a timezone has to be
specified explicitly:
As that example shows, when this function gets passed a datetime.datetime value,
the actual time in the formatted string is adjusted to the timezone specified by the
tzinfo parameter. If the datetime is “naive” (i.e. it has no associated timezone
information), it is assumed to be in UTC.
Parameters: time – the time or datetime object; if None, the current time in
UTC is used
format – one of “full”, “long”, “medium”, or “short”, or a custom
date/time pattern
tzinfo – the time-zone to apply to the time for display
locale – a Locale object or a locale identifier
babel.dates.format_timedelta(delta, granularity='second', threshold=.85,
add_direction=False, format='long', locale=default_locale('LC_TIME'))
Return a time delta according to the rules of the given locale.
The granularity parameter can be provided to alter the lowest unit presented,
which defaults to a second.
The threshold parameter can be used to determine at which value the presentation
switches to the next higher unit. A higher threshold factor means the presentation
will switch later. For example:
>>> format_timedelta(timedelta(hours=23), threshold=0.9, locale='en_US')
u'1 day'
>>> format_timedelta(timedelta(hours=23), threshold=1.1, locale='en_US')
u'23 hours'
In addition directional information can be provided that informs the user if the
date is in the past or in the future:
The format parameter controls how compact or wide the presentation is:
The skeletons are defined in the CLDR data and provide more flexibility than the
simple short/long/medium formats, but are a bit harder to use. The are defined
using the date/time symbols without order or punctuation and map to a suitable
format for the given locale.
If the start instant equals the end instant, the interval is formatted like the instant.
Timezone Functionality
babel.dates.get_timezone(zone: str | datetime.tzinfo | None = None) → tzinfo
Looks up a timezone by name and returns it. The timezone object returned comes
from pytz or zoneinfo, whichever is available. It corresponds to the tzinfo interface
and can be used with all of the functions of Babel that operate with dates.
Parameters: zone – the name of the timezone to look up. If a timezone object
itself is passed in, it’s returned unchanged.
babel.dates.get_timezone_gmt(datetime: _Instant = None, width: Literal['long',
'short', 'iso8601', 'iso8601_short'] = 'long', locale: Locale | str | None =
'en_US_POSIX', return_z: bool = False) → str
Return the timezone associated with the given datetime object formatted as string
indicating the offset from GMT.
The long format depends on the locale, for example in France the acronym UTC
string is used instead of GMT:
Parameters: datetime – the datetime object; if None, the current date and
time in UTC is used
width – either “long” or “short” or “iso8601” or “iso8601_short”
locale – the Locale object, or a locale string
return_z – True or False; Function returns indicator “Z” when
local time offset is 0
babel.dates.get_timezone_location(dt_or_tzinfo: _DtOrTzinfo = None, locale:
Locale | str | None = 'en_US_POSIX', return_city: bool = False) → str
Return a representation of the given timezone using “location format”.
The result depends on both the local display name of the country and the city
associated with the time zone:
>>> tz = get_timezone('America/St_Johns')
>>> print(get_timezone_location(tz, locale='de_DE'))
Kanada (St. John’s) (Ortszeit)
>>> print(get_timezone_location(tz, locale='en'))
Canada (St. John’s) Time
>>> print(get_timezone_location(tz, locale='en', return_city=True))
St. John’s
>>> tz = get_timezone('America/Mexico_City')
>>> get_timezone_location(tz, locale='de_DE')
u'Mexiko (Mexiko-Stadt) (Ortszeit)'
If the timezone is associated with a country that uses only a single timezone, just
the localized country name is returned:
>>> tz = get_timezone('Europe/Berlin')
>>> get_timezone_name(tz, locale='de_DE')
u'Mitteleurop\xe4ische Zeit'
If this function gets passed only a tzinfo object and no concrete datetime, the
returned display name is independent of daylight savings time. This can be used
for example for selecting timezones, or to set the time of events that recur across
DST changes:
>>> tz = get_timezone('America/Los_Angeles')
>>> get_timezone_name(tz, locale='en_US')
u'Pacific Time'
>>> get_timezone_name(tz, 'short', locale='en_US')
u'PT'
If no localized display name for the timezone is available, and the timezone is
associated with a country that uses only a single timezone, the name of that
country is returned, formatted according to the locale:
>>> tz = get_timezone('Europe/Berlin')
>>> get_timezone_name(tz, locale='de_DE')
u'Mitteleurop\xe4ische Zeit'
>>> get_timezone_name(tz, locale='pt_BR')
u'Hor\xe1rio da Europa Central'
On the other hand, if the country uses multiple timezones, the city is also included
in the representation:
>>> tz = get_timezone('America/St_Johns')
>>> get_timezone_name(tz, locale='de_DE')
u'Neufundland-Zeit'
Note that short format is currently not supported for all timezones and all locales.
This is partially because not every timezone has a short code in every locale. In
that case it currently falls back to the long format.
For more information see LDML Appendix J: Time Zone Display Names
Data Access
babel.dates.get_period_names(width: Literal['abbreviated', 'narrow', 'wide'] =
'wide', context: _Context = 'stand-alone', locale: Locale | str | None =
'en_US_POSIX') → LocaleDataDict
Return the names for day periods (AM/PM) used by the locale.
>>> get_period_names(locale='en_US')['am']
u'AM'
>>> get_date_format(locale='en_US')
<DateTimePattern u'MMM d, y'>
>>> get_date_format('full', locale='de_DE')
<DateTimePattern u'EEEE, d. MMMM y'>
>>> get_datetime_format(locale='en_US')
u'{1}, {0}'
>>> get_time_format(locale='en_US')
<DateTimePattern u'h:mm:ss a'>
>>> get_time_format('full', locale='de_DE')
<DateTimePattern u'HH:mm:ss zzzz'>
Basic Parsing
babel.dates.parse_date(string: str, locale: Locale | str | None =
'en_US_POSIX', format: _PredefinedTimeFormat = 'medium') → datetime.date
Parse a date from a string.
This function first tries to interpret the string as ISO-8601 date format, then uses
the date format for the locale as a hint to determine the order in which the date
fields appear in the string.
This function uses the time format for the locale as a hint to determine the order
in which the time fields appear in the string.
>>> parse_pattern("MMMMd").format
u'%(MMMM)s%(d)s'
>>> parse_pattern("MMM d, yyyy").format
u'%(MMM)s %(d)s, %(yyyy)s'
An actual single quote can be used by using two adjacent single quote characters:
틱톡 캡차 해결사: 단 두 줄의 코드로 모든 틱톡
캡차를 우회하세요. 오늘 바로 차단 해제!
Ad by EthicalAds · ℹ️