o
    i{                     @   s4  d dl Z d dlZd dlmZ d dlZd dlZd dlmZmZ d dl	Z	ddl
mZmZmZmZmZmZmZ ejdkZe j ddde jjdZe  dddZd	d
 Zdee j ef dedefddZdd Zdee j ef dedefddZdd Zdd Zdd Zdd Z dd Z!dd Z"eeeeeeee e!e"d 
Z#dS )!    N)BytesIO)DictUnion   )MCS_PER_HOURMCS_PER_MINUTEMCS_PER_SECONDMLS_PER_HOURMLS_PER_MINUTEMLS_PER_SECOND
DAYS_SHIFTnti  tzinfoc                 C   s   t | tjrM| jdur"| t }|jd d |j t t|jd  S t	r;| t
 }|jd d |j t t|jd  S tt|  t t| jd  S | S )zDConverts datetime.datetime object to int timestamp with millisecondsN       )
isinstancedatetimer   epochdayssecondsr   intmicroseconds
is_windowsepoch_naivetimemktime	timetuplemicroseconddataschemadelta r$   b/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastavro/_logical_writers_py.pyprepare_timestamp_millis   s   
r&   r!   r"   returnc                 C   sJ   t | tjr#| jtjjdt }|jd d |j t t	|j
d  S | S )a  Converts datetime.datetime object to int timestamp with milliseconds.

    The local-timestamp-millis logical type represents a timestamp in a local
    timezone, regardless of what specific time zone is considered local, with a
    precision of one millisecond.
    r   r   r   r   )r   r   replacetimezoneutcr   r   r   r   r   r   r    r$   r$   r%   prepare_local_timestamp_millis0   s   	r+   c                 C   s   t | tjrA| jdur| t }|jd d |j t |j S tr3| t	 }|jd d |j t |j S t
t|  t | j S | S )z=Converts datetime.datetime to int timestamp with microsecondsNr   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r$   r$   r%   prepare_timestamp_microsB   s$   
r,   c                 C   sB   t | tjr| jtjjdt }|jd d |j t |j	 S | S )a  Converts datetime.datetime to int timestamp with microseconds

    The local-timestamp-micros logical type represents a timestamp in a local
    timezone, regardless of what specific time zone is considered local, with a
    precision of one microsecond.
    r   r   r   )
r   r   r(   r)   r*   r   r   r   r   r   r    r$   r$   r%   prepare_local_timestamp_microsZ   s   	r-   c                 C   s:   t | tjr|  t S t | trtj|  t S | S )z'Converts datetime.date to int timestamp)r   r   date	toordinalr   strfromisoformatr!   r"   r$   r$   r%   prepare_datel   s
   
r3   c                 C   s   t | tjs| S |dd}|d }|  \}}}t||kr#td|| }|dk r/tdd}|D ]}	|d |	 }q3d| | }| d d }
|rO| }|j|
dd	d
S )z Convert decimal.Decimal to bytesscaler   	precision6The decimal precision is bigger than allowed by schema3Scale provided in schema does not match the decimal
      bigT)	byteordersigned)	r   decimalDecimalgetas_tuplelen
ValueError
bit_lengthto_bytes)r!   r"   r4   r5   signdigitsexpr#   unscaled_datumdigit	bytes_reqr$   r$   r%   prepare_bytes_decimalv   s$   rK   c                 C   s  t | tjs| S |dd}|d }|d }|  \}}}t||kr'td| |kr0td|| }|dkr>|d|  }d}	|D ]}
|	d |
 }	qB|	 d	 }|d
 }|| }d| d	 }d	}t|D ]
}||N }|d	K }qe|d
k rwd	}n|d
 }|d
 dkr|d	7 }t	 }|rd	|> |	 }	||	B }	t|d	 ddD ]}|	d
| ? }|
t|d@ g q| S t|d
 D ]
}|
tdg qt|d	 ddD ]}|	d
| ? }|
t|d@ g q| S )z4Converts decimal.Decimal to fixed length bytes arrayr4   r   sizer5   r6   r7   )r   r8   r   r9         )r   r=   r>   r?   r@   rA   rB   rC   ranger   writebytesgetvalue)r!   r"   r4   rL   r5   rE   rF   rG   r#   rH   rI   bits_reqsize_in_bitsoffset_bitsmaskbitirJ   tmpindexbits_to_writer$   r$   r%   prepare_fixed_decimal   sV   

r]   c                 C   s   t | tjr
t| S | S )zYConverts uuid.UUID to
    string formatted UUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    )r   uuidUUIDr0   r2   r$   r$   r%   prepare_uuid   s   r`   c                 C   s@   t | tjrt| jt | jt  | jt	  t| j
d  S | S )z8Convert datetime.time to int timestamp with millisecondsr   )r   r   r   r   hourr	   minuter
   secondr   r   r2   r$   r$   r%   prepare_time_millis   s   rd   c                 C   s8   t | tjrt| jt | jt  | jt	  | j
 S | S )z8Convert datetime.time to int timestamp with microseconds)r   r   r   r   ra   r   rb   r   rc   r   r   r2   r$   r$   r%   prepare_time_micros   s   re   )
zlong-timestamp-milliszlong-local-timestamp-milliszlong-timestamp-microszlong-local-timestamp-microszint-datezbytes-decimalzfixed-decimalzstring-uuidzint-time-milliszlong-time-micros)$r   r=   ior   osr   typingr   r   r^   constr   r   r   r	   r
   r   r   namer   r)   r*   r   r   r&   r   r+   r,   r-   r3   rK   r]   r`   rd   re   LOGICAL_WRITERSr$   r$   r$   r%   <module>   sX    $



>

