From d1a2ee46209917000e57612c0bdce29b5035e15a Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 28 Jun 2021 01:30:56 -0400 Subject: [PATCH] Add discord.utils.format_dt helper function --- discord/utils.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ docs/api.rst | 2 ++ 2 files changed, 48 insertions(+) diff --git a/discord/utils.py b/discord/utils.py index ccecfeed8..7c088b4fe 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -944,3 +944,49 @@ def resolve_annotation( if cache is None: cache = {} return evaluate_annotation(annotation, globalns, locals, cache) + + +TimestampStyle = Literal['f', 'F', 'd', 'D', 't', 'T', 'R'] + + +def format_dt(dt: datetime.datetime, /, style: Optional[TimestampStyle] = 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. + + +-------------+----------------------------+-----------------+ + | Style | Example Output | Description | + +=============+============================+=================+ + | t | 22:57 | Short Time | + +-------------+----------------------------+-----------------+ + | T | 22:57:58 | Long Time | + +-------------+----------------------------+-----------------+ + | d | 17/05/2016 | Short Date | + +-------------+----------------------------+-----------------+ + | D | 17 May 2016 | Long Date | + +-------------+----------------------------+-----------------+ + | f (default) | 17 May 2016 22:57 | Short Date Time | + +-------------+----------------------------+-----------------+ + | F | Tuesday, 17 May 2016 22:57 | Long Date Time | + +-------------+----------------------------+-----------------+ + | R | 5 years ago | Relative Time | + +-------------+----------------------------+-----------------+ + + Note that the exact output depends on the user's locale setting in the client. The example output + presented is using the ``en-GB`` locale. + + Parameters + ----------- + dt: :class:`datetime.datetime` + The datetime to format. + style: :class:`str` + The style to format the datetime with. + + Returns + -------- + :class:`str` + The formatted string. + """ + if style is None: + return f'' + return f'' diff --git a/docs/api.rst b/docs/api.rst index 05402072b..a27741bb9 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -1066,6 +1066,8 @@ Utility Functions .. autofunction:: discord.utils.utcnow +.. autofunction:: discord.utils.format_dt + .. autofunction:: discord.utils.as_chunks .. _discord-api-enums: