File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 2020import dataclasses
2121import time
2222import uuid
23+ from datetime import datetime , timezone
2324from typing import Any , Generic , Optional , TypeVar
2425
2526from .broker import get_broker
@@ -189,3 +190,13 @@ def _fields(self) -> tuple[str, ...]:
189190
190191 def _replace (self , ** changes ) -> Message [R ]:
191192 return dataclasses .replace (self , ** changes )
193+
194+ @property
195+ def message_datetime (self ) -> datetime :
196+ """Read ``message_timestamp`` as a UTC-aware datetime.
197+
198+ Datetime precision is limited by the representation of ``Message.message_timestamp``, which is an
199+ integer storing milliseconds.
200+ """
201+ unix_seconds , ms = divmod (self .message_timestamp , 1000 )
202+ return datetime .fromtimestamp (unix_seconds , tz = timezone .utc ).replace (microsecond = ms * 1000 )
Original file line number Diff line number Diff line change 11from __future__ import annotations
22
3+ from datetime import datetime , timedelta , timezone
4+
35import dramatiq
46
57
@@ -47,3 +49,21 @@ def actor(arg):
4749
4850 assert repr (message ) in repr (message_proxy ), "Expecting MessageProxy repr to contain Message repr"
4951 assert str (message ) == str (message_proxy ), "Expecting identical __str__ of MessageProxy and Message"
52+
53+
54+ def test_message_datetime (stub_broker ):
55+ """Test reading message time as datetime."""
56+
57+ @dramatiq .actor
58+ def actor (arg ):
59+ return arg
60+
61+ message = actor .send ("input" )
62+ after = datetime .now (timezone .utc )
63+
64+ assert message .message_datetime .tzinfo is timezone .utc
65+ assert message .message_datetime < after
66+ assert message .message_datetime > after - timedelta (seconds = 1 )
67+
68+ # no rounding problems here because message_timestamp is an int
69+ assert message .message_datetime .timestamp () == message .message_timestamp / 1000
You can’t perform that action at this time.
0 commit comments