def epoch2iso()

in curator/helpers/date_ops.py [0:0]


def epoch2iso(epoch: int) -> str:
    """
    Return an ISO8601 value for epoch

    :param epoch: An epoch timestamp
    :type epoch: int

    :returns: An ISO8601 timestamp
    :rtype: str
    """
    # Because Python 3.12 now requires non-naive timezone declarations, we must change.
    #
    # ## Example:
    # ## epoch == 1491256800
    # ##
    # ## The old way:
    # ##datetime.utcfromtimestamp(epoch)
    # ##   datetime.datetime(2017, 4, 3, 22, 0).isoformat()
    # ##   Result: 2017-04-03T22:00:00
    # ##
    # ## The new way:
    # ##     datetime.fromtimestamp(epoch, timezone.utc)
    # ##     datetime.datetime(
    # ##         2017, 4, 3, 22, 0, tzinfo=datetime.timezone.utc).isoformat()
    # ##     Result: 2017-04-03T22:00:00+00:00
    # ##
    # ## End Example
    #
    # Note that the +00:00 is appended now where we affirmatively declare the UTC
    # timezone
    #
    # As a result, we will use this function to prune away the timezone if it is +00:00
    # and replace it with Z, which is shorter Zulu notation for UTC (which
    # Elasticsearch uses)
    #
    # We are MANUALLY, FORCEFULLY declaring timezone.utc, so it should ALWAYS be
    # +00:00, but could in theory sometime show up as a Z, so we test for that.

    parts = datetime.fromtimestamp(epoch, timezone.utc).isoformat().split('+')
    if len(parts) == 1:
        if parts[0][-1] == 'Z':
            return parts[0]  # Our ISO8601 already ends with a Z for Zulu/UTC time
        return f'{parts[0]}Z'  # It doesn't end with a Z so we put one there
    if parts[1] == '00:00':
        return f'{parts[0]}Z'  # It doesn't end with a Z so we put one there
    return f'{parts[0]}+{parts[1]}'  # Fallback publishes the +TZ, whatever that was