
    BfJ#                         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                  ZdZ	dZ
 ej        d	ej                  Zd
 Zd Zd Zd Zd Zd Zd ZeZddZ G 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                      t           j                             t           j        j                                      d          S )zEA :meth:`datetime.datetime.utcnow()` alias to allow mocking in tests.)tzNr   )datetimenowtimezoneutcreplace     o/home/panchajanya/Development/work/volatility3/lib/python3.11/site-packages/google/api_core/datetime_helpers.pyutcnowr   +   s3      H$5$9 ::BB$BOOOr   c                 *    t          |           }|dz  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micross     r   to_millisecondsr   0   s     U##FT>r   c                 <    t           t          j        |           z   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   s    r   from_microsecondsr   =   s     *>>>>>r   c                    | j         s%|                     t          j        j                  } |                     t          j        j                  } t          t          j        | 	                                          dz            | j
        z   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   s    r   r   r   J   so     < <X%6%:;;X.233Exu0011C7885;LLLr   c                 f    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   s    r   from_iso8601_dater)   [   )     %%eZ88==???r   c                 f    t           j                             | d                                          S )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   s    r   from_iso8601_timer-   g   r*   r   c                    t                               |           }|-t          d                    | t           j                            t
          j                            |                    d          t                    }|                    d          }|d}n,dt          |          z
  }t          |          d|z  z  }|dz  }|                    |t
          j        j        	          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   scaler0   s          r   from_rfc3339r?   s   s    *  %%e,,J;BB~- 
 
 	
 $--'')= L ((HCMM!HU+$F8;L;PQQQr   Tc                     |s2| j         +|                     d          |                                 z
  } |                     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_zones     r   
to_rfc3339rE      sG      ?5<3T**U__->->>>>/***r   c                   j    e Zd ZdZdZd Zed             Zd Ze	d             Z
d Ze	d             Zd	S )
DatetimeWithNanosecondszuTrack nanosecond in addition to normal datetime attrs.

    Nanosecond can be passed only as a keyword argument.
    _nanosecondc                     |                     dd          }|dk    rd|v rt          d          |dz  |d<   t          j        j        | g|R i |}|pd|_        |S )N
nanosecondr   r%   z1Specify only one of 'microsecond' or 'nanosecond'r   )pop	TypeErrorr	   __new__rI   )clsargskwr0   insts        r   rN   zDatetimeWithNanoseconds.__new__   sz    |Q''199"" STTT %B} (:t:::r:: :Ar   c                     | j         S )z Read-only: nanosecond precision.rH   )selfs    r   rK   z"DatetimeWithNanoseconds.nanosecond   s     r   c                    | 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   r1   0z{}.{}Z)rI   rE   strrjustrstripr6   rB   r9   )rT   r0   s     r   rfc3339zDatetimeWithNanoseconds.rfc3339   so     q  d###D$%%++As33::3??t}}-ABBEJJJr   c           
         t                               |          }|-t          d                    |t           j                            t
          j                            |                    d          t                    }|                    d          }|d}n'dt          |          z
  }t          |          d|z  z  } | |j        |j        |j        |j        |j        |j        |t
          j        j                  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/   r0   r   r1   r2   rK   r   )r3   r4   r5   r6   r7   r	   r'   r8   r9   r:   r!   yearmonthdayhourminutesecondr   r   )rO   stampr;   barer=   r0   r>   s          r   r?   z$DatetimeWithNanoseconds.from_rfc3339   s    $))%00
;BB>1   
  ))]++-A
 
 ##G,,EEH%EMMRY/EsIJHIKK$(	
 	
 	
 		
r   c                    | j         | n$|                     t          j        j                  }|t
          z
  }t          |                                          }| j        p	| j	        dz  }t          j        ||          S )zReturn a timestamp message.

        Returns:
            (:class:`~google.protobuf.timestamp_pb2.Timestamp`): Timestamp message
        Nr   r   )secondsr0   )r   r   r	   r   r   r   r!   total_secondsrI   r%   r   	Timestamp)rT   rR   deltarf   r0   s        r   timestamp_pbz$DatetimeWithNanoseconds.timestamp_pb  s     {& DX%6%:;; 	
 z!e))++,, ;D$4t$;&weDDDDr   c           
          t          |j        dz            }t          |          } | |j        |j        |j        |j        |j        |j        |j	        t          j        j                  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   r\   )r!   rf   r   r]   r^   r_   r`   ra   rb   r0   r	   r   r   )rO   rc   r   rd   s       r   from_timestamp_pbz)DatetimeWithNanoseconds.from_timestamp_pb  sj     5=3.// ..sIJHIKK{$(	
 	
 	
 		
r   N)__name__
__module____qualname____doc__	__slots__rN   propertyrK   rZ   classmethodr?   rj   rl   r   r   r   rG   rG      s         
 !I       X 	K 	K 	K &
 &
 [&
PE E E  
 
 [
 
 
r   rG   )T)rp   r"   r	   regoogle.protobufr   r   r   r   rC   r9   compileVERBOSEr3   r   r   r   r   r)   r-   r?   from_rfc3339_nanosrE   rG   r   r   r   <module>ry      s]   # "   				 ) ) ) ) ) ) XtQ(2C2GHHH
)* 	 J P P P

 
 

? 
? 
?M M M"	@ 	@ 	@	@ 	@ 	@*R *R *RZ " + + + +&t
 t
 t
 t
 t
h/ t
 t
 t
 t
 t
r   