# -*- coding: utf-8 -*-

"""\
© Copyright. All rights reserved.

"""

from __future__ import unicode_literals

import datetime
try:
    from datetime import timezone
    UTC = timezone.utc
except Exception as _:  # pylint: disable=broad-except
    from datetime import tzinfo, timedelta
    ZERO = timedelta(0)

    class UTC(tzinfo):
        def utcoffset(self, dt):
            return ZERO

        def tzname(self, dt):
            return "UTC"

        def dst(self, dt):
            return ZERO
    UTC = UTC()

from primordial.constants import CANONICAL_TIMESTRING_FORMAT, CANONICAL_TIMESTRING_FORMAT_NO_MICROSECONDS


def datetime_to_epoch_seconds(in_time):
    """\
    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, tz=None, microseconds=True):
    """\
    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.
    :type dt: datetime
    :param tz: The timezone to assume when coverting a naive datetime to UTC (Required if `dt` is naive).
    :type tz: tzinfo
    :param microseconds: Whether to include microseconds in the representation. Defaults to `True`.
    :type microseconds: bool

    :returns: ISO8601-formatted string representation
    :rtype: str

    """
    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)
