Repository navigation
Support CLDR 48 relative date formats #1306
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -355,6 +355,57 @@ def get_era_names( | |
| return Locale.parse(locale or LC_TIME).eras[width] | ||
|
|
||
|
|
||
| def get_relative_name( | ||
| field: str, | ||
| offset: int, | ||
| length: Literal['long', 'short', 'narrow'] = 'long', | ||
| locale: Locale | str | None = None, | ||
| ) -> str | None: | ||
| """Return the locale's literal name for a relative offset of a date field. | ||
|
|
||
| These are the CLDR ``<relative>`` strings such as "yesterday", "today", | ||
| "tomorrow" for ``field='day'``, or "last Monday"/"this Monday"/"next Monday" | ||
| for ``field='mon'``. | ||
|
|
||
| Returns ``None`` when the locale does not define a literal name for the | ||
| requested field/offset (in which case a phrase like "in 2 days" built | ||
| via :func:`format_timedelta` is typically the right fallback). | ||
|
|
||
| >>> get_relative_name('day', -1, locale='en') | ||
| 'yesterday' | ||
| >>> get_relative_name('day', 1, locale='en') | ||
| 'tomorrow' | ||
| >>> get_relative_name('day', -2, locale='de') | ||
| 'vorgestern' | ||
| >>> get_relative_name('year', 1, locale='en') | ||
| 'next year' | ||
| >>> get_relative_name('year', 1, length='short', locale='en') | ||
| 'next yr.' | ||
| >>> get_relative_name('mon', 1, locale='en') | ||
| 'next Monday' | ||
|
|
||
| Offsets without a literal name return ``None``: | ||
|
|
||
| >>> get_relative_name('day', 5, locale='en') is None | ||
| True | ||
|
|
||
| :param field: the date field, e.g. "day", "week", "month", "year", | ||
| "hour", "minute", "second", "quarter", or a weekday | ||
| abbreviation like "mon", "tue", etc. | ||
| :param offset: the offset from the current value of *field*. | ||
| :param length: "long", "short", or "narrow". | ||
| :param locale: a `Locale` object or a locale identifier. Defaults to the | ||
| system time locale. | ||
|
|
||
| .. versionadded:: 2.19 | ||
| """ | ||
| locale = Locale.parse(locale or LC_TIME) | ||
| date_fields = locale._data['date_fields'] | ||
| key = field if length == 'long' else f'{field}-{length}' | ||
| data = date_fields.get(key) or date_fields.get(field) or {} | ||
| return data.get('relative', {}).get(offset) | ||
|
|
||
|
|
||
| def get_date_format( | ||
| format: _PredefinedTimeFormat = 'medium', | ||
| locale: Locale | str | None = None, | ||
|
|
@@ -764,6 +815,58 @@ def format_datetime( | |
| return parse_pattern(format).apply(datetime, locale) | ||
|
|
||
|
|
||
| def format_relative_datetime( | ||
| relative_date: str, | ||
| time: datetime.time | datetime.datetime | float | None = None, | ||
| format: _PredefinedTimeFormat = 'medium', | ||
| tzinfo: datetime.tzinfo | None = None, | ||
| locale: Locale | str | None = None, | ||
| ) -> str: | ||
| """Combine a pre-formatted relative date with a time using the locale's | ||
| relative datetime pattern. | ||
|
|
||
| For instance, in English you might say "tomorrow at 3:30 PM" instead of | ||
| "tomorrow, 3:30 PM". | ||
|
|
||
| The caller supplies the relative date portion as a string | ||
| (such as "tomorrow" or "in 3 days"); this function formats the time and joins the two. | ||
|
|
||
| >>> from datetime import time | ||
| >>> format_relative_datetime('tomorrow', time(15, 30), locale='en', format='long') | ||
| 'tomorrow at 3:30:00\u202fPM UTC' | ||
| >>> format_relative_datetime('morgen', time(15, 30), locale='de', format='long') | ||
| 'morgen um 15:30:00 UTC' | ||
|
|
||
| When the locale does not define a relative pattern for the requested | ||
| width, its standard datetime pattern is used, as CLDR's root locale | ||
| aliases ``relative`` patterns to ``standard`` ones: | ||
|
|
||
| >>> format_relative_datetime('zítra', time(15, 30), locale='cs', format='short') | ||
| 'zítra 15:30' | ||
|
Comment on lines
+844
to
+845
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether it'd be better to somehow signal that the relative pattern does not exist, because this is just incorrect in Czech (should be
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requested access for the CLDR Survey Tool, let's see how that works :D
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC I tried to do that at one point, but you needed to be part of an endorsed organization or something? So I just ended up complaining on the Unicode JIRA about whatever it was back then 😅
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But yeah, it probably could be better to give up on formatting a relative pattern if the result will be wrong. EDIT: Or not. I think we might want to keep this as-is and see if we can improve CLDR instead, eventually :)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Indeed, a good motivation to improve things, especially if other people complain :D |
||
|
|
||
| :param relative_date: the already-localized relative date string. | ||
| :param time: the time to combine with the relative date. | ||
| If ``None`` the current time is used. | ||
| :param format: one of "full", "long", "medium", or "short". | ||
| :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. | ||
|
|
||
| .. versionadded:: 2.19 | ||
| """ | ||
| locale = Locale.parse(locale or LC_TIME) | ||
| pattern = ( | ||
| locale.datetime_formats_relative.get(format) | ||
| or get_datetime_format(format, locale=locale) | ||
| ) | ||
| return ( | ||
| pattern | ||
| .replace("'", "") | ||
| .replace('{0}', format_time(time, format, tzinfo=tzinfo, locale=locale)) | ||
| .replace('{1}', relative_date) | ||
| ) | ||
|
|
||
|
|
||
| def format_time( | ||
| time: datetime.time | datetime.datetime | float | None = None, | ||
| format: _PredefinedTimeFormat | str = 'medium', | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.