From eec2af7f49cc128f3ef086ff67468cdd82c0e686 Mon Sep 17 00:00:00 2001 From: Paillat Date: Fri, 21 Mar 2025 14:53:09 +0100 Subject: [PATCH 1/4] :sparkles: Support formatting `datetime.time` in `format_dt` --- discord/utils.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/discord/utils.py b/discord/utils.py index c04ef0b0bd..72ebceba98 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -1236,7 +1236,9 @@ def resolve_annotation( TimestampStyle = Literal["f", "F", "d", "D", "t", "T", "R"] -def format_dt(dt: datetime.datetime, /, style: TimestampStyle | None = None) -> str: +def format_dt( + dt: datetime.datetime | datetime.time, /, style: TimestampStyle | None = None +) -> str: """A helper function to format a :class:`datetime.datetime` for presentation within Discord. This allows for a locale-independent way of presenting data using Discord specific Markdown. @@ -1266,7 +1268,7 @@ def format_dt(dt: datetime.datetime, /, style: TimestampStyle | None = None) -> Parameters ---------- - dt: :class:`datetime.datetime` + dt: Union[:class:`datetime.datetime`, :class:`datetime.time`] The datetime to format. style: :class:`str` The style to format the datetime with. @@ -1276,6 +1278,14 @@ def format_dt(dt: datetime.datetime, /, style: TimestampStyle | None = None) -> :class:`str` The formatted string. """ + if isinstance(dt, datetime.time): + if style is None: + style = "t" + elif style not in ("t", "T"): + raise ValueError("Time styles must be 't' or 'T'.") + dt = datetime.datetime.combine( + datetime.datetime.fromtimestamp(DISCORD_EPOCH, tz=datetime.UTC), dt + ) if style is None: return f"" return f"" From 4de740a73c2c8350a85f225532aa12c258ca1ed3 Mon Sep 17 00:00:00 2001 From: Paillat Date: Fri, 21 Mar 2025 14:59:34 +0100 Subject: [PATCH 2/4] :memo: CHANGELOG.md --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e225b40ef..ce0a95b724 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,8 +49,11 @@ These changes are available on the `master` branch, but have not yet been releas ([#2579](https://github.com/Pycord-Development/pycord/pull/2579)) - Added new `Subscription` object and related methods/events. ([#2564](https://github.com/Pycord-Development/pycord/pull/2564)) -- Added ability to change the API's base URL with `Route.API_BASE_URL`. +- Added the ability to change the API's base URL with `Route.API_BASE_URL`. ([#2714](https://github.com/Pycord-Development/pycord/pull/2714)) +- Added the ability to pass a `datetime.time` object to `format_dt` if selecting a + time-only style (`"t"` or `"T"`) + ([#2747](https://github.com/Pycord-Development/pycord/pull/2747)) ### Fixed From 8ee4d73606c3e36fab1cddc016d97bb58e1cf5f6 Mon Sep 17 00:00:00 2001 From: Paillat Date: Fri, 21 Mar 2025 16:14:16 +0100 Subject: [PATCH 3/4] :sparkles: Update time formatting logic in `format_dt` to use current datetime --- discord/utils.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/discord/utils.py b/discord/utils.py index 72ebceba98..363d339391 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -1279,13 +1279,7 @@ def format_dt( The formatted string. """ if isinstance(dt, datetime.time): - if style is None: - style = "t" - elif style not in ("t", "T"): - raise ValueError("Time styles must be 't' or 'T'.") - dt = datetime.datetime.combine( - datetime.datetime.fromtimestamp(DISCORD_EPOCH, tz=datetime.UTC), dt - ) + dt = datetime.datetime.combine(datetime.datetime.now(), dt) if style is None: return f"" return f"" From 8487692b8c7b158350d2a3d0356f7a4025f6730e Mon Sep 17 00:00:00 2001 From: Paillat Date: Fri, 21 Mar 2025 16:15:03 +0100 Subject: [PATCH 4/4] :memo: CHANGELOG.md --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce0a95b724..13d9554399 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,8 +51,7 @@ These changes are available on the `master` branch, but have not yet been releas ([#2564](https://github.com/Pycord-Development/pycord/pull/2564)) - Added the ability to change the API's base URL with `Route.API_BASE_URL`. ([#2714](https://github.com/Pycord-Development/pycord/pull/2714)) -- Added the ability to pass a `datetime.time` object to `format_dt` if selecting a - time-only style (`"t"` or `"T"`) +- Added the ability to pass a `datetime.time` object to `format_dt` ([#2747](https://github.com/Pycord-Development/pycord/pull/2747)) ### Fixed