Compare commits

...

2 Commits

Author SHA1 Message Date
Clemens Schwaighofer
ed22105ec8 v0.24.4: Fix Zone info data in TimestampStrings class 2025-09-25 15:54:54 +09:00
Clemens Schwaighofer
7c5af588c7 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
2025-09-25 15:53:26 +09:00
4 changed files with 11 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
# MARK: Project info # MARK: Project info
[project] [project]
name = "corelibs" name = "corelibs"
version = "0.24.3" version = "0.24.4"
description = "Collection of utils for Python scripts" description = "Collection of utils for Python scripts"
readme = "README.md" readme = "README.md"
requires-python = ">=3.13" requires-python = ">=3.13"

View File

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

View File

@@ -144,8 +144,8 @@ class TestTimestampStrings:
ts = TimestampStrings(time_zone=custom_tz_obj) ts = TimestampStrings(time_zone=custom_tz_obj)
# The time_zone should be the ZoneInfo object itself # The time_zone should be the ZoneInfo object itself
assert ts.time_zone is custom_tz_obj assert ts.time_zone_zi is custom_tz_obj
assert isinstance(ts.time_zone, ZoneInfo) assert isinstance(ts.time_zone_zi, ZoneInfo)
def test_zoneinfo_object_vs_string_equivalence(self): def test_zoneinfo_object_vs_string_equivalence(self):
"""Test that ZoneInfo object and string produce equivalent results""" """Test that ZoneInfo object and string produce equivalent results"""
@@ -167,7 +167,7 @@ class TestTimestampStrings:
# The time_zone attributes will be different types but represent the same timezone # The time_zone attributes will be different types but represent the same timezone
assert str(ts_string.time_zone) == 'Europe/Paris' assert str(ts_string.time_zone) == 'Europe/Paris'
assert isinstance(ts_zoneinfo.time_zone, ZoneInfo) assert isinstance(ts_zoneinfo.time_zone_zi, ZoneInfo)
def test_edge_case_midnight(self): def test_edge_case_midnight(self):
"""Test timestamp formatting at midnight""" """Test timestamp formatting at midnight"""

2
uv.lock generated
View File

@@ -53,7 +53,7 @@ wheels = [
[[package]] [[package]]
name = "corelibs" name = "corelibs"
version = "0.24.2" version = "0.24.3"
source = { editable = "." } source = { editable = "." }
dependencies = [ dependencies = [
{ name = "jmespath" }, { name = "jmespath" },