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

Fix: Change To Use Timezone Aware UTC datetime #2679

Merged
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: 1 addition & 1 deletion aries_cloudagent/messaging/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def epoch_to_str(epoch: int) -> str:

def datetime_now() -> datetime:
"""Timestamp in UTC."""
return datetime.utcnow().replace(tzinfo=timezone.utc)
return datetime.now(tz=timezone.utc)


def time_now() -> str:
Expand Down
4 changes: 2 additions & 2 deletions aries_cloudagent/multitenant/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Manager for multitenancy."""

from abc import ABC, abstractmethod
from datetime import datetime
from datetime import datetime, timezone
import logging
from typing import Iterable, List, Optional, cast, Tuple

Expand Down Expand Up @@ -297,7 +297,7 @@ async def create_auth_token(
str: JWT auth token

"""
iat = int(round(datetime.utcnow().timestamp()))
iat = int(round(datetime.now(tz=timezone.utc).timestamp()))

jwt_payload = {"wallet_id": wallet_record.wallet_id, "iat": iat}
jwt_secret = self._profile.settings.get("multitenant.jwt_secret")
Expand Down
10 changes: 5 additions & 5 deletions aries_cloudagent/multitenant/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timezone

from unittest import IsolatedAsyncioTestCase
from aries_cloudagent.tests import mock
Expand Down Expand Up @@ -374,15 +374,15 @@ async def test_create_auth_token_managed(self):
save=mock.CoroutineMock(),
)

utc_now = datetime(2020, 1, 1, 0, 0, 0)
utc_now = datetime(2020, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
iat = int(round(utc_now.timestamp()))

expected_token = jwt.encode(
{"wallet_id": wallet_record.wallet_id, "iat": iat}, "very_secret_jwt"
)

with mock.patch.object(test_module, "datetime") as mock_datetime:
mock_datetime.utcnow.return_value = utc_now
mock_datetime.now.return_value = utc_now
token = await self.manager.create_auth_token(wallet_record)

assert wallet_record.jwt_iat == iat
Expand All @@ -398,7 +398,7 @@ async def test_create_auth_token_unmanaged(self):
save=mock.CoroutineMock(),
)

utc_now = datetime(2020, 1, 1, 0, 0, 0)
utc_now = datetime(2020, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
iat = int(round(utc_now.timestamp()))

expected_token = jwt.encode(
Expand All @@ -411,7 +411,7 @@ async def test_create_auth_token_unmanaged(self):
)

with mock.patch.object(test_module, "datetime") as mock_datetime:
mock_datetime.utcnow.return_value = utc_now
mock_datetime.now.return_value = utc_now
token = await self.manager.create_auth_token(wallet_record, "test_key")

assert wallet_record.jwt_iat == iat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class TestConfig:
routing_keys=[],
service_endpoint=test_endpoint,
)
NOW_8601 = datetime.utcnow().replace(tzinfo=timezone.utc).isoformat(" ", "seconds")
NOW_8601 = datetime.now(tz=timezone.utc).isoformat(" ", "seconds")
TEST_INVI_MESSAGE_TYPE = "out-of-band/1.1/invitation"
NOW_EPOCH = str_to_epoch(NOW_8601)
CD_ID = "GMm4vMw8LLrLJjp81kRRLp:3:CL:12:tag"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ..presentation_request import PresentationRequest


NOW_8601 = datetime.utcnow().replace(tzinfo=timezone.utc).isoformat(" ", "seconds")
NOW_8601 = datetime.now(tz=timezone.utc).isoformat(" ", "seconds")
NOW_EPOCH = str_to_epoch(NOW_8601)
CD_ID = "GMm4vMw8LLrLJjp81kRRLp:3:CL:12:tag"
INDY_PROOF_REQ = json.loads(
Expand Down
6 changes: 3 additions & 3 deletions aries_cloudagent/resolver/did_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

import asyncio
from datetime import datetime
from datetime import datetime, timezone
from itertools import chain
import logging
from typing import List, Optional, Sequence, Text, Tuple, Union
Expand Down Expand Up @@ -91,11 +91,11 @@ async def resolve_with_metadata(
self, profile: Profile, did: Union[str, DID], *, timeout: Optional[int] = None
) -> ResolutionResult:
"""Resolve a DID and return the ResolutionResult."""
resolution_start_time = datetime.utcnow()
resolution_start_time = datetime.now(tz=timezone.utc)

resolver, doc = await self._resolve(profile, did, timeout=timeout)

time_now = datetime.utcnow()
time_now = datetime.now(tz=timezone.utc)
duration = int((time_now - resolution_start_time).total_seconds() * 1000)
retrieved_time = time_now.strftime("%Y-%m-%dT%H:%M:%SZ")
resolver_metadata = ResolutionMetadata(
Expand Down