Skip to content

Commit abeaa32

Browse files
committed
accept string as timestamp
1 parent 186042e commit abeaa32

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

discord_webhook/webhook.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import logging
33
import time
4-
from datetime import datetime
4+
from datetime import datetime, UTC
55
from functools import partial
66
from http.client import HTTPException
77
from typing import Any, Dict, List, Optional, Tuple, Union
@@ -47,7 +47,7 @@ def __init__(
4747
:keyword dict image: image that will be displayed in the embed
4848
:keyword dict provider: information about the provider
4949
:keyword dict thumbnail: thumbnail that will be displayed in the embed
50-
:keyword str timestamp: timestamp that will be displayed in the embed
50+
:keyword float, int, str, datetime timestamp: timestamp that will be displayed in the embed
5151
:keyword str title: title of embed
5252
:keyword str url: add an url to make your embedded title a clickable link
5353
:keyword dict video: video that will be displayed in the embed
@@ -88,18 +88,21 @@ def set_url(self, url: str) -> None:
8888
self.url = url
8989

9090
def set_timestamp(
91-
self, timestamp: Optional[Union[float, int, datetime]] = None
91+
self, timestamp: Optional[Union[float, int, str, datetime]] = None
9292
) -> None:
9393
"""
9494
Set timestamp of the embed content.
95-
:param timestamp: timestamp of embed content
95+
:param timestamp: optional timestamp of embed content
9696
"""
9797
if timestamp is None:
98-
timestamp = datetime.utcnow()
98+
timestamp = datetime.now(UTC)
9999
elif isinstance(timestamp, float) or isinstance(timestamp, int):
100-
timestamp = datetime.utcfromtimestamp(timestamp)
100+
timestamp = datetime.fromtimestamp(timestamp, UTC).replace(tzinfo=None)
101101

102-
self.timestamp = timestamp.isoformat()
102+
if not isinstance(timestamp, str):
103+
timestamp = timestamp.isoformat()
104+
105+
self.timestamp = timestamp
103106

104107
def set_color(self, color: Union[str, int]) -> None:
105108
"""

tests/test_embed.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def test__set_embed__url(embed):
4242
1679610926.0,
4343
datetime.fromisoformat("2023-03-23T22:35:26"),
4444
datetime.fromisoformat("2023-03-23T23:35:26+01:00"),
45+
"2023-03-23T22:35:26",
4546
],
4647
)
4748
def test__set_embed__timestamp(embed, timestamp):

0 commit comments

Comments
 (0)