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

DM-48838: Use current_datetime in fewer places #379

Merged
merged 1 commit into from
Feb 11, 2025
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
11 changes: 3 additions & 8 deletions safir/src/safir/asyncio/_multiqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

import asyncio
from collections.abc import AsyncGenerator
from datetime import timedelta
from datetime import UTC, datetime, timedelta
from types import EllipsisType
from typing import Generic, TypeVar

from safir.datetime import current_datetime

#: Type variable of objects being stored in `AsyncMultiQueue`.
T = TypeVar("T")

Expand Down Expand Up @@ -87,10 +85,7 @@ def aiter_from(
TimeoutError
Raised when the timeout is reached.
"""
if timeout:
end_time = current_datetime(microseconds=True) + timeout
else:
end_time = None
end_time = datetime.now(tz=UTC) + timeout if timeout else None

# Grab a reference to the current contents so that the iterator
# detaches from the contents on clear.
Expand Down Expand Up @@ -118,7 +113,7 @@ async def iterator() -> AsyncGenerator[T]:
elif contents and contents[-1] is Ellipsis:
return
if end_time:
now = current_datetime(microseconds=True)
now = datetime.now(tz=UTC)
timeout_left = (end_time - now).total_seconds()
async with asyncio.timeout(timeout_left):
await trigger.wait()
Expand Down
9 changes: 4 additions & 5 deletions safir/src/safir/sentry/_helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Sentry helpers."""

from datetime import timedelta
from __future__ import annotations

from datetime import UTC, datetime, timedelta

from sentry_sdk.tracing import Span
from sentry_sdk.types import Event, Hint

from safir.datetime import current_datetime

from ._exceptions import SentryException

__all__ = [
Expand All @@ -20,10 +20,9 @@
def duration(span: Span) -> timedelta:
"""Return the time spent in a span (to the present if not finished)."""
if span.timestamp is None:
timestamp = current_datetime(microseconds=True)
timestamp = datetime.now(tz=UTC)
else:
timestamp = span.timestamp

return timestamp - span.start_timestamp


Expand Down
9 changes: 3 additions & 6 deletions safir/src/safir/slack/blockkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from __future__ import annotations

from abc import ABCMeta, abstractmethod
from datetime import datetime
from datetime import UTC, datetime
from typing import Any, ClassVar, Self

from httpx import HTTPError, HTTPStatusError
from pydantic import BaseModel, field_validator

from safir.datetime import current_datetime, format_datetime_for_logging
from safir.datetime import format_datetime_for_logging

__all__ = [
"SlackBaseBlock",
Expand Down Expand Up @@ -275,10 +275,7 @@ def __init__(
# fix. See https://github.com/python/cpython/issues/76877.
self.message = message
self.user = user
if failed_at:
self.failed_at = failed_at
else:
self.failed_at = current_datetime(microseconds=True)
self.failed_at = failed_at if failed_at else datetime.now(tz=UTC)

def __str__(self) -> str:
return self.message
Expand Down