|
|
datetime.datetime | _localize (datetime.tzinfo tz, datetime.datetime dt) |
| |
| tuple[datetime.datetime|None, datetime.tzinfo] | _get_dt_and_tzinfo (_DtOrTzinfo dt_or_tzinfo) |
| |
| str | _get_tz_name (_DtOrTzinfo dt_or_tzinfo) |
| |
| datetime.datetime | _get_datetime (_Instant instant) |
| |
| datetime.datetime | _ensure_datetime_tzinfo (datetime.datetime dt, datetime.tzinfo|None tzinfo=None) |
| |
| datetime.time | _get_time (datetime.time|datetime.datetime|None time, datetime.tzinfo|None tzinfo=None) |
| |
| datetime.tzinfo | get_timezone (str|datetime.tzinfo|None zone=None) |
| |
| LocaleDataDict | get_period_names (Literal['abbreviated', 'narrow', 'wide'] width='wide', _Context context='stand-alone', Locale|str|None locale=None) |
| |
| LocaleDataDict | get_day_names (Literal['abbreviated', 'narrow', 'short', 'wide'] width='wide', _Context context='format', Locale|str|None locale=None) |
| |
| LocaleDataDict | get_month_names (Literal['abbreviated', 'narrow', 'wide'] width='wide', _Context context='format', Locale|str|None locale=None) |
| |
| LocaleDataDict | get_quarter_names (Literal['abbreviated', 'narrow', 'wide'] width='wide', _Context context='format', Locale|str|None locale=None) |
| |
| LocaleDataDict | get_era_names (Literal['abbreviated', 'narrow', 'wide'] width='wide', Locale|str|None locale=None) |
| |
| DateTimePattern | get_date_format (_PredefinedTimeFormat format='medium', Locale|str|None locale=None) |
| |
| DateTimePattern | get_datetime_format (_PredefinedTimeFormat format='medium', Locale|str|None locale=None) |
| |
| DateTimePattern | get_time_format (_PredefinedTimeFormat format='medium', Locale|str|None locale=None) |
| |
| str | get_timezone_gmt (_Instant datetime=None, Literal['long', 'short', 'iso8601', 'iso8601_short'] width='long', Locale|str|None locale=None, bool return_z=False) |
| |
| str | get_timezone_location (_DtOrTzinfo dt_or_tzinfo=None, Locale|str|None locale=None, bool return_city=False) |
| |
| str | get_timezone_name (_DtOrTzinfo dt_or_tzinfo=None, Literal['long', 'short'] width='long', bool uncommon=False, Locale|str|None locale=None, Literal['generic', 'daylight', 'standard']|None zone_variant=None, bool return_zone=False) |
| |
| str | format_date (datetime.date|None date=None, _PredefinedTimeFormat|str format='medium', Locale|str|None locale=None) |
| |
| str | format_datetime (_Instant datetime=None, _PredefinedTimeFormat|str format='medium', datetime.tzinfo|None tzinfo=None, Locale|str|None locale=None) |
| |
| str | format_time (datetime.time|datetime.datetime|float|None time=None, _PredefinedTimeFormat|str format='medium', datetime.tzinfo|None tzinfo=None, Locale|str|None locale=None) |
| |
| str | format_skeleton (str skeleton, _Instant datetime=None, datetime.tzinfo|None tzinfo=None, bool fuzzy=True, Locale|str|None locale=None) |
| |
| str | format_timedelta (datetime.timedelta|int delta, Literal['year', 'month', 'week', 'day', 'hour', 'minute', 'second'] granularity='second', float threshold=.85, bool add_direction=False, Literal['narrow', 'short', 'medium', 'long'] format='long', Locale|str|None locale=None) |
| |
|
str | _format_fallback_interval (_Instant start, _Instant end, str|None skeleton, datetime.tzinfo|None tzinfo, Locale locale) |
| |
| str | format_interval (_Instant start, _Instant end, str|None skeleton=None, datetime.tzinfo|None tzinfo=None, bool fuzzy=True, Locale|str|None locale=None) |
| |
| str | get_period_id (_Instant time, datetime.tzinfo|None tzinfo=None, Literal['selection']|None type=None, Locale|str|None locale=None) |
| |
| datetime.date | parse_date (str string, Locale|str|None locale=None, _PredefinedTimeFormat|str format='medium') |
| |
| datetime.time | parse_time (str string, Locale|str|None locale=None, _PredefinedTimeFormat|str format='medium') |
| |
| DateTimePattern | parse_pattern (str|DateTimePattern pattern) |
| |
|
DateTimePattern | _cached_parse_pattern (str pattern) |
| |
| list[tuple[str, str|tuple[str, int]]] | tokenize_pattern (str pattern) |
| |
| str | untokenize_pattern (Iterable[tuple[str, str|tuple[str, int]]] tokens) |
| |
| list[str] | split_interval_pattern (str pattern) |
| |
| str|None | match_skeleton (str skeleton, Iterable[str] options, bool allow_different_fields=False) |
| |
babel.dates
~~~~~~~~~~~
Locale dependent formatting and parsing of dates and times.
The default locale for the functions in this module is determined by the
following environment variables, in that order:
* ``LC_TIME``,
* ``LC_ALL``, and
* ``LANG``
:copyright: (c) 2013-2025 by the Babel Team.
:license: BSD, see LICENSE for more details.
| datetime.datetime babel.dates._get_datetime |
( |
_Instant |
instant | ) |
|
|
protected |
Get a datetime out of an "instant" (date, time, datetime, number).
.. warning:: The return values of this function may depend on the system clock.
If the instant is None, the current moment is used.
If the instant is a time, it's augmented with today's date.
Dates are converted to naive datetimes with midnight as the time component.
>>> from datetime import date, datetime
>>> _get_datetime(date(2015, 1, 1))
datetime.datetime(2015, 1, 1, 0, 0)
UNIX timestamps are converted to datetimes.
>>> _get_datetime(1400000000)
datetime.datetime(2014, 5, 13, 16, 53, 20)
Other values are passed through as-is.
>>> x = datetime(2015, 1, 1)
>>> _get_datetime(x) is x
True
:param instant: date, time, datetime, integer, float or None
:type instant: date|time|datetime|int|float|None
:return: a datetime
:rtype: datetime
| str babel.dates.format_date |
( |
datetime.date | None |
date = None, |
|
|
_PredefinedTimeFormat | str |
format = 'medium', |
|
|
Locale | str | None |
locale = None |
|
) |
| |
Return a date formatted according to the given pattern.
>>> from datetime import date
>>> d = date(2007, 4, 1)
>>> format_date(d, locale='en_US')
u'Apr 1, 2007'
>>> format_date(d, format='full', locale='de_DE')
u'Sonntag, 1. April 2007'
If you don't want to use the locale default formats, you can specify a
custom date pattern:
>>> format_date(d, "EEE, MMM d, ''yy", locale='en')
u"Sun, Apr 1, '07"
: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.
| str babel.dates.format_datetime |
( |
_Instant |
datetime = None, |
|
|
_PredefinedTimeFormat | str |
format = 'medium', |
|
|
datetime.tzinfo | None |
tzinfo = None, |
|
|
Locale | str | None |
locale = None |
|
) |
| |
Return a date formatted according to the given pattern.
>>> from datetime import datetime
>>> dt = datetime(2007, 4, 1, 15, 30)
>>> format_datetime(dt, locale='en_US')
u'Apr 1, 2007, 3:30:00\u202fPM'
For any pattern requiring the display of the timezone:
>>> format_datetime(dt, 'full', tzinfo=get_timezone('Europe/Paris'),
... locale='fr_FR')
'dimanche 1 avril 2007, 17:30:00 heure d’été d’Europe centrale'
>>> format_datetime(dt, "yyyy.MM.dd G 'at' HH:mm:ss zzz",
... tzinfo=get_timezone('US/Eastern'), locale='en')
u'2007.04.01 AD at 11:30:00 EDT'
:param datetime: the `datetime` object; if `None`, the current date and
time is used
:param format: one of "full", "long", "medium", or "short", or a custom
date/time pattern
:param tzinfo: the timezone to apply to the time for display
:param locale: a `Locale` object or a locale identifier. Defaults to the system time locale.
| str babel.dates.format_interval |
( |
_Instant |
start, |
|
|
_Instant |
end, |
|
|
str | None |
skeleton = None, |
|
|
datetime.tzinfo | None |
tzinfo = None, |
|
|
bool |
fuzzy = True, |
|
|
Locale | str | None |
locale = None |
|
) |
| |
Format an interval between two instants according to the locale's rules.
>>> from datetime import date, time
>>> format_interval(date(2016, 1, 15), date(2016, 1, 17), "yMd", locale="fi")
u'15.\u201317.1.2016'
>>> format_interval(time(12, 12), time(16, 16), "Hm", locale="en_GB")
'12:12\u201316:16'
>>> format_interval(time(5, 12), time(16, 16), "hm", locale="en_US")
'5:12\u202fAM\u2009–\u20094:16\u202fPM'
>>> format_interval(time(16, 18), time(16, 24), "Hm", locale="it")
'16:18\u201316:24'
If the start instant equals the end instant, the interval is formatted like the instant.
>>> format_interval(time(16, 18), time(16, 18), "Hm", locale="it")
'16:18'
Unknown skeletons fall back to "default" formatting.
>>> format_interval(date(2015, 1, 1), date(2017, 1, 1), "wzq", locale="ja")
'2015/01/01\uff5e2017/01/01'
>>> format_interval(time(16, 18), time(16, 24), "xxx", locale="ja")
'16:18:00\uff5e16:24:00'
>>> format_interval(date(2016, 1, 15), date(2016, 1, 17), "xxx", locale="de")
'15.01.2016\u2009–\u200917.01.2016'
:param start: First instant (datetime/date/time)
:param end: Second instant (datetime/date/time)
:param skeleton: The "skeleton format" to use for formatting.
:param tzinfo: tzinfo to use (if none is already attached)
:param fuzzy: If the skeleton is not found, allow choosing a skeleton that's
close enough to it.
:param locale: A locale object or identifier. Defaults to the system time locale.
:return: Formatted interval
| str babel.dates.format_skeleton |
( |
str |
skeleton, |
|
|
_Instant |
datetime = None, |
|
|
datetime.tzinfo | None |
tzinfo = None, |
|
|
bool |
fuzzy = True, |
|
|
Locale | str | None |
locale = None |
|
) |
| |
Return a time and/or date formatted according to the given pattern.
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.
>>> from datetime import datetime
>>> t = datetime(2007, 4, 1, 15, 30)
>>> format_skeleton('MMMEd', t, locale='fr')
u'dim. 1 avr.'
>>> format_skeleton('MMMEd', t, locale='en')
u'Sun, Apr 1'
>>> format_skeleton('yMMd', t, locale='fi') # yMMd is not in the Finnish locale; yMd gets used
u'1.4.2007'
>>> format_skeleton('yMMd', t, fuzzy=False, locale='fi') # yMMd is not in the Finnish locale, an error is thrown
Traceback (most recent call last):
...
KeyError: yMMd
>>> format_skeleton('GH', t, fuzzy=True, locale='fi_FI') # GH is not in the Finnish locale and there is no close match, an error is thrown
Traceback (most recent call last):
...
KeyError: None
After the skeleton is resolved to a pattern `format_datetime` is called so
all timezone processing etc is the same as for that.
:param skeleton: A date time skeleton as defined in the cldr data.
:param datetime: the ``time`` or ``datetime`` object; if `None`, the current
time in UTC is used
:param tzinfo: the time-zone to apply to the time for display
:param fuzzy: If the skeleton is not found, allow choosing a skeleton that's
close enough to it. If there is no close match, a `KeyError`
is thrown.
:param locale: a `Locale` object or a locale identifier. Defaults to the system time locale.
| str babel.dates.format_time |
( |
datetime.time | datetime.datetime | float | None |
time = None, |
|
|
_PredefinedTimeFormat | str |
format = 'medium', |
|
|
datetime.tzinfo | None |
tzinfo = None, |
|
|
Locale | str | None |
locale = None |
|
) |
| |
Return a time formatted according to the given pattern.
>>> from datetime import datetime, time
>>> t = time(15, 30)
>>> format_time(t, locale='en_US')
u'3:30:00\u202fPM'
>>> format_time(t, format='short', locale='de_DE')
u'15:30'
If you don't want to use the locale default formats, you can specify a
custom time pattern:
>>> format_time(t, "hh 'o''clock' a", locale='en')
u"03 o'clock PM"
For any pattern requiring the display of the time-zone a
timezone has to be specified explicitly:
>>> t = datetime(2007, 4, 1, 15, 30)
>>> tzinfo = get_timezone('Europe/Paris')
>>> t = _localize(tzinfo, t)
>>> format_time(t, format='full', tzinfo=tzinfo, locale='fr_FR')
'15:30:00 heure d’été d’Europe centrale'
>>> format_time(t, "hh 'o''clock' a, zzzz", tzinfo=get_timezone('US/Eastern'),
... locale='en')
u"09 o'clock AM, Eastern Daylight Time"
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.
These timezone calculations are **not** performed if the value is of type
``datetime.time``, as without date information there's no way to determine
what a given time would translate to in a different timezone without
information about whether daylight savings time is in effect or not. This
means that time values are left as-is, and the value of the `tzinfo`
parameter is only used to display the timezone name if needed:
>>> t = time(15, 30)
>>> format_time(t, format='full', tzinfo=get_timezone('Europe/Paris'),
... locale='fr_FR') # doctest: +SKIP
u'15:30:00 heure normale d\u2019Europe centrale'
>>> format_time(t, format='full', tzinfo=get_timezone('US/Eastern'),
... locale='en_US') # doctest: +SKIP
u'3:30:00\u202fPM Eastern Standard Time'
:param time: the ``time`` or ``datetime`` object; if `None`, the current
time in UTC is used
:param format: one of "full", "long", "medium", or "short", or a custom
date/time pattern
:param tzinfo: the time-zone to apply to the time for display
:param locale: a `Locale` object or a locale identifier. Defaults to the system time locale.
| str babel.dates.format_timedelta |
( |
datetime.timedelta | int |
delta, |
|
|
Literal['year', 'month', 'week', 'day', 'hour', 'minute', 'second'] |
granularity = 'second', |
|
|
float |
threshold = .85, |
|
|
bool |
add_direction = False, |
|
|
Literal['narrow', 'short', 'medium', 'long'] |
format = 'long', |
|
|
Locale | str | None |
locale = None |
|
) |
| |
Return a time delta according to the rules of the given locale.
>>> from datetime import timedelta
>>> format_timedelta(timedelta(weeks=12), locale='en_US')
u'3 months'
>>> format_timedelta(timedelta(seconds=1), locale='es')
u'1 segundo'
The granularity parameter can be provided to alter the lowest unit
presented, which defaults to a second.
>>> format_timedelta(timedelta(hours=3), granularity='day', locale='en_US')
u'1 day'
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:
>>> format_timedelta(timedelta(hours=1), add_direction=True, locale='en')
u'in 1 hour'
>>> format_timedelta(timedelta(hours=-1), add_direction=True, locale='en')
u'1 hour ago'
The format parameter controls how compact or wide the presentation is:
>>> format_timedelta(timedelta(hours=3), format='short', locale='en')
u'3 hr'
>>> format_timedelta(timedelta(hours=3), format='narrow', locale='en')
u'3h'
:param delta: a ``timedelta`` object representing the time difference to
format, or the delta in seconds as an `int` value
:param granularity: determines the smallest unit that should be displayed,
the value can be one of "year", "month", "week", "day",
"hour", "minute" or "second"
:param threshold: factor that determines at which point the presentation
switches to the next higher unit
:param add_direction: if this flag is set to `True` the return value will
include directional information. For instance a
positive timedelta will include the information about
it being in the future, a negative will be information
about the value being in the past.
:param format: the format, can be "narrow", "short" or "long". (
"medium" is deprecated, currently converted to "long" to
maintain compatibility)
:param locale: a `Locale` object or a locale identifier. Defaults to the system time locale.
| str babel.dates.get_timezone_gmt |
( |
_Instant |
datetime = None, |
|
|
Literal['long', 'short', 'iso8601', 'iso8601_short'] |
width = 'long', |
|
|
Locale | str | None |
locale = None, |
|
|
bool |
return_z = False |
|
) |
| |
Return the timezone associated with the given `datetime` object formatted
as string indicating the offset from GMT.
>>> from datetime import datetime
>>> dt = datetime(2007, 4, 1, 15, 30)
>>> get_timezone_gmt(dt, locale='en')
u'GMT+00:00'
>>> get_timezone_gmt(dt, locale='en', return_z=True)
'Z'
>>> get_timezone_gmt(dt, locale='en', width='iso8601_short')
u'+00'
>>> tz = get_timezone('America/Los_Angeles')
>>> dt = _localize(tz, datetime(2007, 4, 1, 15, 30))
>>> get_timezone_gmt(dt, locale='en')
u'GMT-07:00'
>>> get_timezone_gmt(dt, 'short', locale='en')
u'-0700'
>>> get_timezone_gmt(dt, locale='en', width='iso8601_short')
u'-07'
The long format depends on the locale, for example in France the acronym
UTC string is used instead of GMT:
>>> get_timezone_gmt(dt, 'long', locale='fr_FR')
u'UTC-07:00'
.. versionadded:: 0.9
:param datetime: the ``datetime`` object; if `None`, the current date and
time in UTC is used
:param width: either "long" or "short" or "iso8601" or "iso8601_short"
:param locale: the `Locale` object, or a locale string. Defaults to the system time locale.
:param return_z: True or False; Function returns indicator "Z"
when local time offset is 0
| str babel.dates.get_timezone_name |
( |
_DtOrTzinfo |
dt_or_tzinfo = None, |
|
|
Literal['long', 'short'] |
width = 'long', |
|
|
bool |
uncommon = False, |
|
|
Locale | str | None |
locale = None, |
|
|
Literal['generic', 'daylight', 'standard'] | None |
zone_variant = None, |
|
|
bool |
return_zone = False |
|
) |
| |
Return the localized display name for the given timezone. The timezone
may be specified using a ``datetime`` or `tzinfo` object.
>>> from datetime import time
>>> dt = time(15, 30, tzinfo=get_timezone('America/Los_Angeles'))
>>> get_timezone_name(dt, locale='en_US') # doctest: +SKIP
u'Pacific Standard Time'
>>> get_timezone_name(dt, locale='en_US', return_zone=True)
'America/Los_Angeles'
>>> get_timezone_name(dt, width='short', locale='en_US') # doctest: +SKIP
u'PST'
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
<https://www.unicode.org/reports/tr35/#Time_Zone_Fallback>`_
.. versionadded:: 0.9
.. versionchanged:: 1.0
Added `zone_variant` support.
:param dt_or_tzinfo: the ``datetime`` or ``tzinfo`` object that determines
the timezone; if a ``tzinfo`` object is used, the
resulting display name will be generic, i.e.
independent of daylight savings time; if `None`, the
current date in UTC is assumed
:param width: either "long" or "short"
:param uncommon: deprecated and ignored
:param zone_variant: defines the zone variation to return. By default the
variation is defined from the datetime object
passed in. If no datetime object is passed in, the
``'generic'`` variation is assumed. The following
values are valid: ``'generic'``, ``'daylight'`` and
``'standard'``.
:param locale: the `Locale` object, or a locale string. Defaults to the system time locale.
:param return_zone: True or False. If true then function
returns long time zone ID
| datetime.date babel.dates.parse_date |
( |
str |
string, |
|
|
Locale | str | None |
locale = None, |
|
|
_PredefinedTimeFormat | str |
format = 'medium' |
|
) |
| |
Parse a date from a string.
If an explicit format is provided, it is used to parse the date.
>>> parse_date('01.04.2004', format='dd.MM.yyyy')
datetime.date(2004, 4, 1)
If no format is given, or if it is one of "full", "long", "medium",
or "short", the function first tries to interpret the string as
ISO-8601 date format and then uses the date format for the locale
as a hint to determine the order in which the date fields appear in
the string.
>>> parse_date('4/1/04', locale='en_US')
datetime.date(2004, 4, 1)
>>> parse_date('01.04.2004', locale='de_DE')
datetime.date(2004, 4, 1)
>>> parse_date('2004-04-01', locale='en_US')
datetime.date(2004, 4, 1)
>>> parse_date('2004-04-01', locale='de_DE')
datetime.date(2004, 4, 1)
>>> parse_date('01.04.04', locale='de_DE', format='short')
datetime.date(2004, 4, 1)
:param string: the string containing the date
:param locale: a `Locale` object or a locale identifier
:param locale: a `Locale` object or a locale identifier. Defaults to the system time locale.
:param format: the format to use, either an explicit date format,
or one of "full", "long", "medium", or "short"
(see ``get_time_format``)