Skip to content

fix: add hacky workaround for near unix time timestamps on windows #1626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion interactions/models/discord/timestamp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
from datetime import datetime, timezone
import sys
from datetime import tzinfo, datetime, timezone
from enum import Enum
from typing import TYPE_CHECKING, Optional, Union

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

@classmethod
def fromtimestamp(cls, t: float, tz=None) -> "Timestamp":
if sys.platform == "win32" and t < 0:
raise ValueError("Negative timestamps are not supported on Windows.")

try:
timestamp = super().fromtimestamp(t, tz=tz)
except Exception:
Expand Down Expand Up @@ -85,6 +89,28 @@ def utcnow(cls) -> "Timestamp":
t = time.time()
return cls.utcfromtimestamp(t)

def astimezone(self, tz: tzinfo | None = None) -> "Timestamp":
# workaround of https://github.com/python/cpython/issues/107078

if sys.platform != "win32":
return super().astimezone(tz)

# this bound is loose, but it's good enough for our purposes
if self.year > 1970 or (self.year == 1970 and (self.month > 1 or self.day > 1)):
return super().astimezone(tz)

if self.year < 1969 or self.month < 12 or self.day < 31:
# windows kind of breaks down for dates before unix time
# technically this is solvable, but it's not worth the effort
# also, again, this is a loose bound, but it's good enough for our purposes
raise ValueError("astimezone with no arguments is not supported for dates before Unix Time on Windows.")

# to work around the issue to some extent, we'll use a timestamp with a date
# that doesn't trigger the bug, and use the timezone from it to modify this
# timestamp
sample_datetime = Timestamp(1970, 1, 5).astimezone()
return self.replace(tzinfo=sample_datetime.tzinfo)

def to_snowflake(self, high: bool = False) -> Union[str, "Snowflake"]:
"""
Returns a numeric snowflake pretending to be created at the given date.
Expand Down