o
    /iJ#                     @   s   d Z ddlZddlZddlZddlmZ ejdddejjdZdZ	dZ
ed	ejZd
d Zdd Zdd Zdd Zdd Zdd Zdd ZeZdddZG dd dejZdS )zHelpers for :mod:`datetime`.    N)timestamp_pb2i     tzinfoz%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 j jt jjdjddS )zEA :meth:`datetime.datetime.utcnow()` alias to allow mocking in tests.)tzNr   )datetimenowtimezoneutcreplace r   r   T/var/www/passon-env/lib/python3.10/site-packages/google/api_core/datetime_helpers.pyutcnow+   s   r   c                 C   s   t | }|d S )zConvert a zone-aware datetime to milliseconds since the unix epoch.

    Args:
        value (datetime.datetime): The datetime to covert.

    Returns:
        int: Milliseconds since the unix epoch.
      )to_microseconds)valuemicrosr   r   r   to_milliseconds0   s   	r   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_EPOCHr   	timedeltar   r   r   r   from_microseconds=   s   
r   c                 C   s@   | j s| jtjjd} | tjj} tt| 	 d | j
 S )zConvert a datetime to microseconds since the unix epoch.

    Args:
        value (datetime.datetime): The datetime to covert.

    Returns:
        int: Microseconds since the unix epoch.
    r       .A)r   r   r   r	   r
   
astimezoneintcalendartimegm	timetuplemicrosecondr   r   r   r   r   J   s   	r   c                 C      t j | d S )zConvert a ISO8601 date string to a date.

    Args:
        value (str): The ISO8601 date string.

    Returns:
        datetime.date: A date equivalent to the date string.
    z%Y-%m-%d)r   strptimedater   r   r   r   from_iso8601_date[      	r#   c                 C   r    )zConvert a zoneless ISO8601 time string to a time.

    Args:
        value (str): The ISO8601 time string.

    Returns:
        datetime.time: A time equivalent to the time string.
    z%H:%M:%S)r   r!   timer   r   r   r   from_iso8601_timeg   r$   r&   c                 C   s   t | }|du rtd| t jtj|dt}|d}|du r(d}ndt	| }t
|d|  }|d }|j|tjjd	S )
a]  Convert an RFC3339-format timestamp to a native datetime.

    Supported formats include those without fractional seconds, or with
    any fraction up to nanosecond precision.

    .. note::
        Python datetimes do not support nanosecond precision; this function
        therefore truncates such values to microseconds.

    Args:
        value (str): The RFC3339 string to convert.

    Returns:
        datetime.datetime: The datetime object equivalent to the timestamp
        in UTC.

    Raises:
        ValueError: If the timestamp does not match the RFC3339
            regular expression.
    Nz-Timestamp: {!r}, does not match pattern: {!r}no_fractionnanosr   	   
   r   )r   r   )_RFC3339_NANOSmatch
ValueErrorformatpatternr   r!   group_RFC3339_NO_FRACTIONlenr   r   r	   r
   )r   
with_nanosbare_secondsfractionr   scaler(   r   r   r   from_rfc3339s   s"   


r7   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.
    Nr   )r   r   	utcoffsetstrftime_RFC3339_MICROS)r   ignore_zoner   r   r   
to_rfc3339   s   
r<   c                   @   sP   e Zd ZdZdZdd Zedd Zdd Ze	d	d
 Z
dd Ze	dd Zd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   r   z1Specify only one of 'microsecond' or 'nanosecond'r   )pop	TypeErrorr   __new__r?   )clsargskwr(   instr   r   r   rC      s   
zDatetimeWithNanoseconds.__new__c                 C   s   | j S )z Read-only: nanosecond precision.r>   )selfr   r   r   r@      s   z"DatetimeWithNanoseconds.nanosecondc                 C   s<   | j dkr	t| S t| j ddd}d| t|S )zReturn an RFC3339-compliant timestamp.

        Returns:
            (str): Timestamp string according to RFC3339 spec.
        r   r)   0z{}.{}Z)r?   r<   strrjustrstripr.   r9   r1   )rH   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: {}r'   r(   r   r)   r*   r@   r   )r+   r,   r-   r.   r/   r   r!   r0   r1   r2   r   yearmonthdayhourminutesecondr	   r
   )rD   stampr3   barer5   r(   r6   r   r   r   r7      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 )zReturn a timestamp message.

        Returns:
            (:class:`~google.protobuf.timestamp_pb2.Timestamp`): Timestamp message
        Nr   r   )secondsr(   )r   r   r   r	   r
   r   r   total_secondsr?   r   r   	Timestamp)rH   rG   deltarW   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
        r   rN   )r   rW   r   rO   rP   rQ   rR   rS   rT   r(   r   r	   r
   )rD   rU   r   rV   r   r   r   from_timestamp_pb  s   z)DatetimeWithNanoseconds.from_timestamp_pbN)__name__
__module____qualname____doc__	__slots__rC   propertyr@   rM   classmethodr7   r[   r\   r   r   r   r   r=      s    

(r=   )T)r`   r   r   regoogle.protobufr   r	   r
   r   r:   r1   compileVERBOSEr+   r   r   r   r   r#   r&   r7   from_rfc3339_nanosr<   r=   r   r   r   r   <module>   s,   
-
