
    FCf                       d Z ddlm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 ddlmZ ej                  j%                  de      Zej)                  d      Zd	Z G d
 d      Z ej0                  d      ej2                  j"                  fdd       Z ej0                  d      ej2                  j"                  fdd       Z	 	 	 	 	 	 ddZddZy)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://pymongo.readthedocs.io/en/stable/examples/datetimes.html#handling-out-of-range-datetimesc                  z    e Zd ZdZdZddZddZddZddZddZ	ddZ
dd	Zdd
ZddZdZef	 	 	 ddZddZy)
DatetimeMSzRepresents a BSON UTC datetime._valuec                    t        |t              r*d|cxk  rdk  st        d       t        d      || _        yt        |t        j                        rt        |      | _        y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)selfvalues     P/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/bson/datetime_ms.py__init__zDatetimeMS.__init__,   sr    $ eS!22#$NOO 3#$NOODKx001-e4DKtE{m+NOPP    c                ,    t        | j                        S N)hashr   r   s    r   __hash__zDatetimeMS.__hash__G   s    DKK  r   c                d    t        |       j                  dz   t        | j                        z   dz   S )N())r   __name__strr   r"   s    r   __repr__zDatetimeMS.__repr__J   s*    Dz""S(3t{{+;;cAAr   c                     | j                   |k  S r    r   r   others     r   __lt__zDatetimeMS.__lt__M       {{U""r   c                     | j                   |k  S r    r   r+   s     r   __le__zDatetimeMS.__le__P       {{e##r   c                V    t        |t              r| j                  |j                  k(  S y)NFr   r   r   r+   s     r   __eq__zDatetimeMS.__eq__S   s"    eZ(;;%,,..r   c                V    t        |t              r| j                  |j                  k7  S y)NTr3   r+   s     r   __ne__zDatetimeMS.__ne__X   s"    eZ(;;%,,..r   c                     | j                   |kD  S r    r   r+   s     r   __gt__zDatetimeMS.__gt__]   r.   r   c                     | j                   |k\  S r    r   r+   s     r   __ge__zDatetimeMS.__ge__`   r1   r   	   c                ^    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   codec_optionss     r   as_datetimezDatetimeMS.as_datetimee   s#     H%%':4;;'VWWr   c                    | j                   S r    r   r"   s    r   __int__zDatetimeMS.__int__q   s    {{r   N)r   zUnion[int, datetime.datetime])returnr   )rB   r(   )r,   zUnion[DatetimeMS, int]rB   bool)r,   r   rB   rC   )r>   CodecOptions[Any]rB   datetime.datetime)r'   
__module____qualname____doc__	__slots__r   r#   r)   r-   r0   r4   r6   r8   r:   _type_markerr   r?   rA    r   r   r   r   '   sc    )IQ6!B#$

#$ L 2G
X.
X	
Xr   r   )maxsizec                h    t        t        j                  j                  j                  |             S Nr   )r   r   minreplacetzs    r   _min_datetime_msrS   x   '    x0044<<B<GHHr   c                h    t        t        j                  j                  j                  |             S rN   )r   r   maxrP   rQ   s    r   _max_datetime_msrW   }   rT   r   c           	        |j                   t        j                  k(  s;|j                   t        j                  k(  s|j                   t        j                  k(  r?|j
                  xs t        j                  j                  }|j                   t        j                  k(  r)t        t        |      t        | t        |                  } nQ|j                   t        j                  k(  r4t        |      | cxk  rt        |      k  st        |       S  t        |       S | dz  dz   dz  }| |z
  dz  }|dz  }	 |j                  r=t        t        j                   ||      z   }|j
                  r|j#                  |      }|S t$        t        j                   ||      z   S |j                   t        j,                  k(  rt        |       S t/        d      # t&        $ r}t)        | dt*               |d}~ww xY w)z1Convert milliseconds since epoch UTC to datetime.  )secondsmicroseconds Nz<datetime_conversion must be an element of DatetimeConversion)datetime_conversionr	   DATETIMEDATETIME_CLAMPDATETIME_AUTOr   r   timezoner   rV   rS   rO   rW   r   tz_awareEPOCH_AWARE	timedelta
astimezoneEPOCH_NAIVEArithmeticErrorr
   _DATETIME_ERROR_SUGGESTIONDATETIME_MS
ValueError)millisoptsrR   diffrZ   microsdterrs           r   r=   r=      s   
 	  $6$?$??##'9'H'HH##'9'G'GG[[1H--11##'9'H'HH)"-s6;KB;O/PQF%%);)I)II$R(FJ6Fr6JJ!&)) K!&))$$&$.D=T)		N}} 8#5#5gTZ#[[;;r*B	"X%7%7V\%]]] 
	!	!%7%C%C	C&!!WXX  	NQ'A&BCD#M	Ns   1AG :G 	G.G))G.c                    | j                         | | j                         z
  } t        t        j                  | j	                               dz  | j
                  dz  z         S )z1Convert datetime to milliseconds since epoch UTC.rY   )	utcoffsetr   calendartimegm	timetuplemicrosecond)dtms    r   r   r      sK    
}}"CMMO#xs}}/$6D9PPQQr   )rR   zdatetime.timezonerB   r   )rk   r   rl   rD   rB   z$Union[datetime.datetime, DatetimeMS])rw   rE   rB   r   )rH   
__future__r   rs   r   	functoolstypingr   r   r   bson.codec_optionsr   r   r	   bson.errorsr
   bson.tz_utilr   fromtimestamprc   rP   rf   rh   r   	lru_cachera   rS   rW   r=   r   rK   r   r   <module>r      s    #    # # V V # --a5!!!.m K Kb T"-5->->-B-B I #I T"-5->->-B-B I #I"Y"Y("Y)"YJRr   