Skip to content

Commit af9866f

Browse files
authored
fix: add hacky workaround for small timestamps on windows (#1626)
1 parent ea649a7 commit af9866f

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

interactions/models/discord/timestamp.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
2-
from datetime import datetime, timezone
2+
import sys
3+
from datetime import tzinfo, datetime, timezone
34
from enum import Enum
45
from typing import TYPE_CHECKING, Optional, Union
56

@@ -56,6 +57,9 @@ def fromisocalendar(cls, year: int, week: int, day: int) -> "Timestamp":
5657

5758
@classmethod
5859
def fromtimestamp(cls, t: float, tz=None) -> "Timestamp":
60+
if sys.platform == "win32" and t < 0:
61+
raise ValueError("Negative timestamps are not supported on Windows.")
62+
5963
try:
6064
timestamp = super().fromtimestamp(t, tz=tz)
6165
except Exception:
@@ -85,6 +89,28 @@ def utcnow(cls) -> "Timestamp":
8589
t = time.time()
8690
return cls.utcfromtimestamp(t)
8791

92+
def astimezone(self, tz: tzinfo | None = None) -> "Timestamp":
93+
# workaround of https://github.com/python/cpython/issues/107078
94+
95+
if sys.platform != "win32":
96+
return super().astimezone(tz)
97+
98+
# this bound is loose, but it's good enough for our purposes
99+
if self.year > 1970 or (self.year == 1970 and (self.month > 1 or self.day > 1)):
100+
return super().astimezone(tz)
101+
102+
if self.year < 1969 or self.month < 12 or self.day < 31:
103+
# windows kind of breaks down for dates before unix time
104+
# technically this is solvable, but it's not worth the effort
105+
# also, again, this is a loose bound, but it's good enough for our purposes
106+
raise ValueError("astimezone with no arguments is not supported for dates before Unix Time on Windows.")
107+
108+
# to work around the issue to some extent, we'll use a timestamp with a date
109+
# that doesn't trigger the bug, and use the timezone from it to modify this
110+
# timestamp
111+
sample_datetime = Timestamp(1970, 1, 5).astimezone()
112+
return self.replace(tzinfo=sample_datetime.tzinfo)
113+
88114
def to_snowflake(self, high: bool = False) -> Union[str, "Snowflake"]:
89115
"""
90116
Returns a numeric snowflake pretending to be created at the given date.

0 commit comments

Comments
 (0)