Update the TimestampStrings zone info handling

time_zone is the string version of the time zone data
time_zone_zi is the ZoneInfo object of above
This commit is contained in:
Clemens Schwaighofer
2025-09-25 15:53:26 +09:00
parent 2690a285d9
commit 7c5af588c7
3 changed files with 10 additions and 8 deletions

View File

@@ -25,13 +25,15 @@ class TimestampStrings:
def __init__(self, time_zone: str | ZoneInfo | None = None):
self.timestamp_now = datetime.now()
self.time_zone = time_zone if time_zone is not None else self.TIME_ZONE
# set time zone as string
time_zone = time_zone if time_zone is not None else self.TIME_ZONE
self.time_zone = str(time_zone) if not isinstance(time_zone, str) else time_zone
# set ZoneInfo type
try:
self.timestamp_now_tz = datetime.now(
ZoneInfo(self.time_zone) if isinstance(self.time_zone, str) else self.time_zone
)
self.time_zone_zi = ZoneInfo(self.time_zone)
except ZoneInfoNotFoundError as e:
raise ValueError(f'Zone could not be loaded [{self.time_zone}]: {e}') from e
self.timestamp_now_tz = datetime.now(self.time_zone_zi)
self.today = self.timestamp_now.strftime('%Y-%m-%d')
self.timestamp = self.timestamp_now.strftime("%Y-%m-%d %H:%M:%S")
self.timestamp_tz = self.timestamp_now_tz.strftime("%Y-%m-%d %H:%M:%S %Z")