Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 12 additions & 2 deletions discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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"<t:{int(dt.timestamp())}>"
return f"<t:{int(dt.timestamp())}:{style}>"
Expand Down