Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not use deprecated datetime.utcnow and datetime.utcfromtimestamp #610

Merged
merged 2 commits into from
Mar 1, 2024
Merged
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
2 changes: 0 additions & 2 deletions aio_pika/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
ConsumerTag = str

MILLISECONDS = 1000
ZERO_TIME = datetime.utcfromtimestamp(0)


class SSLOptions(TypedDict, total=False):
Expand Down Expand Up @@ -958,6 +957,5 @@ def _get_exchange_name_from_str(value: str) -> str:
"TransactionState",
"UnderlayChannel",
"UnderlayConnection",
"ZERO_TIME",
"get_exchange_name",
)
45 changes: 11 additions & 34 deletions aio_pika/message.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import time
import warnings
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from functools import singledispatch
from pprint import pformat
from types import TracebackType
Expand All @@ -11,7 +10,7 @@
from pamqp.common import FieldValue

from .abc import (
MILLISECONDS, ZERO_TIME, AbstractChannel, AbstractIncomingMessage,
MILLISECONDS, AbstractChannel, AbstractIncomingMessage,
AbstractMessage, AbstractProcessContext, DateType, DeliveryMode,
HeadersType, MessageInfo, NoneType,
)
Expand Down Expand Up @@ -58,26 +57,21 @@ def decode_expiration(t: Any) -> Optional[float]:
raise ValueError("Invalid expiration type: %r" % type(t), t)


@decode_expiration.register(time.struct_time)
def decode_expiration_struct_time(t: time.struct_time) -> float:
return (datetime(*t[:7]) - ZERO_TIME).total_seconds()


@decode_expiration.register(str)
def decode_expiration_str(t: str) -> float:
return float(t)
return float(t) / MILLISECONDS


@decode_expiration.register(NoneType) # type: ignore
def decode_expiration_none(_: Any) -> None:
return None


@singledispatch
def encode_timestamp(value: Any) -> Optional[datetime]:
raise ValueError("Invalid timestamp type: %r" % type(value), value)


@encode_timestamp.register(time.struct_time)
def encode_timestamp_struct_time(value: time.struct_time) -> datetime:
return datetime(*value[:6])


@encode_timestamp.register(datetime)
def encode_timestamp_datetime(value: datetime) -> datetime:
return value
Expand All @@ -86,12 +80,12 @@ def encode_timestamp_datetime(value: datetime) -> datetime:
@encode_timestamp.register(float)
@encode_timestamp.register(int)
def encode_timestamp_number(value: Union[int, float]) -> datetime:
return datetime.utcfromtimestamp(value)
return datetime.fromtimestamp(value, tz=timezone.utc)


@encode_timestamp.register(timedelta)
def encode_timestamp_timedelta(value: timedelta) -> datetime:
return datetime.utcnow() + value
return datetime.now(tz=timezone.utc) + value


@encode_timestamp.register(NoneType) # type: ignore
Expand All @@ -109,17 +103,6 @@ def decode_timestamp_datetime(value: datetime) -> datetime:
return value


@decode_timestamp.register(float)
@decode_timestamp.register(int)
def decode_timestamp_number(value: Union[float, int]) -> datetime:
return datetime.utcfromtimestamp(value)


@decode_timestamp.register(time.struct_time)
def decode_timestamp_struct_time(value: time.struct_time) -> datetime:
return datetime(*value[:6])


@decode_timestamp.register(NoneType) # type: ignore
def decode_timestamp_none(_: Any) -> None:
return None
Expand Down Expand Up @@ -373,12 +356,6 @@ def __init__(self, message: DeliveredMessage, no_ack: bool = False):
self.__no_ack = no_ack
self.__processed = False

expiration = None
if message.header.properties.expiration:
expiration = decode_expiration(
message.header.properties.expiration,
)

super().__init__(
body=message.body,
content_type=message.header.properties.content_type,
Expand All @@ -388,7 +365,7 @@ def __init__(self, message: DeliveredMessage, no_ack: bool = False):
priority=message.header.properties.priority,
correlation_id=message.header.properties.correlation_id,
reply_to=message.header.properties.reply_to,
expiration=expiration / 1000.0 if expiration else None,
expiration=decode_expiration(message.header.properties.expiration),
message_id=message.header.properties.message_id,
timestamp=decode_timestamp(message.header.properties.timestamp),
type=message.header.properties.message_type,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_message.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time
from copy import copy
from datetime import datetime
from datetime import datetime, timezone
from typing import List, Tuple

import shortuuid
Expand Down Expand Up @@ -45,7 +45,7 @@ def test_message_info():
redelivered=None,
reply_to="test",
routing_key=None,
timestamp=datetime.utcfromtimestamp(int(time.time())),
timestamp=datetime.fromtimestamp(int(time.time()), tz=timezone.utc),
type="0",
user_id="guest",
)
Expand Down
Loading