U
    d                     @   s   d Z ddlZddlZddlZddlmZmZmZ ddlm	Z	m
Z
mZ ddlmZ ejdeZejddZG dd dZejdd	ejjfejed
ddZejdd	ejjfejed
ddZee
eejef dddZejedddZdS )zFTools for representing the BSON datetime type.

.. versionadded:: 4.3
    N)AnyUnioncast)DEFAULT_CODEC_OPTIONSCodecOptionsDatetimeConversion)utctzinfoc                   @   s   e Zd ZdZdZeeejf dddZedddZ	e
dd	d
Zed ef edddZed ef edddZeedddZeedddZed ef edddZed ef edddZdZefeejdddZedddZdS )
DatetimeMSzRepresents a BSON UTC datetime._value)valuec                 C   s^   t |tr0d|  krdks(n td|| _n*t |tjrHt|| _ntt| ddS )a  Represents a BSON UTC datetime.

        BSON UTC datetimes are defined as an int64 of milliseconds since the
        Unix epoch. The principal use of DatetimeMS is to represent
        datetimes outside the range of the Python builtin
        :class:`~datetime.datetime` class when
        encoding/decoding BSON.

        To decode UTC datetimes as a ``DatetimeMS``, `datetime_conversion` in
        :class:`~bson.CodecOptions` must be set to 'datetime_ms' or
        'datetime_auto'. See :ref:`handling-out-of-range-datetimes` for
        details.

        :Parameters:
          - `value`: An instance of :class:`datetime.datetime` to be
            represented as milliseconds since the Unix epoch, or int of
            milliseconds since the Unix epoch.
        l         l    z(Must be a 64-bit integer of millisecondsz# is not a valid type for DatetimeMSN)
isinstanceintOverflowErrorr   datetime_datetime_to_millis	TypeErrortype)selfr    r   4/tmp/pip-unpacked-wheel-oblwsawz/bson/datetime_ms.py__init__%   s    
zDatetimeMS.__init__)returnc                 C   s
   t | jS N)hashr   r   r   r   r   __hash__A   s    zDatetimeMS.__hash__c                 C   s   t | jd t| j d S )N())r   __name__strr   r   r   r   r   __repr__D   s    zDatetimeMS.__repr__)otherr   c                 C   s
   | j |k S r   r   r   r$   r   r   r   __lt__G   s    zDatetimeMS.__lt__c                 C   s
   | j |kS r   r   r%   r   r   r   __le__J   s    zDatetimeMS.__le__c                 C   s   t |tr| j|jkS dS )NFr   r   r   r%   r   r   r   __eq__M   s    
zDatetimeMS.__eq__c                 C   s   t |tr| j|jkS dS )NTr(   r%   r   r   r   __ne__R   s    
zDatetimeMS.__ne__c                 C   s
   | j |kS r   r   r%   r   r   r   __gt__W   s    zDatetimeMS.__gt__c                 C   s
   | j |kS r   r   r%   r   r   r   __ge__Z   s    zDatetimeMS.__ge__	   )codec_optionsr   c                 C   s   t tjt| j|S )ai  Create a Python :class:`~datetime.datetime` from this DatetimeMS object.

        :Parameters:
          - `codec_options`: A CodecOptions instance for specifying how the
            resulting DatetimeMS object will be formatted using ``tz_aware``
            and ``tz_info``. Defaults to
            :const:`~bson.codec_options.DEFAULT_CODEC_OPTIONS`.
        )r   r   _millis_to_datetimer   )r   r.   r   r   r   as_datetime_   s    	zDatetimeMS.as_datetimec                 C   s   | j S r   r   r   r   r   r   __int__j   s    zDatetimeMS.__int__N)r!   
__module____qualname____doc__	__slots__r   r   r   r   r   r"   r#   boolr&   r'   r   r)   r*   r+   r,   Z_type_markerr   r   r0   r1   r   r   r   r   r       s   r   )maxsize)tzr   c                 C   s   t tjjj| dS Nr	   )r   r   minreplacer8   r   r   r   _min_datetime_msq   s    r=   c                 C   s   t tjjj| dS r9   )r   r   maxr;   r<   r   r   r   _max_datetime_msv   s    r?   )millisoptsr   c                 C   s  |j tjks$|j tjks$|j tjkr|jp0tjj}|j tjkrXt	t
|t| t|} n2|j tjkrt
||   krt|ksn t| S | d d d }| | d }|d }|jrttj||d }|jr||}|S ttj||d S n|j tjkrt| S tddS )z1Convert milliseconds since epoch UTC to datetime.  )secondsmicrosecondsz<datetime_conversion must be an element of DatetimeConversionN)Zdatetime_conversionr   ZDATETIMEZDATETIME_CLAMPZDATETIME_AUTOr
   r   timezoner   r>   r=   r:   r?   r   Ztz_awareEPOCH_AWARE	timedelta
astimezoneEPOCH_NAIVEZDATETIME_MS
ValueError)r@   rA   r8   ZdiffrC   Zmicrosdtr   r   r   r/   {   s0    



r/   )dtmr   c                 C   s8   |   dk	r| |    } tt|  d | jd  S )z1Convert datetime to milliseconds since epoch UTC.NrB   )	utcoffsetr   calendartimegm	timetuplemicrosecond)rL   r   r   r   r      s    r   )r4   rN   r   	functoolstypingr   r   r   Zbson.codec_optionsr   r   r   Zbson.tz_utilr   fromtimestamprF   r;   rI   r   	lru_cacherE   r   r=   r?   r/   r   r   r   r   r   <module>   s   Q

