Skip to content

verri.dates

Utility functions related to dates and times.

Note

All date-related values used by Verri use UTC as a timezone to ensure independence of the timezone when creating a version.

verri.dates.now() -> dt.datetime

The current timezone-aware date and time, in UTC.

Returns:

Type Description
datetime

The current date and time in UTC, timezone-aware.

Source code in verri/dates.py
def now() -> dt.datetime:
    """
    The current timezone-aware date and time, in UTC.

    :return: The current date and time in UTC, timezone-aware.
    """
    return dt.datetime.now(tz=dt.timezone.utc)

verri.dates.midnight(ts: dt.datetime | None = None) -> dt.datetime

Midnight of the current or a specified date, in UTC.

Parameters:

Name Type Description Default

ts

datetime | None

A specific date to turn into midnight (defaults to the current date). Note that this needs to be a timezone aware datetime, a naive one will be rejected.

None

Returns:

Type Description
datetime

A datetime at UTC midnight.

Source code in verri/dates.py
def midnight(ts: dt.datetime | None = None) -> dt.datetime:
    """
    Midnight of the current or a specified date, in UTC.

    :param ts: A specific date to turn into midnight (defaults to the current date). *Note that this needs to be a
        timezone **aware** datetime, a naive one will be rejected.*
    :return: A `datetime` at UTC midnight.
    """
    ts = ts or now()
    if not ts.tzinfo:
        raise ValueError('a timezone aware datetime is required')

    return ts.replace(hour=0, minute=0, second=0, microsecond=0)

verri.dates.from_ts(ts: int | float) -> dt.datetime

Creates a datetime instance of ts.

Parameters:

Name Type Description Default

ts

int | float

A point in time, measured in seconds since the UNIX epoch.

required

Returns:

Type Description
datetime

A datetime instance in UTC.

Source code in verri/dates.py
def from_ts(ts: int | float) -> dt.datetime:
    """
    Creates a `datetime` instance of *ts*.

    :param ts: A point in time, measured in seconds since the UNIX epoch.
    :return: A `datetime` instance in UTC.
    """
    return dt.datetime.fromtimestamp(ts, tz=dt.timezone.utc)