HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.52.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Oct 15 06:39:08 EDT 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //var/opt/nydus/ops/primordial/timeutils.py
# -*- coding: utf-8 -*-
from typing import Optional
import datetime

from pytz import utc as UTC

from primordial.constants import CANONICAL_TIMESTRING_FORMAT, CANONICAL_TIMESTRING_FORMAT_NO_MICROSECONDS


def datetime_to_epoch_seconds(in_time: datetime.datetime) -> int:
    """
    Return integer seconds since epoch.

    :param in_time: Datetime
    :returns: Integer seconds since epoch
    """
    return int((in_time - datetime.datetime(1970, 1, 1)).total_seconds())


def iso8601_utc(dt: datetime.datetime, tz: Optional[datetime.tzinfo] = None, microseconds: bool = True) -> str:
    """
    Return a string representation of a datetime in ISO8601 format (YYYY-MM-DDTHH:MM:SS.ssssssZ)
    in the UTC (Z) timezone.

    :param dt: The datetime object.
    :param tz: The timezone to assume when coverting a naive datetime to UTC (Required if `dt` is naive).
    :param microseconds: Whether to include microseconds in the representation. Defaults to `True`.
    :returns: ISO8601-formatted string representation
    :raises ValueError: If the datetime is naive, and no tz is provided
    """
    tformat = CANONICAL_TIMESTRING_FORMAT if microseconds else CANONICAL_TIMESTRING_FORMAT_NO_MICROSECONDS
    if dt.tzinfo is None:
        if tz is None:
            raise ValueError('`tz` param must be provided if datetime object is naive')
        dt = dt.replace(tzinfo=tz)
    if dt.tzinfo is not UTC:
        dt = dt.astimezone(UTC)
    return dt.strftime(tformat)