diff --git a/nats/src/nats/js/api.py b/nats/src/nats/js/api.py index cdd254db..1385b766 100644 --- a/nats/src/nats/js/api.py +++ b/nats/src/nats/js/api.py @@ -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. @@ -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]): @@ -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) @@ -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( diff --git a/nats/tests/test_js.py b/nats/tests/test_js.py index 1acfd4b9..ca68eece 100644 --- a/nats/tests/test_js.py +++ b/nats/tests/test_js.py @@ -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()