Skip to content

Commit

Permalink
Timezone-aware datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
diorcety committed Jan 16, 2025
1 parent 7e7883e commit 4db8e82
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions nats/aio/msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def _from_reply(cls, reply: str) -> Msg.Metadata:
tokens = cls._get_metadata_fields(reply)
if len(tokens) == _V1_TOKEN_COUNT:
t = datetime.datetime.fromtimestamp(
int(tokens[7]) / 1_000_000_000.0
int(tokens[7]) / 1_000_000_000.0, datetime.timezone.utc
)
return cls(
sequence=Msg.Metadata.SequencePair(
Expand All @@ -236,7 +236,8 @@ def _from_reply(cls, reply: str) -> Msg.Metadata:
)
else:
t = datetime.datetime.fromtimestamp(
int(tokens[Msg.Ack.Timestamp]) / 1_000_000_000.0
int(tokens[Msg.Ack.Timestamp]) / 1_000_000_000.0,
datetime.timezone.utc
)

# Underscore indicate no domain is set. Expose as empty string
Expand Down
2 changes: 1 addition & 1 deletion nats/js/kv.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ async def purge_deletes(self, olderthan: int = 30 * 60) -> bool:
for entry in delete_markers:
keep = 0
subject = f"{self._pre}{entry.key}"
duration = datetime.datetime.now() - entry.created
duration = datetime.datetime.now(datetime.timezone.utc) - entry.created
if olderthan > 0 and olderthan > duration.total_seconds():
keep = 1
await self._js.purge_stream(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ async def test_fetch_one(self):
msg = msgs[0]
assert msg.metadata.sequence.stream == 1
assert msg.metadata.sequence.consumer == 1
assert datetime.datetime.now() > msg.metadata.timestamp
assert datetime.datetime.now(datetime.timezone.utc) > msg.metadata.timestamp
assert msg.metadata.num_pending == 0
assert msg.metadata.num_delivered == 1

Expand Down Expand Up @@ -294,7 +294,7 @@ async def test_fetch_one_wait_forever(self):
msg = msgs[0]
assert msg.metadata.sequence.stream == 1
assert msg.metadata.sequence.consumer == 1
assert datetime.datetime.now() > msg.metadata.timestamp
assert datetime.datetime.now(datetime.timezone.utc) > msg.metadata.timestamp
assert msg.metadata.num_pending == 0
assert msg.metadata.num_delivered == 1

Expand Down

0 comments on commit 4db8e82

Please sign in to comment.