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
Open
akx
wants to merge
2
commits into
master
Choose a base branch
from
cldr-48-relative
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
|
|
@@ -766,6 +817,57 @@ 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, the standard ``datetime_formats`` pattern is used as a fallback: | ||
|
|
||
| >>> format_relative_datetime('zítra', time(15, 30), locale='cs', format='short') | ||
| 'zítra 15:30' | ||
|
Comment on lines
+845
to
+846
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 |
||
|
|
||
| :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 locale.datetime_formats[format] | ||
| ) | ||
|
akx marked this conversation as resolved.
Resolved
|
||
| 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', | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.