o
    TdH                     @   s  d Z ddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ ddlmZmZmZmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ ejd ZZeeejZej fd Z!dd Z"dd Z#dd Z$dd Z%dd Z&dsddZ'dsddZ(dd Z)dd Z*eeg d Z+eeg d!Z,eeg d"Z-eeg d#Z.eej/eej d$d% Z0eej1ej d&d' Z2eej3ej d(d) Z4ee5ej d*d+ Z6d,d- Z7eej8ge!R  eej9ge!R  d.d/ Z:eej;ge!R  eej<ge!R  d0d1 Z=d2d3 Z>eej?ej ej@eejAej ej@eej?ej ejBeejAej ejBd4d5 ZCeej?ej@ej eejAej@ej eej?ejBej eejAejBej d6d7 ZDeejEej ej@eejFej ej@eejGej ej@eejHej ej@eejEej ejBeejFej ejBeejGej ejBeejHej ejBd8d9 ZIeejEge!R  eejFge!R  d:d; ZJeejGge!R  d<d= ZKd>d? ZLd@dA ZMdBdC ZNeMdDe	jOZPeMdEe	jQZReNdFZSeNdGZTeNdHZUeNdIZVejWePfejXeRfejYeSfejZeTfej[eUfej\eVffD ]\Z]Z^ee]ge!R  e^ qdJdK Z_dLdM Z`dNdO ZadPdQ ZbdRdS ZcecdTZdecdUZeeej8ej/ej eej9ej/ej dVdW Zfeej8ej ej/eej9ej ej/dXdY Zgeej;ej/ej eej<ej/ej dZd[ Zheej;ej/ej/d\d] Zid^d_ ZjejdDZkejdEZlejdFZmejdGZnejdHZoejdIZpejWekfejXelfejYemfejZenfej[eofej\epffD ]\ZqZ^eeqgej/gd R  e^ qd`da ZrerdbZserdcZtddde ZueudbZveudcZwdfdg ZxexdbZyexdcZzdhdi Z{e{dbZ|e{dcZ}djdk Z~eejej/eejej dldm Zeej/ej@eej ej@dndo Zeej dpeej/dpdqdr Zeejej/ej/ev eejej ej ev eejej/ej/es eejej ej es dS )tz4
Implementation of operations on numpy timedelta64.
    N)Constant)typescgutils)create_constant_array)lower_builtinlower_constantimpl_ret_untracked
lower_cast)npdatetime_helpersnumpy_supportnpyfuncs)overload_method	IS_32BITS)LoweringError@      c                 C      |  |tt|S )z2
    Multiply *val* by the constant *factor*.
    )mulr   TIMEDELTA64buildervalfactor r   f/home/ncw/WWW/www-new/content/articles/pi-bbp/venv/lib/python3.10/site-packages/numba/np/npdatetime.pyscale_by_constant      r   c                 C   r   )z0
    Divide *val* by the constant *factor*.
    )sdivr   r   r   r   r   r   unscale_by_constant"   r   r   c                 C   r   )z(
    Add constant *const* to *val*.
    )addr   r   )r   r   constr   r   r   add_constant)   r   r"   c                 C   s@   t |j|j}|du rd|j d|j }t|t|||S )zk
    Scale the timedelta64 *val* from *srcty* to *destty*
    (both numba.types.NPTimedelta instances)
    Nz cannot convert timedelta64 from  to )r
   get_timedelta_conversion_factorunitr   r   )contextr   r   srctydesttyr   msgr   r   r   scale_timedelta0   s   r*   c                 C   s`   t |j|j}|durt||||fS t |j|j}|dur(|t|||fS td||f )ze
    Scale either *left* or *right* to the other's unit, in order to have
    homogeneous units.
    Nzcannot normalize %r and %r)r
   r$   r%   r   RuntimeError)r&   r   leftrightlefttyrighttyr   r   r   r   normalize_timedeltas>   s   r0   retc                 C   s    t j| t|d}| t| |S )zM
    Allocate a NaT-initialized datetime64 (or timedelta64) result slot.
    name)r   alloca_oncer   storeNATr   r3   r1   r   r   r   alloc_timedelta_resultO   s   r8   c                 C   s   t j| tjd|d}|S )z8
    Allocate an uninitialized boolean result slot.
       r2   )r   r4   llvmliteirIntTyper7   r   r   r   alloc_boolean_resultX   s   r=   c                 C   s   |  d|tS )z?
    Return a predicate which is true if *val* is not NaT.
    !=)icmp_unsignedr6   )r   r   r   r   r   
is_not_nat`   s   r@   c                 C   sF   t |dksJ t| |d }|dd D ]}| |t| |}q|S )zH
    Return a predicate which is true if all of *vals* are not NaT.
    r9   r   N)lenr@   and_)r   valspredr   r   r   r   are_not_natg   s
   rE   )      rF      rF   rH   rF   rF   rH   rF   rH   rF   )rF      rF   rH   rF   rH   rF   rF   rH   rF   rH   rF   )r   rF   ;   Z   x               i  i0  iN  )r   rF   <   [   y               i  i1  iO  c                 C   s   t |tjS N)
DATETIME64astypenpint64)r&   r   typyvalr   r   r   datetime_constant      r_   c                 C   s   |d }t | ||j|S Nr   )r   return_typer&   r   sigargsresr   r   r   timedelta_pos_impl   s   rg   c                 C   s   | |d }t| ||j|S ra   )negr   rb   rc   r   r   r   timedelta_neg_impl   s   ri   c           	   	   C   s   |\}t |}|t||=\}}| |||| W d    n1 s)w   Y  | ||| W d    n1 sAw   Y  W d    n1 sPw   Y  ||}t| ||j|S rX   )	r8   if_elser   is_scalar_negr5   rh   loadr   rb   )	r&   r   rd   re   r   r1   then	otherwiserf   r   r   r   timedelta_abs_impl   s   
ro   c                 C   sh  |\}t |}ttd}||d||\}}| |ttd| W d   n1 s/w   Y  |Z ||d||@\}	}
|	 |ttd| W d   n1 sZw   Y  |
 |ttd| W d   n1 suw   Y  W d   n1 sw   Y  W d   n1 sw   Y  W d   n1 sw   Y  ||}t| ||j	|S )z
    np.sign(timedelta64)
    r   >r9   N==)
r8   r   r   rj   icmp_signedr5   r?   rl   r   rb   )r&   r   rd   re   r   r1   zerogt_zerole_zeroeq_zerolt_zerorf   r   r   r   timedelta_sign_impl   s2   

ry   c           
      C      |\}}|j \}}t|}t|t|||g$ t| ||||j}t| ||||j}||||| W d    n1 s?w   Y  |	|}	t
| ||j|	S rX   )re   r8   r   	if_likelyrE   r*   rb   r5   r    rl   r   
r&   r   rd   re   vavbtatbr1   rf   r   r   r   timedelta_add_impl      

r   c           
      C   rz   rX   )re   r8   r   r{   rE   r*   rb   r5   subrl   r   r|   r   r   r   timedelta_sub_impl   r   r   c           	      C   s   t |}t|t||6 t|tjr(|||j}|	||}t
| ||}n|||}t| ||||}||| W d    n1 sFw   Y  ||S rX   )r8   r   r{   r@   
isinstancer   Floatsitofptypefmul_cast_to_timedeltar   r*   r5   rl   )	r&   r   td_argtd_type
number_argnumber_typerb   r1   r   r   r   r   _timedelta_times_number   s   
r   c                 C   :   t | ||d |jd |d |jd |j}t| ||j|S )Nr   r9   r   re   rb   r   rc   r   r   r   timedelta_times_number   
   r   c                 C   r   Nr9   r   r   rc   r   r   r   number_times_timedelta   r   r   c              	   C   s   |\}}|j d }t|}|t|||t||}t||: t|t	j
r>|||j}	||	|}	t| ||	}	n|||}	t| ||	|j d |j}	||	| W d    n1 s`w   Y  ||}
t| ||j|
S r   )re   r8   rB   r@   not_r   is_scalar_zero_or_nanr{   r   r   r   r   r   fdivr   r   r*   rb   r5   rl   r   )r&   r   rd   re   r   r   r   r1   okr   rf   r   r   r   timedelta_over_number   s&   	

r   c                 C   s   |\}}|j \}}t|||g}| |j}	tj||	dd}
|t|	td|
 t	||) t
| |||||\}}|||	}|||	}|||||
 W d    n1 s[w   Y  ||
}t| ||j|S )Nr1   r2   nan)re   rE   get_value_typerb   r   r4   r5   r   floatr{   r0   r   r   rl   r   )r&   r   rd   re   r}   r~   r   r   not_nanll_ret_typer1   rf   r   r   r   timedelta_over_timedelta  s   

r   c                 C   sn  |\}}|j \}}| |j}t|||g}	tj||dd}
t|d}t|d}|||
 t||	 t	| |||||\}}|
|d||}t|| |d||}|||d||}||\}}|g ||R\}}| |||}|||}|||
 W d    n1 sw   Y  | |||}|||}|||
 W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  | |||}|||
 W d    n1 sw   Y  W d    n	1 sw   Y  W d    n	1 sw   Y  W d    n	1 s%w   Y  ||
}t| ||j|S )Nr1   r2   r   r9   rq   <)re   r   rb   rE   r   r4   r   r5   r{   r0   r   rs   or_rj   r   r   r    rl   r   )r&   r   rd   re   r}   r~   r   r   r   r   r1   rt   onedenom_okvanegrh   rm   rn   negthennegotherwisetopdivrf   r   r   r   timedelta_floor_div_timedelta+  sV   




r   c                 C   s  |\}}|j \}}t|||g}| |j}	t|}
|t|
 t|	d}t	|| t
| |||||\}}||d||}t	||v |d||}|d||}|||}|||||d||}||>\}}| |||
 W d    n1 sw   Y  | |||||
 W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  ||
}t| ||j|S )Nr   rq   rp   )re   rE   r   rb   r8   r5   r6   r   r   r{   r0   r   rs   sremr   rB   rj   r    rl   r   )r&   r   rd   re   r}   r~   r   r   r   r   r1   rt   r   vaposvbposremcondrm   rn   rf   r   r   r   timedelta_mod_timedeltaN  s@   


r   c                    s    fdd}|S )Nc              
      s*  |\}}|j \}}t|}|t|||gi\}	}
|	0 zt| |||||\}}W n ty9   | | Y nw ||||| W d    n1 sOw   Y  |
 dkrc|tj	| n|tj
| W d    n1 stw   Y  W d    n1 sw   Y  ||}t| ||j|S Nr>   )re   r=   rj   rE   r0   r+   r5   r?   r   true_bit	false_bitrl   r   rb   r&   r   rd   re   r}   r~   r   r   r1   rm   rn   norm_anorm_brf   default_valuell_opr   r   implt  s2   
	
z/_create_timedelta_comparison_impl.<locals>.implr   )r   r   r   r   r   r   !_create_timedelta_comparison_impls  s   r   c                        fdd}|S )Nc              	      s   |\}}|j \}}t|}|t|||gK\}	}
|	 t| |||||\}}|| ||| W d    n1 s=w   Y  |
 |tj| W d    n1 sVw   Y  W d    n1 sew   Y  |	|}t
| ||j|S rX   )re   r=   rj   rE   r0   r5   rs   r   r   rl   r   rb   r   r   r   r   r     s"   

z-_create_timedelta_ordering_impl.<locals>.implr   r   r   r   r   r   _create_timedelta_ordering_impl     r   rq   r>   r   <=rp   >=c              	   C   st   |  |ttd}t| | |ttd}t| | |ttd}t| | |ttd}| || ||S )z_
    Return a predicate indicating whether *year_val* (offset by 1970) is a
    leap year.
    i     d     )	r    r   rY   r   is_nullrB   is_not_nullr   r   )r   year_valactual_yearmultiple_of_4not_multiple_of_100multiple_of_400r   r   r   is_leap_year  s   
r   c              	   C   sl  t | t}t| |d}| t | |\}}|> t| |d}| |t| |d}t| |d}| 	|t| |d}t| |d}	| |t| |	d}| 
|| W d   n1 s[w   Y  |8 t| |d	}
| |t| |
d}t| |
d
}| 	|t| |d}| |t| |d}| 
|| W d   n1 sw   Y  W d   n1 sw   Y  | |S )zg
    Given a year *year_val* (offset to 1970), return the number of days
    since the 1970 epoch.
    im  r9      D   r   i,  r   Ni)r   r4   r   r   rj   
is_neg_intr"   r    r   r   r5   rl   )r   r   r1   daysif_negif_pos	from_1968p_days	from_1900	from_1600	from_1972n_days	from_2000r   r   r   year_to_days  sH   






(r   c                 C   s  t j| }t j| }|dk s|dkr||fS |dkr"|}t| |}nt| dt}t| dt}	t| t}
t	| |d\}}| 
t| |T\}}| | tj| |d|dd}| ||
 W d    n1 sjw   Y  | | tj| |	d|dd}| ||
 W d    n1 sw   Y  W d    n1 sw   Y  t| |}| || |
}|dkrt	| |d\}}|d	fS |d
fS )Nr   r   leap_year_months_accnormal_year_months_acc   T)inbounds   WD)r
   DATETIME_UNITSr   r   global_constantr   r   r4   r   divmod_by_constantrj   r   rl   gepr5   r    )r   dt_valsrc_unit	dest_unitdest_unit_codesrc_unit_coder   days_val
leap_arraynormal_arrayr   yearmonthrm   rn   addendweeks_r   r   r   reduce_datetime_for_unit  sH   



r   c                 C   sB   t | |||\}}t||}|du rtd||f t| ||S )zC
    Convert datetime *dt_val* from *src_unit* to *dest_unit*.
    Nz'cannot convert datetime64 from %r to %r)r   r
   r$   r   r   )r   r   r   r   dt_unit	dt_factorr   r   r   convert_datetime_for_arith)  s   r   c                    r   )Nc           
         s   t |}t|t|||g) t||||}t||}t|||}t| ||}	|	|	| W d    n1 s;w   Y  |
|S rX   )r8   r   r{   rE   r   r
   r$   r   getattrr5   rl   )
r&   r   dt_argr   r   td_unitret_unitr1   	td_factorret_val
ll_op_namer   r   r   :  s   
z'_datetime_timedelta_arith.<locals>.implr   )r   r   r   r   r   _datetime_timedelta_arith9  s   r  r    r   c           	      C   >   |\}}|j \}}t| |||j||j|jj}t| ||j|S rX   re   _datetime_plus_timedeltar%   rb   r   	r&   r   rd   re   r   r   dt_typer   rf   r   r   r   datetime_plus_timedeltaO     
r  c           	      C   s>   |\}}|j \}}t| |||j||j|jj}t| ||j|S rX   r  )	r&   r   rd   re   r   r   r   r  rf   r   r   r   timedelta_plus_datetime[  r  r	  c           	      C   r  rX   )re   _datetime_minus_timedeltar%   rb   r   r  r   r   r   datetime_minus_timedeltai  r  r  c                 C   s   |\}}|j \}}|j}|j}	|jj}
t|}t|t|||g" t||||
}t|||	|
}|||}|	|| W d    n1 sGw   Y  |
|}t| ||j|S rX   )re   r%   rb   r8   r   r{   rE   r   r   r5   rl   r   )r&   r   rd   re   r}   r~   r   r   unit_aunit_br   r1   r   rf   r   r   r   datetime_minus_datetimew  s   

r  c                    r   )Nc              	      s$  |\}}|j \}}|j}|j}	t||	}
t|}|t|||gZ\}}|# t||||
}t|||	|
}| ||}|	|| W d    n1 sNw   Y  |  dkr^t
j}nt
j}|	|| W d    n1 sqw   Y  W d    n1 sw   Y  ||}t| ||j|S r   )re   r%   r
   get_best_unitr=   rj   rE   r   rs   r5   r   r   r   rl   r   rb   )r&   r   rd   re   r}   r~   r   r   r  r  r   r1   rm   rn   r   r   r   rf   r   r   r   r     s6   

z._create_datetime_comparison_impl.<locals>.implr   r   r   r   r    _create_datetime_comparison_impl  s   r  c                    r   )Nc           
         r   |\}}t ||}t ||}|d||}||||}	 r#||}}|||	|}	|||	|}	t| ||j|	S Nr   r@   rs   selectr   rb   
r&   r   rd   re   in1in2in1_not_natin2_not_nat
in1_ge_in2rf   NAT_DOMINATESr   r   datetime_max_impl     


z1_gen_datetime_max_impl.<locals>.datetime_max_implr   )r  r  r   r  r   _gen_datetime_max_impl  r   r  TFc                    r   )Nc           
         r  Nr   r  
r&   r   rd   re   r  r  r  r  
in1_le_in2rf   r  r   r   datetime_min_impl  r  z1_gen_datetime_min_impl.<locals>.datetime_min_implr   )r  r#  r   r  r   _gen_datetime_min_impl  r   r$  c                    r   )Nc           
         r  r  r  r  r  r   r   timedelta_max_impl  r  z3_gen_timedelta_max_impl.<locals>.timedelta_max_implr   )r  r%  r   r  r   _gen_timedelta_max_impl  r   r&  c                    r   )Nc           
         r  r   r  r!  r  r   r   timedelta_min_impl  r  z3_gen_timedelta_min_impl.<locals>.timedelta_min_implr   )r  r'  r   r  r   _gen_timedelta_min_impl  r   r(  c              	   C   s   | t}|d||}||>\}}| |t| W d    n1 s'w   Y  | |||t| W d    n1 sCw   Y  W d    n1 sRw   Y  ||S )Nuno)allocar   fcmp_unorderedrj   r5   r6   fptosirl   )r&   r   r   temp
val_is_nanrm   elsr   r   r   r     s   

r   c                 C   s   t | |||S rX   )r   np_datetime_isnat_impl)r&   r   rd   re   r   r   r   _np_isnat_impl  r`   r1  c                 C   s.   |j dkrd| d| d| d}t||S )Nr   zCannot cast r#   z as z is not 64 bits wide.)bitwidth
ValueError)r&   r   fromtytotyr   r)   r   r   r   _cast_npdatetime_int64!  s   
r6  __hash__c                 C   s   t rdd }|S dd }|S )Nc                 S   sf   t | } | dk rt | }nt | d@ d? }t | d@ }t |d|  }|dkr1t d}|S )Nil            l    iCB rr   r   )r[   r\   int32)xyhilor   r   r   r   .  s   

z ol_hash_npdatetime.<locals>.implc                 S   s"   t | dkrt dS t | S )Nrr   r   )r[   r\   )r:  r   r   r   r   :  s   

r   )r:  r   r   r   r   ol_hash_npdatetime*  s
   r>  )r1   )__doc__numpyr[   operatorllvmlite.irr:   r   
numba.corer   r   numba.core.cgutilsr   numba.core.imputilsr   r   r   r	   numba.npr
   r   r   numba.extendingr   numba.core.configr   numba.core.errorsr   r;   r<   rY   r   r6   NPTimedeltaTIMEDELTA_BINOP_SIGr   r   r"   r*   r0   r8   r=   r@   rE   normal_year_monthsleap_year_monthsr   r   
NPDatetimer_   posrg   rh   ri   absro   ry   r    iaddr   r   isubr   r   r   Integerimulr   r   r   truedivitruedivfloordiv	ifloordivr   r   r   r   r   r   r   timedelta_eq_timedelta_implr   timedelta_ne_timedelta_impltimedelta_lt_timedelta_impltimedelta_le_timedelta_impltimedelta_gt_timedelta_impltimedelta_ge_timedelta_impleqneltlegtgeop_funcr   r   r   r   r  r  r
  r  r	  r  r  r  datetime_eq_datetime_impldatetime_ne_datetime_impldatetime_lt_datetime_impldatetime_le_datetime_impldatetime_gt_datetime_impldatetime_ge_datetime_implopr  datetime_maximum_impldatetime_fmax_implr$  datetime_minimum_impldatetime_fmin_implr&  timedelta_maximum_impltimedelta_fmax_implr(  timedelta_minimum_impltimedelta_fmin_implr   isnatr1  r6  r>  datetime_minimumdatetime_maximumr   r   r   r   <module>   sB   

	




"%6-

 

