File tree Expand file tree Collapse file tree 2 files changed +64
-0
lines changed
Expand file tree Collapse file tree 2 files changed +64
-0
lines changed Original file line number Diff line number Diff line change 1+ import datetime
2+ import enum
3+ import typing
4+
5+ import arrow
6+
7+
8+ class TimeStampEnum (enum .Enum ):
9+ """
10+ Timestamp modes for discord.
11+
12+ Full docs on this format are viewable here:
13+ https://discord.com/developers/docs/reference#message-formatting
14+ """
15+
16+ SHORT_TIME = "t"
17+ LONG_TIME = "T"
18+ SHORT_DATE = "d"
19+ LONG_DATE = "D"
20+ SHORT_DATE_TIME = "f"
21+ LONG_DATE_TIME = "F"
22+ RELATIVE_TIME = "R"
23+
24+ # DEFAULT
25+ DEFAULT = SHORT_DATE_TIME
26+
27+
28+ TypeTimes = typing .Union [arrow .Arrow , datetime .datetime ]
29+
30+
31+ def get_discord_formatted_timestamp (
32+ timestamp : TypeTimes , format : TimeStampEnum = TimeStampEnum .DEFAULT
33+ ) -> str :
34+ """
35+ Return a discord formatted timestamp from a datetime compatiable datatype.
36+
37+ `format` must be an enum member of TimeStampEnum. Default style is SHORT_DATE_TIME
38+ """
39+ return f"<t:{ int (timestamp .timestamp ())} :{ format .value } >"
Original file line number Diff line number Diff line change 1+ import arrow
2+ import pytest
3+
4+ from modmail .utils import time as utils_time
5+ from modmail .utils .time import TimeStampEnum
6+
7+
8+ @pytest .mark .parametrize (
9+ ["timestamp" , "expected" , "mode" ],
10+ [
11+ [arrow .get (1634593650 ), "<t:1634593650:f>" , TimeStampEnum .SHORT_DATE_TIME ],
12+ [arrow .get (1 ), "<t:1:f>" , TimeStampEnum .SHORT_DATE_TIME ],
13+ [arrow .get (12356941 ), "<t:12356941:R>" , TimeStampEnum .RELATIVE_TIME ],
14+ ],
15+ )
16+ def test_timestamp (timestamp , expected : str , mode : utils_time .TimeStampEnum ):
17+ """Test the timestamp is of the proper form."""
18+ fmtted_timestamp = utils_time .get_discord_formatted_timestamp (timestamp , mode )
19+ assert expected == fmtted_timestamp
20+
21+
22+ def test_enum_default ():
23+ """Ensure that the default mode is of the correct mode, and works properly."""
24+ assert TimeStampEnum .DEFAULT .name == TimeStampEnum .SHORT_DATE_TIME .name
25+ assert TimeStampEnum .DEFAULT .value == TimeStampEnum .SHORT_DATE_TIME .value
You can’t perform that action at this time.
0 commit comments