This commit is contained in:
doms9 2025-11-11 17:16:10 -05:00
parent ceea4f7f5b
commit 00000d91dc
6 changed files with 28 additions and 21 deletions

View file

@ -85,7 +85,7 @@ async def get_events(
sport = pattern.split(details)[0].strip() sport = pattern.split(details)[0].strip()
event_time = Time.from_str(f"{now.date()} {match[0]} CET", "%Y-%m-%d %H:%M") event_time = Time.from_str(f"{now.date()} {match[0]}", timezone="CET")
if not start_ts <= event_time <= end_ts: if not start_ts <= event_time <= end_ts:
continue continue

View file

@ -61,7 +61,7 @@ async def get_events(
events: list[dict[str, str]] = [] events: list[dict[str, str]] = []
for info in api_data["days"]: for info in api_data["days"]:
day = Time.from_str(info["day_et"], "%Y-%m-%d") day = Time.from_str(info["day_et"])
if now.date() != day.date(): if now.date() != day.date():
continue continue

View file

@ -63,7 +63,7 @@ async def get_events(
end_dt = now.delta(minutes=30) end_dt = now.delta(minutes=30)
for event in api_data["events"]: for event in api_data["events"]:
event_dt = Time.from_str(f'{event["date"]} UTC', "%Y-%m-%dT%H:%M:%S.%fZ") event_dt = Time.from_str(f'{event["date"]}', timezone="UTC")
if now.date() != event_dt.date(): if now.date() != event_dt.date():
continue continue

View file

@ -80,7 +80,7 @@ async def refresh_html_cache(
data_start = span.attributes["data-start"].rsplit(":", 1)[0] data_start = span.attributes["data-start"].rsplit(":", 1)[0]
event_dt = Time.from_str(f"{data_start} PST") event_dt = Time.from_str(f"{data_start}", timezone="PST")
key = f"[{sport}] {event} (ROXIE)" key = f"[{sport}] {event} (ROXIE)"

View file

@ -47,44 +47,47 @@ class Time(datetime):
dt = self.astimezone(self.ZONES[tzone]) dt = self.astimezone(self.ZONES[tzone])
return self.__class__.fromtimestamp(dt.timestamp(), tz=self.ZONES[tzone]) return self.__class__.fromtimestamp(dt.timestamp(), tz=self.ZONES[tzone])
@classmethod
def _to_class_tz(cls, dt) -> "Time":
dt = dt.astimezone(cls.TZ)
return cls.fromtimestamp(dt.timestamp(), tz=cls.TZ)
@classmethod @classmethod
def from_str( def from_str(
cls, cls,
s: str, s: str,
fmt: str | None = None, fmt: str | None = None,
timezone: str | None = None,
) -> "Time": ) -> "Time":
tz = cls.ZONES.get(timezone, cls.TZ)
pattern = re.compile(rf"\b({"|".join(cls.ZONES.keys())})\b")
match = pattern.search(s)
tz = cls.ZONES.get(match[1]) if match else cls.TZ
cleaned_str = pattern.sub("", s).strip()
if fmt: if fmt:
dt = datetime.strptime(cleaned_str, fmt) dt = datetime.strptime(s, fmt)
dt = tz.localize(dt)
else: else:
formats = [ formats = [
"%B %d, %Y %H:%M %p", "%B %d, %Y %I:%M %p",
"%B %d, %Y %I:%M:%S %p",
"%m/%d/%Y %I:%M %p",
"%B %d, %Y %H:%M", "%B %d, %Y %H:%M",
"%B %d, %Y %H:%M:%S", "%B %d, %Y %H:%M:%S",
"%B %d, %Y %H:%M:%S %p", "%Y-%m-%d",
"%Y-%m-%d %H:%M", "%Y-%m-%d %H:%M",
"%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M:%S",
"%Y/%m/%d %H:%M", "%Y/%m/%d %H:%M",
"%Y/%m/%d %H:%M:%S", "%Y/%m/%d %H:%M:%S",
"%m/%d/%Y %H:%M", "%m/%d/%Y %H:%M",
"%m/%d/%Y %H:%M:%S", "%m/%d/%Y %H:%M:%S",
"%m/%d/%Y %I:%M %p",
"%Y/%m/%dT%H:%M:%S.%fZ", "%Y/%m/%dT%H:%M:%S.%fZ",
"%Y-%m-%dT%H:%M:%S.%fZ",
"%a, %d %b %Y %H:%M:%S %z", "%a, %d %b %Y %H:%M:%S %z",
] ]
for frmt in formats: for frmt in formats:
try: try:
dt = datetime.strptime(cleaned_str, frmt) dt = datetime.strptime(s, frmt)
break break
except ValueError: except ValueError:
continue continue
@ -92,9 +95,13 @@ class Time(datetime):
return cls.from_ts(Time.default_8()) return cls.from_ts(Time.default_8())
if not dt.tzinfo: if not dt.tzinfo:
dt = tz.localize(dt) if hasattr(tz, "localize") else dt.replace(tzinfo=tz) dt = (
tz.localize(dt)
if hasattr(tz, "localize")
else dt.replace(tzinfo=tz)
)
return cls.fromtimestamp(dt.astimezone(cls.TZ).timestamp(), tz=cls.TZ) return cls._to_class_tz(dt)
class Leagues: class Leagues: