Skip to content
Open
Show file tree
Hide file tree
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
30 changes: 19 additions & 11 deletions nats/src/nats/js/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ def _to_nanoseconds(val: Optional[float]) -> Optional[int]:
return 0
return int(val * _NANOSECOND)

@classmethod
def _python38_iso_parsing(cls, time_string: str):
# Replace Z with UTC offset
s = time_string.replace("Z", "+00:00")
# Trim fractional seconds to 6 digits
date_part, frac_tz = s.split(".", 1)
frac, tz = frac_tz.split("+")
frac = frac[:6] # keep only microseconds
s = f"{date_part}.{frac}+{tz}"
return s


@classmethod
def from_response(cls: type[_B], resp: Dict[str, Any]) -> _B:
"""Read the class instance from a server response.
Expand Down Expand Up @@ -410,6 +422,7 @@ class StreamInfo(Base):
sources: Optional[List[StreamSourceInfo]] = None
cluster: Optional[ClusterInfo] = None
did_create: Optional[bool] = None
created: Optional[datetime.datetime] = None

@classmethod
def from_response(cls, resp: Dict[str, Any]):
Expand All @@ -418,6 +431,12 @@ def from_response(cls, resp: Dict[str, Any]):
cls._convert(resp, "mirror", StreamSourceInfo)
cls._convert(resp, "sources", StreamSourceInfo)
cls._convert(resp, "cluster", ClusterInfo)

if "created" in resp and resp["created"]:
resp["created"] = datetime.datetime.fromisoformat(cls._python38_iso_parsing(resp["created"])).astimezone(
datetime.timezone.utc
)

return super().from_response(resp)


Expand Down Expand Up @@ -711,17 +730,6 @@ def header(self) -> Optional[Dict]:
"""
return self.headers

@classmethod
def _python38_iso_parsing(cls, time_string: str):
# Replace Z with UTC offset
s = time_string.replace("Z", "+00:00")
# Trim fractional seconds to 6 digits
date_part, frac_tz = s.split(".", 1)
frac, tz = frac_tz.split("+")
frac = frac[:6] # keep only microseconds
s = f"{date_part}.{frac}+{tz}"
return s

@classmethod
def from_response(cls, resp: Dict[str, Any]):
resp["time"] = datetime.datetime.fromisoformat(cls._python38_iso_parsing(resp["time"])).astimezone(
Expand Down
1 change: 1 addition & 0 deletions nats/tests/test_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,7 @@ async def test_stream_management(self):
assert isinstance(current.config, nats.js.api.StreamConfig)
assert current.config.name == "hello"
assert isinstance(current.state, nats.js.api.StreamState)
assert isinstance(current.created, datetime.datetime)

# Send messages
producer = nc.jetstream()
Expand Down