Skip to content

Commit

Permalink
Merge pull request #379 from lsst-sqre/tickets/DM-48838a
Browse files Browse the repository at this point in the history
DM-48838: Use current_datetime in fewer places
  • Loading branch information
rra authored Feb 11, 2025
2 parents 6eb0b5b + beae4b1 commit 3a692fe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
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

0 comments on commit 3a692fe

Please sign in to comment.