o
    'h                     @  s   d Z ddlm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 ddlmZ ejdeZejddZd	ZG d
d dZdddZejjjedZejjjedZeeZeeZefdddZefdddZd ddZ dS )!zFTools for representing the BSON datetime type.

.. versionadded:: 4.3
    )annotationsN)AnyUnioncast)DEFAULT_CODEC_OPTIONSCodecOptionsDatetimeConversion)InvalidBSON)utc)tzinfoz(Consider Using CodecOptions(datetime_conversion=DATETIME_AUTO) or MongoClient(datetime_conversion='DATETIME_AUTO')). See: https://www.mongodb.com/docs/languages/python/pymongo-driver/current/data-formats/dates-and-times/#handling-out-of-range-datetimesc                   @  s   e Zd ZdZdZd'ddZd(d	d
Zd)ddZd*ddZd*ddZ	d+ddZ
d+ddZd*ddZd*ddZdZefd,d"d#Zd(d$d%Zd&S )-
DatetimeMSzRepresents a BSON UTC datetime._valuevalueUnion[int, datetime.datetime]c                 C  sd   t |trd|  krdkstd td|| _dS t |tjr)t|| _dS tt| d)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.codec_options.CodecOptions` must be set to 'datetime_ms' or
        'datetime_auto'. See :ref:`handling-out-of-range-datetimes` for
        details.

        :param 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   L/var/www/html/olx_land/venv/lib/python3.10/site-packages/bson/datetime_ms.py__init__+   s   

zDatetimeMS.__init__returnr   c                 C  s
   t | jS N)hashr   r   r   r   r   __hash__F      
zDatetimeMS.__hash__strc                 C  s   t | jd t| j d S )N())r   __name__r"   r   r   r   r   r   __repr__I   s   zDatetimeMS.__repr__otherUnion[DatetimeMS, int]boolc                 C  s
   | j |k S r   r   r   r'   r   r   r   __lt__L   r!   zDatetimeMS.__lt__c                 C  s
   | j |kS r   r   r*   r   r   r   __le__O   r!   zDatetimeMS.__le__r   c                 C  s   t |tr| j|jkS dS )NFr   r   r   r*   r   r   r   __eq__R      
zDatetimeMS.__eq__c                 C  s   t |tr| j|jkS dS )NTr-   r*   r   r   r   __ne__W   r/   zDatetimeMS.__ne__c                 C  s
   | j |kS r   r   r*   r   r   r   __gt__\   r!   zDatetimeMS.__gt__c                 C  s
   | j |kS r   r   r*   r   r   r   __ge___   r!   zDatetimeMS.__ge__	   codec_optionsCodecOptions[Any]datetime.datetimec                 C  s   t tjt| j|S )aU  Create a Python :class:`~datetime.datetime` from this DatetimeMS object.

        :param 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   r4   r   r   r   as_datetimed   s   
zDatetimeMS.as_datetimec                 C  s   | j S r   r   r   r   r   r   __int__p   s   zDatetimeMS.__int__N)r   r   )r   r   )r   r"   )r'   r(   r   r)   )r'   r   r   r)   )r4   r5   r   r6   )r%   
__module____qualname____doc__	__slots__r   r    r&   r+   r,   r.   r0   r1   r2   _type_markerr   r8   r9   r   r   r   r   r   &   s     








r   dtmr6   r   r   c                 C  s8   |   dur| |    } tt|  d | jd  S )z1Convert datetime to milliseconds since epoch UTC.N  )	utcoffsetr   calendartimegm	timetuplemicrosecond)r?   r   r   r   r   t   s    r   tzdatetime.tzinfoc                 C  D   |  t}|d ur|jd |j d |jd  }nd}ttt| S NiQ r@   r   )rA   _MIN_UTCdayssecondsmicrosecondsmax_MIN_UTC_MSrF   deltaoffset_millisr   r   r   _min_datetime_ms   
   
 rS   c                 C  rH   rI   )rA   _MAX_UTCrK   rL   rM   min_MAX_UTC_MSrP   r   r   r   _max_datetime_ms   rT   rX   millisoptsr5   $Union[datetime.datetime, DatetimeMS]c              
   C  s<  |j tjks|j tjks|j tjkr|jpt}|j tjkr*tt|t	| t
|} n|j tjkrFt||   krAt
|ksFt| S  t| S | d d d }| | d }|d }z!|jrpttj||d }|jrm||}|W S ttj||d W S  ty } z
t| dt |d}~ww |j tjkrt| S td)z1Convert milliseconds since epoch UTC to datetime.r@   )rL   rM    Nz<datetime_conversion must be an element of DatetimeConversion)datetime_conversionr   DATETIMEDATETIME_CLAMPDATETIME_AUTOr   r
   rN   rS   rV   rX   r   tz_awareEPOCH_AWAREr   	timedelta
astimezoneEPOCH_NAIVEArithmeticErrorr	   _DATETIME_ERROR_SUGGESTIONDATETIME_MS
ValueError)rY   rZ   rF   diffrL   microsdterrr   r   r   r7      s8   

r7   )r?   r6   r   r   )rF   rG   r   r   )rY   r   rZ   r5   r   r[   )!r<   
__future__r   rB   r   typingr   r   r   bson.codec_optionsr   r   r   bson.errorsr	   bson.tz_utilr
   fromtimestamprb   replacere   rg   r   r   rV   rJ   rN   rU   rO   rW   rS   rX   r7   r   r   r   r   <module>   s*   
N		