o
    wvXjN2  ã                   @   s$  d Z ddlmZmZmZ ddl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mZmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ dd
l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# dZ$ej% &dd¡Z'dZ(e)e*e(e+dƒƒƒZ,dd„ dd„ dd„ dœZ-dddd„ fdddd„ fd d!d"d„ fd#d$d%d„ ffZ.edƒZ/dZ0eG d&d'„ d'eƒƒZ1G d(d)„ d)e2ƒZ3e3ƒ Zd*d+„ Z4d,d-„ Z5dLd.d/„Z6d0d1„ Z7d2d3„ Z8dMd6d7„Z9d8d9„ Z:d:d;„ Z;d<d=„ Z<d>d?„ Z=d@dA„ Z>dNdBdC„Z?eG dDdE„ dEe2ƒƒZ@e	e	jAfdFdG„ZBeBfdHdI„ZC	dOdJdK„ZDdS )Pz<Utilities related to dates, times, intervals, and timezones.é    )Úabsolute_importÚprint_functionÚunicode_literalsN)Ú
monthrange)ÚdateÚdatetimeÚ	timedeltaÚtzinfo)Úreprcall)Úcached_property)ÚAmbiguousTimeErrorÚFixedOffset)Útimezone)Úutc)ÚPY3Úpython_2_unicode_compatibleÚstring_té   )Ú
dictfilter)Úparse_iso8601)Ú	pluralize)ÚLocalTimezoner   Úmaybe_timedeltaÚdelta_resolutionÚ	remainingÚrateÚweekdayÚhumanize_secondsÚmaybe_iso8601Úis_naiveÚ
make_awareÚlocalizeÚto_utcÚmaybe_make_awareÚffwdÚ	utcoffsetÚadjust_timestampÚ get_exponential_backoff_intervalÚ
C_REMDEBUGF)ÚsunÚmonÚtueÚwedÚthuÚfriÚsaté   c                 C   s   | S ©N© ©Únr2   r2   úN/var/www/html/myproject/venv/lib/python3.10/site-packages/celery/utils/time.pyÚ<lambda>'   s    r6   c                 C   ó   | d S ©Nç      N@r2   r3   r2   r2   r5   r6   (   ó    c                 C   s   | d d S r8   r2   r3   r2   r2   r5   r6   )   s    )ÚsÚmÚhÚdayg     õ@c                 C   ó
   t | dƒS ©Nú.2f©Úformatr3   r2   r2   r5   r6   -   ó   
 Úhourg      ¬@c                 C   r?   r@   rB   r3   r2   r2   r5   r6   .   rD   Úminuter9   c                 C   r?   r@   rB   r3   r2   r2   r5   r6   /   rD   Úsecondç      ð?c                 C   r?   r@   rB   r3   r2   r2   r5   r6   0   rD   c                   @   sP   e Zd ZdZi Zdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Z	e
r"dd„ Zdd„ ZdS )r   zpLocal time implementation.

    Note:
        Used only when the :setting:`enable_utc` setting is disabled.
    c                 C   sL   t tj d�| _tjrt tj d�| _n| j| _| j| j | _t 	| ¡ d S )N©Úseconds)
r   Ú_timer   Ú	STDOFFSETÚdaylightÚaltzoneÚ	DSTOFFSETÚDSTDIFFr	   Ú__init__©Úselfr2   r2   r5   rQ   B   s   zLocalTimezone.__init__c                 C   s   d  t| j ¡ d ƒ¡S )Nz<LocalTimezone: UTC{0:+03d}>é  )rC   ÚintrO   Útotal_secondsrR   r2   r2   r5   Ú__repr__M   s   ÿzLocalTimezone.__repr__c                 C   s   |   |¡r| jS | jS r1   )Ú_isdstrO   rL   ©rS   Údtr2   r2   r5   r%   R   s   zLocalTimezone.utcoffsetc                 C   s   |   |¡r| jS tS r1   )rX   rP   ÚZEROrY   r2   r2   r5   ÚdstU   s   zLocalTimezone.dstc                 C   s   t j|  |¡ S r1   )rK   ÚtznamerX   rY   r2   r2   r5   r]   X   s   zLocalTimezone.tznamec                 C   sZ   t |  |¡jd ƒ}z| j| }W n ty#   t|ƒ }| j|< Y nw | |j|d�¡S )Nr9   ©r	   )rU   r%   rJ   Ú_offset_cacheÚKeyErrorr   ÚfromutcÚreplace)rS   rZ   ÚoffsetÚtzr2   r2   r5   ra   ]   s   ÿzLocalTimezone.fromutcc              	   C   sD   |j |j|j|j|j|j| ¡ ddf	}t |¡}t 	|¡}|j
dkS )Nr   )ÚyearÚmonthr>   rE   rF   rG   r   rK   ÚmktimeÚ	localtimeÚtm_isdst)rS   rZ   ÚttÚstampr2   r2   r5   rX   j   s   
þ


zLocalTimezone._isdstN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r_   rQ   rW   r%   r\   r]   r   ra   rX   r2   r2   r2   r5   r   8   s    r   c                   @   s^   e Zd Zddd„Zddd„Zerdd„ Zndd„ Zd	d
„ Zdd„ Ze	dd„ ƒZ
e	dd„ ƒZdS )Ú_ZoneNc                 C   s   |d u r| j S |  |¡S r1   )ÚlocalÚget_timezone)rS   r	   r2   r2   r5   Útz_or_localu   s   
z_Zone.tz_or_localc                 C   s(   t |ƒrt||p
| jƒ}t||  |¡ƒS r1   )r   r    r   r!   rs   )rS   rZ   rq   Úorigr2   r2   r5   Úto_local{   s   z_Zone.to_localc                 C   s   |j d d�S )N)rd   )Ú
astimezonerY   r2   r2   r5   Ú	to_system‚   s   z_Zone.to_systemc                 C   s   t || jƒS r1   )r!   rq   rY   r2   r2   r5   rw   ‰   s   c                 C   s    t |ƒr
t|| jƒS t|| jƒS r1   )r   r    rq   r!   rY   r2   r2   r5   Úto_local_fallbackŒ   s   z_Zone.to_local_fallbackc                 C   s   t |tƒr	t|ƒS |S r1   )Ú
isinstancer   Ú	_timezone)rS   Úzoner2   r2   r5   rr   ‘   s   
z_Zone.get_timezonec                 C   s   t ƒ S r1   )r   rR   r2   r2   r5   rq   –   s   z_Zone.localc                 C   s
   |   d¡S )NÚUTC)rr   rR   r2   r2   r5   r   š   s   
z	_Zone.utcr1   )NN)rl   rm   rn   rs   ru   r   rw   rx   rr   r   rq   r   r2   r2   r2   r5   rp   s   s    



rp   c                 C   s   t | tjƒrt| d�S | S )z8Convert integer to timedelta, if argument is an integer.rI   )ry   ÚnumbersÚRealr   )Údeltar2   r2   r5   r   ¢   s   
r   c                 C   s†   t | ¡ dƒ}ddd„ fddd„ fddd„ ff}| j| j| j| j| j| jf}|D ]\}}||ƒd	kr@t|d
|… d| j	iŽ  S q(| S )a¥  Round a :class:`~datetime.datetime` to the resolution of timedelta.

    If the :class:`~datetime.timedelta` is in days, the
    :class:`~datetime.datetime` will be rounded to the nearest days,
    if the :class:`~datetime.timedelta` is in hours the
    :class:`~datetime.datetime` will be rounded to the nearest hour,
    and so on until seconds, which will just return the original
    :class:`~datetime.datetime`.
    r   é   c                 S   r7   )Ni€Q r2   ©Úxr2   r2   r5   r6   µ   r:   z"delta_resolution.<locals>.<lambda>é   c                 S   r7   )NrT   r2   r�   r2   r2   r5   r6   ¶   r:   é   c                 S   r7   )Né<   r2   r�   r2   r2   r5   r6   ·   r:   rH   Nr	   )
ÚmaxrV   re   rf   r>   rE   rF   rG   r   r	   )rZ   r   ÚresolutionsÚargsÚresÚ	predicater2   r2   r5   r   ©   s   



þÿr   c                 C   s‚   |pt  ¡ }t| jƒt|jƒkr| ¡ |  ¡ kr| j|jd�} | | }|r.t||ƒjdd�}|| }tr?td|| |||f ƒ |S )a_  Calculate the remaining time for a start date and a timedelta.

    For example, "how many seconds left for 30 seconds after start?"

    Arguments:
        start (~datetime.datetime): Starting date.
        ends_in (~datetime.timedelta): The end delta.
        relative (bool): If enabled the end time will be calculated
            using :func:`delta_resolution` (i.e., rounded to the
            resolution of `ends_in`).
        now (Callable): Function returning the current time and date.
            Defaults to :func:`datetime.utcnow`.

    Returns:
        ~datetime.timedelta: Remaining time.
    r^   r   )Úmicrosecondz2rem: NOW:%r START:%r ENDS_IN:%r END_DATE:%s REM:%s)	r   ÚutcnowÚstrr	   r%   rb   r   r(   Úprint)ÚstartÚends_inÚnowÚrelativeÚend_dateÚretr2   r2   r5   r   À   s   $
ÿr   c                 C   sB   | rt | tƒr|  d¡\}}}t|pd t|ƒƒpdS | pdS dS )zAConvert rate string (`"100/m"`, `"2/h"` or `"0.5/s"`) to seconds.ú/r;   r   )ry   r   Ú	partitionÚRATE_MODIFIER_MAPÚfloat)ÚrÚopsÚ_Úmodifierr2   r2   r5   r   ß   s   
r   c                 C   s2   | dd…   ¡ }zt| W S  ty   t| ƒ‚w )zžReturn the position of a weekday: 0 - 7, where 0 is Sunday.

    Example:
        >>> weekday('sunday'), weekday('sun'), weekday('mon')
        (0, 0, 1)
    r   r€   )ÚlowerÚWEEKDAYSr`   )ÚnameÚabbreviationr2   r2   r5   r   é   s   
þr   Ú r‘   c           	   	   C   st   t tt | ƒdƒƒ} tD ]\}}}| |kr)| t |ƒ }d ||||ƒt||ƒ¡  S q|r8| dkr8dj| ||d�S |S )a[  Show seconds in human form.

    For example, 60 becomes "1 minute", and 7200 becomes "2 hours".

    Arguments:
        prefix (str): can be used to add a preposition to the output
            (e.g., 'in' will give 'in 1 second', but add nothing to 'now').
        now (str): Literal 'now'.
        microseconds (bool): Include microseconds.
    rA   z{0}{1}{2} {3}g        z{prefix}{sep}{0:.2f} seconds)ÚsepÚprefix)r˜   rC   Ú
TIME_UNITSr   )	Úsecsr£   r¢   r‘   ÚmicrosecondsÚunitÚdividerÚ	formatterÚwr2   r2   r5   r   ø   s   ÿþÿr   c                 C   s   | sdS t | tƒr| S t| ƒS )z:Either ``datetime | str -> datetime`` or ``None -> None``.N)ry   r   r   ©rZ   r2   r2   r5   r     s
   
r   c                 C   s   | j du p| j  | ¡du S )z=Return :const:`True` if :class:`~datetime.datetime` is naive.N)r	   r%   r«   r2   r2   r5   r     s   r   c              	   C   sf   z|j }W n ty   | j|d� Y S w z|| dd�W S  ty2   t|| dd�|| dd�ƒ Y S w )z6Set timezone for a :class:`~datetime.datetime` object.r^   N©Úis_dstTF)r!   ÚAttributeErrorrb   r   Úmin)rZ   rd   Ú	_localizer2   r2   r5   r      s   
ÿ
ÿÿr    c              	   C   sœ   t | ƒr	t| |ƒ} | jtkr|  |¡} z|j}W n ty#   |  Y S w z|| dd�W S  ty8   || ƒ Y S  tyM   t	|| dd�|| dd�ƒ Y S w )z>Convert aware :class:`~datetime.datetime` to another timezone.Nr¬   TF)
r   r    r	   r   rv   Ú	normalizer®   Ú	TypeErrorr   r¯   )rZ   rd   Ú
_normalizer2   r2   r5   r!   ,  s$   



ÿ
ÿÿr!   c                 C   s   t | tjƒS )z1Convert naive :class:`~datetime.datetime` to UTC.)r    r   r   r«   r2   r2   r5   r"   @  s   r"   c                 C   s4   t | ƒrt| ƒ} t| |du rtjƒS t |¡ƒS | S )z@Convert dt to aware datetime, do nothing if dt is already aware.N)r   r"   r!   r   r   rs   )rZ   rd   r2   r2   r5   r#   E  s   ÿÿr#   c                   @   s6   e Zd ZdZ		ddd„Zdd„ Zdd	„ Zd
d„ ZdS )r$   zBVersion of ``dateutil.relativedelta`` that only supports addition.Nr   c
                 K   sZ   || _ || _|| _|| _|| _|| _|| _|| _|	| _|d | _	| jd up)| jd u| _
d S )Nr0   )re   rf   Úweeksr   r>   rE   rF   rG   r‹   ÚdaysÚ	_has_time)rS   re   rf   r´   r   r>   rE   rF   rG   r‹   Úkwargsr2   r2   r5   rQ   S  s   
zffwd.__init__c                 C   s   t dd| j| j| jd�ƒS )Nr$   r2   )r´   r   )r
   Ú_fieldsr´   r   rR   r2   r2   r5   rW   d  s   ÿzffwd.__repr__c              	   C   s    t |tƒstS | jp|j}| jp|j}tt||ƒd | jp|jƒ}|jdi t	t
|  ¡ ƒ|||d�¤Ž}| jd urH|td| ¡  | j d d�7 }|t| jd� S )Nr   )re   rf   r>   r0   )rµ   r2   )ry   r   ÚNotImplementedre   rf   r¯   r   r>   rb   Údictr   r¸   r   r   rµ   )rS   Úotherre   rf   r>   r”   r2   r2   r5   Ú__radd__h  s   

ÿ
 zffwd.__radd__c              	   K   s.   t | j| j| j| j| j| j| jdœfi |¤ŽS )N)re   rf   r>   rE   rF   rG   r‹   )r   re   rf   r>   rE   rF   rG   r‹   )rS   Úextrar2   r2   r5   r¸   t  s   ýüzffwd._fields)	NNr   NNNNNN)rl   rm   rn   ro   rQ   rW   r¼   r¸   r2   r2   r2   r5   r$   O  s    
ÿr$   c                 C   s   |ƒ j r	| jd S | jd S )z*Return the current offset to UTC in hours.rT   )ri   rN   r   )Útimerh   r2   r2   r5   r%   |  s   

r%   c                 C   s   | ||ƒ  d  S )z-Adjust timestamp based on provided utcoffset.rT   r2   )Útsrc   Úherer2   r2   r5   r&   ƒ  s   r&   c                 C   s.   t || d|  ƒ}|rt |d ¡}td|ƒS )z,Calculate the exponential backoff wait time.é   r   r   )r¯   ÚrandomÚ	randranger†   )ÚfactorÚretriesÚmaximumÚfull_jitterÚ	countdownr2   r2   r5   r'   ˆ  s   
r'   )NF)r¡   r¡   r‘   Fr1   )F)Ero   Ú
__future__r   r   r   r}   ÚosrÂ   r¾   rK   Úcalendarr   r   r   r   r	   Úkombu.utils.functionalr
   Úkombu.utils.objectsr   Úpytzr   r   r   rz   r   Úcelery.fiver   r   r   Ú
functionalr   Úiso8601r   Útextr   Ú__all__ÚenvironÚgetr(   ÚDAYNAMESrº   ÚzipÚrangerž   r—   r¤   r[   Ú_local_timezoner   Úobjectrp   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   rh   r%   r&   r'   r2   r2   r2   r5   Ú<module>   sl   	ýü:,


	

,	ü