Module peprock.datetime.awareness

Timezone awareness helpers.

See https://docs.python.org/3/library/datetime.html#determining-if-an-object-is-aware-or-naive for definitions.

Examples

>>> naive = datetime.datetime.now()
>>> is_naive(naive)
True
>>> is_aware(naive)
False
>>> aware = ensure_aware(naive, assumed_tz=datetime.timezone.utc)
>>> is_naive(aware)
False
>>> is_aware(aware)
True

Functions

def ensure_aware(arg: datetime.datetime, /, *, assumed_tz: datetime.tzinfo | None = None, target_tz: datetime.tzinfo | None = None) ‑> datetime.datetime

Ensure timezone awareness of arg.

def is_aware(arg: datetime.date | datetime.time | datetime.datetime, /) ‑> bool

Determine if arg is timezone aware and return a bool.

def is_naive(arg: datetime.date | datetime.time | datetime.datetime, /) ‑> bool

Determine if arg is timezone naive and return a bool.

Classes

class EnsureAwareError (arg: datetime.datetime, /, *, assumed_tz: datetime.tzinfo | None = None, target_tz: datetime.tzinfo | None = None)

Unable to ensure awareness.

Initialize EnsureAwareError with arguments used in ensure_aware().

Expand source code
class EnsureAwareError(ValueError):
    """Unable to ensure awareness."""

    def __init__(
        self: EnsureAwareError,
        arg: datetime.datetime,
        /,
        *,
        assumed_tz: datetime.tzinfo | None = None,
        target_tz: datetime.tzinfo | None = None,
    ) -> None:
        """Initialize EnsureAwareError with arguments used in ensure_aware()."""
        super().__init__(
            f"Unable to ensure awareness of '{arg!r}' using "
            f"'{assumed_tz=}' and '{target_tz=}'",
        )

Ancestors

  • builtins.ValueError
  • builtins.Exception
  • builtins.BaseException