U
    dy                     @   sV   d Z ddlZddlZddlmZmZ ddlmZmZ ddl	m
Z
 dZG dd dZdS )	z3Tools for representing MongoDB internal Timestamps.    N)AnyUnion)_getstate_slots_setstate_slots)utcl        c                   @   s   e Zd ZdZdZeZeZdZ	e
ejef eddddZeedd	d
ZeedddZeedddZedddZeedddZeedddZeedddZeedddZeedddZedddZejdddZdS ) 	Timestampz.MongoDB internal timestamps used in the opLog.)Z__timeZ__inc   N)timeincreturnc                 C   s   t |tjr6| }|dk	r$|| }tt| }t |tsHtdt |tsZtdd|  krntk sxn t	dd|  krtk sn t	d|| _
|| _dS )a  Create a new :class:`Timestamp`.

        This class is only for use with the MongoDB opLog. If you need
        to store a regular timestamp, please use a
        :class:`~datetime.datetime`.

        Raises :class:`TypeError` if `time` is not an instance of
        :class: `int` or :class:`~datetime.datetime`, or `inc` is not
        an instance of :class:`int`. Raises :class:`ValueError` if
        `time` or `inc` is not in [0, 2**32).

        :Parameters:
          - `time`: time in seconds since epoch UTC, or a naive UTC
            :class:`~datetime.datetime`, or an aware
            :class:`~datetime.datetime`
          - `inc`: the incrementing counter
        Nztime must be an instance of intzinc must be an instance of intr   z$time must be contained in [0, 2**32)z#inc must be contained in [0, 2**32))
isinstancedatetime	utcoffsetintcalendartimegm	timetuple	TypeError
UPPERBOUND
ValueError_Timestamp__time_Timestamp__inc)selfr	   r
   offset r   2/tmp/pip-unpacked-wheel-oblwsawz/bson/timestamp.py__init__%   s    

zTimestamp.__init__)r   c                 C   s   | j S )z0Get the time portion of this :class:`Timestamp`.)r   r   r   r   r   r	   H   s    zTimestamp.timec                 C   s   | j S )z/Get the inc portion of this :class:`Timestamp`.)r   r   r   r   r   r
   M   s    zTimestamp.inc)otherr   c                 C   s*   t |tr"| j|jko | j|jkS tS d S N)r   r   r   r	   r   r
   NotImplementedr   r   r   r   r   __eq__R   s    
zTimestamp.__eq__c                 C   s   t | jt | jA S r   )hashr	   r
   r   r   r   r   __hash__X   s    zTimestamp.__hash__c                 C   s
   | |k S r   r   r!   r   r   r   __ne__[   s    zTimestamp.__ne__c                 C   s&   t |tr"| j| jf|j|jfk S tS r   r   r   r	   r
   r    r!   r   r   r   __lt__^   s    
zTimestamp.__lt__c                 C   s&   t |tr"| j| jf|j|jfkS tS r   r&   r!   r   r   r   __le__c   s    
zTimestamp.__le__c                 C   s&   t |tr"| j| jf|j|jfkS tS r   r&   r!   r   r   r   __gt__h   s    
zTimestamp.__gt__c                 C   s&   t |tr"| j| jf|j|jfkS tS r   r&   r!   r   r   r   __ge__m   s    
zTimestamp.__ge__c                 C   s   d| j  d| j dS )Nz
Timestamp(z, ))r   r   r   r   r   r   __repr__r   s    zTimestamp.__repr__c                 C   s   t j | jtS )zReturn a :class:`~datetime.datetime` instance corresponding
        to the time portion of this :class:`Timestamp`.

        The returned datetime's timezone is UTC.
        )r   fromtimestampr   r   r   r   r   r   as_datetimeu   s    zTimestamp.as_datetime)__name__
__module____qualname____doc__	__slots__r   __getstate__r   __setstate__Z_type_markerr   r   r   r   propertyr	   r
   r   boolr"   r$   r%   r'   r(   r)   r*   strr,   r.   r   r   r   r   r      s&   #r   )r2   r   r   typingr   r   Zbson._helpersr   r   Zbson.tz_utilr   r   r   r   r   r   r   <module>   s   