Skip to content

Commit d0a094d

Browse files
committed
♻️ move format_dt to public
1 parent b2b8625 commit d0a094d

File tree

2 files changed

+60
-51
lines changed

2 files changed

+60
-51
lines changed

discord/utils/__init__.py

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,16 @@
5050
)
5151

5252
from ..errors import HTTPException
53-
from .public import basic_autocomplete, generate_snowflake, utcnow, snowflake_time, oauth_url, Undefined, MISSING
53+
from .public import (
54+
basic_autocomplete,
55+
generate_snowflake,
56+
utcnow,
57+
snowflake_time,
58+
oauth_url,
59+
Undefined,
60+
MISSING,
61+
format_dt,
62+
)
5463

5564
try:
5665
import msgspec
@@ -603,53 +612,3 @@ def as_chunks(iterator: _Iter[T], max_size: int) -> _Iter[list[T]]:
603612
if isinstance(iterator, AsyncIterator):
604613
return _achunk(iterator, max_size)
605614
return _chunk(iterator, max_size)
606-
607-
608-
TimestampStyle = Literal["f", "F", "d", "D", "t", "T", "R"]
609-
610-
611-
def format_dt(dt: datetime.datetime | datetime.time, /, style: TimestampStyle | None = None) -> str:
612-
"""A helper function to format a :class:`datetime.datetime` for presentation within Discord.
613-
614-
This allows for a locale-independent way of presenting data using Discord specific Markdown.
615-
616-
+-------------+----------------------------+-----------------+
617-
| Style | Example Output | Description |
618-
+=============+============================+=================+
619-
| t | 22:57 | Short Time |
620-
+-------------+----------------------------+-----------------+
621-
| T | 22:57:58 | Long Time |
622-
+-------------+----------------------------+-----------------+
623-
| d | 17/05/2016 | Short Date |
624-
+-------------+----------------------------+-----------------+
625-
| D | 17 May 2016 | Long Date |
626-
+-------------+----------------------------+-----------------+
627-
| f (default) | 17 May 2016 22:57 | Short Date Time |
628-
+-------------+----------------------------+-----------------+
629-
| F | Tuesday, 17 May 2016 22:57 | Long Date Time |
630-
+-------------+----------------------------+-----------------+
631-
| R | 5 years ago | Relative Time |
632-
+-------------+----------------------------+-----------------+
633-
634-
Note that the exact output depends on the user's locale setting in the client. The example output
635-
presented is using the ``en-GB`` locale.
636-
637-
.. versionadded:: 2.0
638-
639-
Parameters
640-
----------
641-
dt: Union[:class:`datetime.datetime`, :class:`datetime.time`]
642-
The datetime to format.
643-
style: :class:`str`R
644-
The style to format the datetime with.
645-
646-
Returns
647-
-------
648-
:class:`str`
649-
The formatted string.
650-
"""
651-
if isinstance(dt, datetime.time):
652-
dt = datetime.datetime.combine(datetime.datetime.now(), dt)
653-
if style is None:
654-
return f"<t:{int(dt.timestamp())}>"
655-
return f"<t:{int(dt.timestamp())}:{style}>"

discord/utils/public.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,53 @@ def oauth_url(
259259
if disable_guild_select:
260260
url += "&disable_guild_select=true"
261261
return url
262+
263+
264+
TimestampStyle = Literal["f", "F", "d", "D", "t", "T", "R"]
265+
266+
267+
def format_dt(dt: datetime.datetime | datetime.time, /, style: TimestampStyle | None = None) -> str:
268+
"""A helper function to format a :class:`datetime.datetime` for presentation within Discord.
269+
270+
This allows for a locale-independent way of presenting data using Discord specific Markdown.
271+
272+
+-------------+----------------------------+-----------------+
273+
| Style | Example Output | Description |
274+
+=============+============================+=================+
275+
| t | 22:57 | Short Time |
276+
+-------------+----------------------------+-----------------+
277+
| T | 22:57:58 | Long Time |
278+
+-------------+----------------------------+-----------------+
279+
| d | 17/05/2016 | Short Date |
280+
+-------------+----------------------------+-----------------+
281+
| D | 17 May 2016 | Long Date |
282+
+-------------+----------------------------+-----------------+
283+
| f (default) | 17 May 2016 22:57 | Short Date Time |
284+
+-------------+----------------------------+-----------------+
285+
| F | Tuesday, 17 May 2016 22:57 | Long Date Time |
286+
+-------------+----------------------------+-----------------+
287+
| R | 5 years ago | Relative Time |
288+
+-------------+----------------------------+-----------------+
289+
290+
Note that the exact output depends on the user's locale setting in the client. The example output
291+
presented is using the ``en-GB`` locale.
292+
293+
.. versionadded:: 2.0
294+
295+
Parameters
296+
----------
297+
dt: Union[:class:`datetime.datetime`, :class:`datetime.time`]
298+
The datetime to format.
299+
style: :class:`str`R
300+
The style to format the datetime with.
301+
302+
Returns
303+
-------
304+
:class:`str`
305+
The formatted string.
306+
"""
307+
if isinstance(dt, datetime.time):
308+
dt = datetime.datetime.combine(datetime.datetime.now(), dt)
309+
if style is None:
310+
return f"<t:{int(dt.timestamp())}>"
311+
return f"<t:{int(dt.timestamp())}:{style}>"

0 commit comments

Comments
 (0)