o
    g                     @   s   d Z ddlZddlmZ ddlZedddZedddZd	Zd
ZeZeZdZ	dZ
edejZedZdd Zdd ZefddZedfddZdd Zdd Zdd ZdS )zManage Excel date weirdness.    N)isnanip     ik        g   @lBAg   @oBAiQ z%Y-%m-%dT%H:%M:%SZz
(?P<date>(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}))?T?
(?P<time>(?P<hour>\d{2}):(?P<minute>\d{2})(:(?P<second>\d{2})(?P<microsecond>\.\d{1,3})?)?)?Z?zHPT((?P<hours>\d+)H)?((?P<minutes>\d+)M)?((?P<seconds>\d+(\.\d{1,3})?)S)?c                 C   s$   t | dr| jr| jddS |  S )z.Convert from a datetime to a timestamp string.microsecondmilliseconds)timespec)hasattrr   	isoformat)dt r   Q/var/www/html/api-tag/env/lib/python3.10/site-packages/openpyxl/utils/datetime.py
to_ISO8601   s   r   c                 C   sD  | sdS t | }|rpt| rp|d}dD ]}|| r&t|| ||< q|d r7tt|d d |d< |d sMt|d |d |d	 |d }|S |d
 s`t	|d |d |d }|S |d
= |d= tjdi |}|S t
| }|rt| r|d}| D ]\}}|rt|||< qtjdi |S td| )az  Convert from a timestamp string to a datetime object. According to
    18.17.4 in the specification the following ISO 8601 formats are
    supported.

    Dates B.1.1 and B.2.1
    Times B.1.2 and B.2.2
    Datetimes B.1.3 and B.2.3

    There is no concept of timedeltas in the specification, but Excel
    writes them (in strict OOXML mode), so these are also understood.
    Nr   )yearmonthdayhourminutesecondr   @B dater   r   r   timer   r   r   zInvalid datetime value {}r   )	ISO_REGEXmatchanygroups	groupdictintfloatdatetimer   r   ISO_DURATIONitems	timedelta
ValueErrorformat)formatted_stringr   partskeyr   valr   r   r   from_ISO8601#   s<   

 

r)   c                 C   s   t | tjr
t| S t | tjrt| S t| jrdS t| ds)tj	| t } | | j
}d|  k r8dkrBn n|tkrB|d8 }|t|  S )z'Convert Python datetime to Excel serialNr   r   <   r   )
isinstancer   r   time_to_daysr"   timedelta_to_daysr   r   r	   combinedaysWINDOWS_EPOCH)r   epochr/   r   r   r   to_excelQ   s   


 r2   Fc                 C   s   | du rdS |r"t j| d}|jr t j| d t|jdd}|S t| d\}}t jt|t d d}d|   kr?dk rJn n	|jdkrJt|S d|   k rTd	k r^n n|t	kr^|d7 }|t j|d | S )
z'Convert Excel serial to Python datetimeN)r/   r   )secondsmicrosecondsi  )r   r   r*   )
r   r"   r5   total_secondsrounddivmodSECS_PER_DAYr/   days_to_timer0   )valuer1   r"   tdr   fractiondiffr   r   r   
from_exceld   s    
" r?   c                 C   s(   | j d | jd  | j | jd  t S )z(Convert a time value to fractions of dayi  r*   r   )r   r   r   r   r9   r;   r   r   r   r,   z   s   r,   c                 C   s   |   t S )z/Convert a timedelta value to fractions of a day)r6   r9   r@   r   r   r   r-      s   r-   c                 C   s0   t | jd\}}t |d\}}t|||| jS )Nr*   )r8   r4   r   r   r5   )r;   minsr4   hoursr   r   r   r:      s   r:   )__doc__r   mathr   re	MAC_EPOCHr0   CALENDAR_WINDOWS_1900CALENDAR_MAC_1904r9   
ISO_FORMATcompileVERBOSEr   r    r   r)   r2   r?   r,   r-   r:   r   r   r   r   <module>   s.   
.
