
    Ng                         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j        dej                  Zd Zdd	Z G 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                 <    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_EPOCHdatetime	timedelta)values    R/var/www/html/ai-engine/env/lib/python3.11/site-packages/proto/datetime_helpers.py_from_microsecondsr   ,   s     *>>>>>    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.
    Ntzinfo)r   replace	utcoffsetstrftime_RFC3339_MICROS)r	   ignore_zones     r
   _to_rfc3339r   9   sG      ?5<3T**U__->->>>>/***r   c                   z     e Zd ZdZdZd Z fdZed             Zd Z	e
d             Zd Ze
d	             Z xZ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   microsecond1Specify only one of 'microsecond' or 'nanosecond'  )pop	TypeErrorr   __new__r   )clsargskwnanosinsts        r
   r    zDatetimeWithNanoseconds.__new__U   sz    |Q''199"" STTT %B} (:t:::r:: :Ar   c                    d|v }d|v }|                     dd          }| j        }|r|rt          d          |r|dz  |d<    t                      j        |i |}|r|j        dz  |_        n|r||_        n||_        |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__s	           r
   r   zDatetimeWithNanoseconds.replace`   s     $r)"b(ff\1--_
 	Q; 	QOPPP 	4 +t 3B}uww+++ 	*#/$6D 	**D  *Dr   c                 $    | j         p	| j        dz  S )z Read-only: nanosecond precision.r   )r   r   )r(   s    r
   r   z"DatetimeWithNanoseconds.nanosecond   s     :4#3d#::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   	   0z{}.{}Z)r   r   strrjustrstripformatr   _RFC3339_NO_FRACTION)r(   r$   s     r
   rfc3339zDatetimeWithNanoseconds.rfc3339   so     q  t$$$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: {}no_fractionr$   r   r0   
   r   r   )_RFC3339_NANOSmatch
ValueErrorr5   patternr   strptimegroupr6   lenintyearmonthdayhourminutesecondtimezoneutc)r!   stamp
with_nanosbarefractionr$   scales          r
   from_rfc3339z$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   )secondsr$   )r   r   r   rJ   rK   r   rC   total_secondsr   r   r   	Timestamp)r(   r%   deltarS   r$   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
        g    .Ar;   )rC   rS   r   rD   rE   rF   rG   rH   rI   r$   r   rJ   rK   )r!   rL   r   rN   s       r
   from_timestamp_pbz)DatetimeWithNanoseconds.from_timestamp_pb   sj     5=3.//!,//sIJHIKK{$(	
 	
 	
 		
r   )__name__
__module____qualname____doc__	__slots__r    r   propertyr   r7   classmethodrQ   rW   rY   __classcell__)r-   s   @r
   r   r   L   s         
 !I           D ; ; X;	K 	K 	K &
 &
 [&
PE E E  
 
 [
 
 
 
 
r   r   )T)r]   calendarr   regoogle.protobufr   fromtimestamprJ   rK   r   r   r6   compileVERBOSEr<   r   r   r    r   r
   <module>ri      s    # "   				 ) ) ) ) ) ) ,,Q0A0EFF
)* 	 J 
? 
? 
?+ + + +&U
 U
 U
 U
 U
h/ U
 U
 U
 U
 U
r   