@@ -1364,7 +1364,17 @@ def to_datetime(value: str, /) -> datetime.datetime:
13641364_VALID_DATETIME_STYLES = frozenset (("t" , "T" , "d" , "D" , "f" , "F" , "R" ))
13651365
13661366
1367+ @typing .overload
1368+ def from_datetime (value : datetime .timedelta , / ) -> str :
1369+ ...
1370+
1371+
1372+ @typing .overload
13671373def from_datetime (value : datetime .datetime , / , * , style : str = "f" ) -> str :
1374+ ...
1375+
1376+
1377+ def from_datetime (value : typing .Union [datetime .datetime , datetime .timedelta ], / , * , style : str = "f" ) -> str :
13681378 """Format a datetime as Discord's datetime format.
13691379
13701380 More information on this format can be found at
@@ -1374,12 +1384,17 @@ def from_datetime(value: datetime.datetime, /, *, style: str = "f") -> str:
13741384 ----------
13751385 value
13761386 The datetime to format.
1387+
1388+ If a timedelta is passed here then this is treated as a date that's
1389+ relative to the current time.
13771390 style
13781391 The style to use.
13791392
13801393 The valid styles can be found at
13811394 <https://discord.com/developers/docs/reference#message-formatting-formats>.
13821395
1396+ This is always "R" when `value` is a [datetime.timedelta][].
1397+
13831398 Returns
13841399 -------
13851400 str
@@ -1391,6 +1406,9 @@ def from_datetime(value: datetime.datetime, /, *, style: str = "f") -> str:
13911406 If the provided datetime is timezone naive.
13921407 If an invalid style is provided.
13931408 """
1409+ if isinstance (value , datetime .timedelta ):
1410+ return from_datetime (datetime .datetime .now (tz = datetime .timezone .utc ) + value , style = "R" )
1411+
13941412 if style not in _VALID_DATETIME_STYLES :
13951413 raise ValueError (f"Invalid style: { style } " )
13961414
0 commit comments