o
    ýÞhË  ã                   @   sx   d Z ddlZddlZddlZddlmZ ej dejj¡Z	dZ
dZe dej¡Zdd„ Zdd
d„ZG dd„ dejƒZdS )zHelpers for :mod:`datetime`.é    N)Útimestamp_pb2z%Y-%m-%dT%H:%M:%S.%fZz%Y-%m-%dT%H:%M:%Sah  
    (?P<no_fraction>
        \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}  # YYYY-MM-DDTHH:MM:SS
    )
    (                                        # Optional decimal part
     \.                                      # decimal point
     (?P<nanos>\d{1,9})                      # nanoseconds, maybe truncated
    )?
    Z                                        # Zulu
c                 C   s   t tj| d� S )a  Convert timestamp in microseconds since the unix epoch to datetime.

    Args:
        value (float): The timestamp to convert, in microseconds.

    Returns:
        datetime.datetime: The datetime object equivalent to the timestamp in
            UTC.
    )Úmicroseconds)Ú
_UTC_EPOCHÚdatetimeÚ	timedelta)Úvalue© r   úV/var/www/html/premium_crap/venv/lib/python3.10/site-packages/proto/datetime_helpers.pyÚ_from_microseconds,   s   
r
   Tc                 C   s,   |s| j dur| jdd�|  ¡  } |  t¡S )a‡  Convert a datetime to an RFC3339 timestamp string.

    Args:
        value (datetime.datetime):
            The datetime object to be converted to a string.
        ignore_zone (bool): If True, then the timezone (if any) of the
            datetime object is ignored and the datetime is treated as UTC.

    Returns:
        str: The RFC3339 formatted string representing the datetime.
    N©Útzinfo)r   ÚreplaceÚ	utcoffsetÚstrftimeÚ_RFC3339_MICROS)r   Úignore_zoner   r   r	   Ú_to_rfc33399   s   
r   c                       s`   e Zd ZdZdZdd„ Z‡ fdd„Zedd„ ƒZd	d
„ Z	e
dd„ ƒZdd„ Ze
dd„ ƒZ‡  ZS )ÚDatetimeWithNanosecondszuTrack nanosecond in addition to normal datetime attrs.

    Nanosecond can be passed only as a keyword argument.
    )Ú_nanosecondc                 O   sX   |  dd¡}|dkrd|v rtdƒ‚|d |d< tjj| g|¢R i |¤Ž}|p(d|_|S )NÚ
nanosecondr   Úmicrosecondú1Specify only one of 'microsecond' or 'nanosecond'éè  )ÚpopÚ	TypeErrorr   Ú__new__r   )ÚclsÚargsÚkwÚnanosÚinstr   r   r	   r   U   s   
zDatetimeWithNanoseconds.__new__c                    s€   d|v }d|v }|  dd¡}| j}|r|rtdƒ‚|r!|d |d< tƒ j|i |¤Ž}|r4|jd |_|S |r;||_|S ||_|S )a@  Return a date with the same value, except for those parameters given
        new values by whichever keyword arguments are specified. For example,
        if d == date(2002, 12, 31), then
        d.replace(day=26) == date(2002, 12, 26).
        NOTE: nanosecond and microsecond are mutually exclusive arguments.
        r   r   r   r   r   )r   r   r   Úsuperr   r   r   )Úselfr   r   Úms_providedÚns_providedÚprovided_nsÚ
prev_nanosr    ©Ú	__class__r   r	   r   `   s"   ùþzDatetimeWithNanoseconds.replacec                 C   s   | j p| jd S )z Read-only: nanosecond precision.r   )r   r   )r"   r   r   r	   r   ‚   s   z"DatetimeWithNanoseconds.nanosecondc                 C   s<   | j dkr	t| ƒS t| j ƒ dd¡ d¡}d |  t¡|¡S )z€Return an RFC3339-compliant timestamp.

        Returns:
            (str): Timestamp string according to RFC3339 spec.
        r   é	   Ú0z{}.{}Z)r   r   ÚstrÚrjustÚrstripÚformatr   Ú_RFC3339_NO_FRACTION)r"   r   r   r   r	   Úrfc3339‡   s   
zDatetimeWithNanoseconds.rfc3339c              
   C   s”   t  |¡}|du rtd |t j¡ƒ‚tj | d¡t¡}| d¡}|du r(d}ndt	|ƒ }t
|ƒd|  }| |j|j|j|j|j|j|tjjd�S )	ak  Parse RFC3339-compliant timestamp, preserving nanoseconds.

        Args:
            stamp (str): RFC3339 stamp, with up to nanosecond precision

        Returns:
            :class:`DatetimeWithNanoseconds`:
                an instance matching the timestamp string

        Raises:
            ValueError: if `stamp` does not match the expected format
        Nz)Timestamp: {}, does not match pattern: {}Úno_fractionr   r   r)   é
   ©r   r   )Ú_RFC3339_NANOSÚmatchÚ
ValueErrorr.   Úpatternr   ÚstrptimeÚgroupr/   ÚlenÚintÚyearÚmonthÚdayÚhourÚminuteÚsecondÚtimezoneÚutc)r   ÚstampÚ
with_nanosÚbareÚfractionr   Úscaler   r   r	   Úfrom_rfc3339’   s2   
ÿÿ
ÿ
øz$DatetimeWithNanoseconds.from_rfc3339c                 C   sP   | j dur| n| jtjjd�}|t }t| ¡ ƒ}| jp | j	d }t
j||d�S )z‰Return a timestamp message.

        Returns:
            (:class:`~google.protobuf.timestamp_pb2.Timestamp`): Timestamp message
        Nr   r   )Úsecondsr   )r   r   r   rB   rC   r   r;   Útotal_secondsr   r   r   Ú	Timestamp)r"   r    ÚdeltarJ   r   r   r   r	   Útimestamp_pb»   s   
ÿýz$DatetimeWithNanoseconds.timestamp_pbc              
   C   s@   t |jd ƒ}t|ƒ}| |j|j|j|j|j|j|j	t
jjd�S )a&  Parse RFC3339-compliant timestamp, preserving nanoseconds.

        Args:
            stamp (:class:`~google.protobuf.timestamp_pb2.Timestamp`): timestamp message

        Returns:
            :class:`DatetimeWithNanoseconds`:
                an instance matching the timestamp message
        g    €„.Ar3   )r;   rJ   r
   r<   r=   r>   r?   r@   rA   r   r   rB   rC   )r   rD   r   rF   r   r   r	   Úfrom_timestamp_pbË   s   øz)DatetimeWithNanoseconds.from_timestamp_pb)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   Úpropertyr   r0   ÚclassmethodrI   rN   rO   Ú__classcell__r   r   r'   r	   r   L   s    "

(r   )T)rS   Úcalendarr   ÚreÚgoogle.protobufr   ÚfromtimestamprB   rC   r   r   r/   ÚcompileÚVERBOSEr4   r
   r   r   r   r   r   r	   Ú<module>   s   
õ
