U
    g#                     @   sF  d Z ddlZddlZddlmZmZmZ er8ddlm	Z	 ej
jZzddlZejZW n ek
rj   eZY nX ejejejdddZdeeejejejf ee eej edd	d
Zdeeejejejdf ee eej ee dddZdee eej eej dddZed eejdf dddZejedddZdS )a  This module contains helper functions related to datetime and timestamp conversations.

.. versionchanged:: 20.0
   Previously, the contents of this module were available through the (no longer existing)
   module ``telegram._utils.helpers``.

Warning:
    Contents of this module are intended to be used internally by the library and *not* by the
    user. Changes to this module are not considered breaking changes and may not be documented in
    the changelog.
    N)TYPE_CHECKINGOptionalUnion)Bot)datetimetzinforeturnc                 C   s   |t kr| jt dS || S )zYLocalize the datetime, where UTC is handled depending on whether pytz is available or notr   )DTM_UTCreplaceZlocalize)r   r    r   </tmp/pip-unpacked-wheel-swnnwir2/telegram/_utils/datetime.py	_localize0   s    r   )time_objectreference_timestampr   r   c                 C   s  |dkrt   }nt| tjr&tdt| tjr>||   S t| ttfrT||  S |dkr`t	}t| tj rtjj
|| jp||d}| }| }tj|| }|jdkrt||}|| kr|tjdd7 }t|S t| tjr| jdkrt| |} t| S tdt| j ddS )a   
    Converts a given time object to a float POSIX timestamp.
    Used to convert different time specifications to a common format. The time object
    can be relative (i.e. indicate a time increment, or a time of day) or absolute.
    Objects from the :class:`datetime` module that are timezone-naive will be assumed
    to be in UTC, if ``bot`` is not passed or ``bot.defaults`` is :obj:`None`.

    Args:
        time_object (:obj:`float` | :obj:`datetime.timedelta` |             :obj:`datetime.datetime` | :obj:`datetime.time`):
            Time value to convert. The semantics of this parameter will depend on its type:

            * :obj:`float` will be interpreted as "seconds from :paramref:`reference_t`"
            * :obj:`datetime.timedelta` will be interpreted as
              "time increment from :paramref:`reference_timestamp`"
            * :obj:`datetime.datetime` will be interpreted as an absolute date/time value
            * :obj:`datetime.time` will be interpreted as a specific time of day

        reference_timestamp (:obj:`float`, optional): POSIX timestamp that indicates the absolute
            time from which relative calculations are to be performed (e.g. when
            :paramref:`time_object` is given as an :obj:`int`, indicating "seconds from
            :paramref:`reference_time`"). Defaults to now (the time at which this function is
            called).

            If :paramref:`time_object` is given as an absolute representation of date & time (i.e.
            a :obj:`datetime.datetime` object), :paramref:`reference_timestamp` is not relevant
            and so its value should be :obj:`None`. If this is not the case, a :exc:`ValueError`
            will be raised.
        tzinfo (:class:`datetime.tzinfo`, optional): If :paramref:`time_object` is a naive object
            from the :mod:`datetime` module, it will be interpreted as this timezone. Defaults to
            ``pytz.utc``, if available, and :attr:`datetime.timezone.utc` otherwise.

            Note:
                Only to be used by ``telegram.ext``.

    Returns:
        :obj:`float` | :obj:`None`:
            The return value depends on the type of argument :paramref:`time_object`.
            If :paramref:`time_object` is given as a time increment (i.e. as a :obj:`int`,
            :obj:`float` or :obj:`datetime.timedelta`), then the return value will be
            :paramref:`reference_timestamp` + :paramref:`time_object`.

            Else if it is given as an absolute date/time value (i.e. a :obj:`datetime.datetime`
            object), the equivalent value as a POSIX timestamp will be returned.

            Finally, if it is a time of the day without date (i.e. a :obj:`datetime.time`
            object), the return value is the nearest future occurrence of that time of day.

    Raises:
        TypeError: If :paramref:`time_object` s type is not one of those described above.
        ValueError: If :paramref:`time_object` is a :obj:`datetime.datetime` and
            :paramref:`reference_timestamp` is not :obj:`None`.
    NzAt is an (absolute) datetime while reference_timestamp is not Nonetz   )dayszUnable to convert z object to timestamp)time
isinstancedtmr   
ValueError	timedeltatotal_secondsintfloatUTCfromtimestampr   datetimetzcombiner   _datetime_to_float_timestamp	TypeErrortype__name__)r   r   r   Zreference_dtZreference_dateZreference_timeZaware_datetimer   r   r   to_float_timestamp7   s8    :
 



r&   )dt_objr   r   r   c                 C   s   | dk	rt t| ||S dS )z
    Wrapper over :func:`to_float_timestamp` which returns an integer (the float value truncated
    down to the nearest integer).

    See the documentation for :func:`to_float_timestamp` for more details.
    N)r   r&   )r'   r   r   r   r   r   to_timestamp   s    r(   )unixtimer   r   c                 C   s(   | dkrdS t jj| |dkr tn|dS )ai  
    Converts an (integer) unix timestamp to a timezone aware datetime object.
    :obj:`None` s are left alone (i.e. ``from_timestamp(None)`` is :obj:`None`).

    Args:
        unixtime (:obj:`int`): Integer POSIX timestamp.
        tzinfo (:obj:`datetime.tzinfo`, optional): The timezone to which the timestamp is to be
            converted to. Defaults to :obj:`None`, in which case the returned datetime object will
            be timezone aware and in UTC.

    Returns:
        Timezone aware equivalent :obj:`datetime.datetime` value if :paramref:`unixtime` is not
        :obj:`None`; else :obj:`None`.
    Nr   )r   r   r   r   )r)   r   r   r   r   from_timestamp   s    r*   r   )botr   c                 C   s(   | dkrdS t | dr$| jr$| jjS dS )z
    Extracts the timezone info from the default values of the bot.
    If the bot has no default values, :obj:`None` is returned.
    Ndefaults)hasattrr,   r   )r+   r   r   r   extract_tzinfo_from_defaults   s
    r.   )r'   r   c                 C   s"   | j dkr| jtjjd} |  S )z
    Converts a datetime object to a float timestamp (with sub-second precision).
    If the datetime object is timezone-naive, it is assumed to be in UTC.
    Nr	   )r   r   r   timezoneutc	timestamp)r'   r   r   r   r"      s    
r"   )NN)NN)N)__doc__r   r   r   typingr   r   r   Ztelegramr   r/   r0   r
   Zpytzr   ImportErrorr   r   r   r   r&   r   r(   r*   r.   r"   r   r   r   r   <module>   sH   

	  `   