
    Xi                        d Z ddlZdZed         ed         z
  dk    sJ  ej        ddd          Z ej        ddd	          Z ej        ddd
          ZdZ G d de          Z G d de          Z	 G d de          Z
 G d de          Z G d de          Z G d de          Zd Zd Zd ZdZd Zd Zd ZdS )a  
Tools for working with dates and times in Excel files.

The conversion from ``days`` to ``(year, month, day)`` starts with
an integral "julian day number" aka JDN.
FWIW:

- JDN 0 corresponds to noon on Monday November 24 in Gregorian year -4713.

More importantly:

- Noon on Gregorian 1900-03-01 (day 61 in the 1900-based system) is JDN 2415080.0
- Noon on Gregorian 1904-01-02 (day  1 in the 1904-based system) is JDN 2416482.0

    N)i$ ia$    i  ip  ik           )i$- i- c                       e Zd ZdZdS )XLDateErrorz-A base class for all datetime-related errors.N__name__
__module____qualname____doc__     6/usr/local/lib/python3.11/dist-packages/xlrd/xldate.pyr   r   #   s        3333r   r   c                       e Zd ZdZdS )XLDateNegativez``xldate < 0.00``Nr	   r   r   r   r   r   '   s        r   r   c                       e Zd ZdZdS )XLDateAmbiguouszGThe 1900 leap-year problem ``(datemode == 0 and 1.0 <= xldate < 61.0)``Nr	   r   r   r   r   r   +   s        MMMMr   r   c                       e Zd ZdZdS )XLDateTooLargezGregorian year 10000 or laterNr	   r   r   r   r   r   /   s        ####r   r   c                       e Zd ZdZdS )XLDateBadDatemodez#``datemode`` arg is neither 0 nor 1Nr	   r   r   r   r   r   3   s        ))))r   r   c                       e Zd ZdS )XLDateBadTupleN)r
   r   r   r   r   r   r   r   7   s        Dr   r   c                    |dvrt          |          | dk    rdS | dk     rt          |           t          |           }| |z
  }t          t          |dz                      }d|cxk    rdk    sn J |dk    rdx}x}}|dz  }n&t	          |d          \  }}t	          |d          \  }}|t
          |         k    rt          |           |dk    rddd|||fS |d	k     r|dk    rt          |           |t          |         z   }	|	d
z  dz   dz  dz  d
z  |	z   dz   d
z  dz   }
|
dz  d
z  dz  dz   }|dz  dz  dz   }|dz  }|dk    r|
dz  dz
  |dz
  ||||fS |
dz  dz
  |dz   ||||fS )aU  
    Convert an Excel number (presumed to represent a date, a datetime or a time) into
    a tuple suitable for feeding to datetime or mx.DateTime constructors.

    :param xldate: The Excel number
    :param datemode: 0: 1900-based, 1: 1904-based.
    :raises xlrd.xldate.XLDateNegative:
    :raises xlrd.xldate.XLDateAmbiguous:

    :raises xlrd.xldate.XLDateTooLarge:
    :raises xlrd.xldate.XLDateBadDatemode:
    :raises xlrd.xldate.XLDateError:
    :returns: Gregorian ``(year, month, day, hour, minute, nearest_second)``.

    .. warning::

      When using this function to interpret the contents of a workbook, you
      should pass in the :attr:`~xlrd.book.Book.datemode`
      attribute of that workbook. Whether the workbook has ever been anywhere
      near a Macintosh is irrelevant.

    .. admonition:: Special case

        If ``0.0 <= xldate < 1.0``, it is assumed to represent a time;
        ``(0, 0, 0, hour, minute, second)`` will be returned.

    .. note::

        ``1904-01-01`` is not regarded as a valid date in the ``datemode==1``
        system; its "serial number" is zero.
    r   r           )r   r   r   r   r   r   g     @r   iQ r   <   =      ie/ i:    iS    i  iM  i @     
   ik  	   l  )	r   r   introunddivmod_XLDAYS_TOO_LARGEr   r   
_JDN_delta)xldatedatemodexldaysfracsecondshourminutesecondminutesjdnyregmpds                r   xldate_as_tupler9   ;   s   @ v)))~~!!}}V$$$[[FF?D%w''((G    5      %!"""v! !"--gr**f"8,,,V$$${{1avv..{{x1}}f%%%
:h'
'CQwF*a/14;dBaG!KD$;1
	#c	)B
u*	!A2IB	Rxx%rAvq$GG%rAvq$GGr   c                     |rt           }n| dk     rt          }nt          }t          |           }| |z
  }t          t	          |dz                      }t          |d          \  }}|t          j        ||d|          z   S )z
    Convert an Excel date/time number into a :class:`datetime.datetime` object.

    :param xldate: The Excel number
    :param datemode: 0: 1900-based, 1: 1904-based.

    :returns: A :class:`datetime.datetime` object.
    r   g    pAi  r   )
epoch_1904
epoch_1900epoch_1900_minus_1r'   r(   r)   datetime	timedelta)r,   r-   epochdaysfractionr0   millisecondss          r   xldate_as_datetimerD      s      'B;;EE 'E v;;D}H %:-..//G"7D11G\8%dGQEEEEr   c                 0    | dz  rdS | dz  rdS | dz  rdS dS )Nr    r   d   r   i  r   )ys    r   _leaprH      s1    1uQQ3wqq3wqq1r   )Nr      r   r   r   r   r   r   r   r   r   r   c                    | \  }}}|dvrt          |          |dk    r|dk    r|dk    rdS d|cxk    rdk    sn t          d|||f          d|cxk    rdk    sn t          d	|||f          |dk     s,|t          |         k    r0|d
k    r|dk    rt          |          st          d|||f          |dz   }|}|dk    r|dz
  }|dz   }n|dz
  }d|z  dz  d|z  dz   dz  z   |z   dz
  |dz   dz  dz  dz  z
  }|t          |         z
  }	|	dk    rt          d|||f          |	dk     r|dk    rt          d|||f          t          |	          S )a  
    Convert a date tuple (year, month, day) to an Excel date.

    :param year: Gregorian year.
    :param month: ``1 <= month <= 12``
    :param day: ``1 <= day <= last day of that (year, month)``
    :param datemode: 0: 1900-based, 1: 1904-based.
    :raises xlrd.xldate.XLDateAmbiguous:
    :raises xlrd.xldate.XLDateBadDatemode:
    :raises xlrd.xldate.XLDateBadTuple:
      ``(year, month, day)`` is too early/late or has invalid component(s)
    :raises xlrd.xldate.XLDateError:
    r   r   r   il  i'  zInvalid year: r   r   zInvalid month:       zInvalid day: r&   r%   r!   r"   r    i         iT     rF   zInvalid (year, month, day): r   zBefore 1900-03-01: )r   r   _days_in_monthrH   r+   r   float)

date_tupler-   yearmonthdayYpMMpr5   r.   s
             r   xldate_from_date_tuplerY      s    "D%v)))qyyUaZZC1HHtD    D    nD%3E3EGHHH"nT5#4F4FHIII	q
u%
%
%cRiiEQJJ5QU;;Jn42D2DFGGG	BAAvv!VUU"9>sRx"}3
4SS(A-24C:h''F{{n$sASASUVVV{{x1}}o$s9K9KMNNN==r   c                     | \  }}}d|cxk    rdk     r4n n1d|cxk    rdk     r$n n!d|cxk    rdk     rn n|dz  |z   dz  |z   dz  S t          d|||f          )a-  
    Convert a time tuple ``(hour, minute, second)`` to an Excel "date" value
    (fraction of a day).

    :param hour: ``0 <= hour < 24``
    :param minute: ``0 <= minute < 60``
    :param second: ``0 <= second < 60``
    :raises xlrd.xldate.XLDateBadTuple: Out-of-range hour, minute, or second
    r      r   g      N@g      8@z Invalid (hour, minute, second): )r   )
time_tupler1   r2   r3   s       r   xldate_from_time_tupler]      s     &D&&D~~~~2~~~~~!v*********qF/?/?/?/?R/?/?/?/?/?$'4/$6$>>
.$PVAWAWY
Z
ZZr   c                 b    t          | dd         |          t          | dd                   z   S )a+  
    Convert a datetime tuple ``(year, month, day, hour, minute, second)`` to an
    Excel date value.
    For more details, refer to other xldate_from_*_tuple functions.

    :param datetime_tuple: ``(year, month, day, hour, minute, second)``
    :param datemode: 0: 1900-based, 1: 1904-based.
    Nr!   )rY   r]   )datetime_tupler-   s     r   xldate_from_datetime_tupler`      s8     	~bqb18<<~abb122	3r   )r   r>   r+   r;   r<   r=   r*   
ValueErrorr   r   r   r   r   r   r9   rD   rH   rP   rY   r]   r`   r   r   r   <module>rb      s	    (
!}z!}$,,,, XtQ**
XtR,,
&X&tR44  . 4 4 4 4 4* 4 4 4    [   N N N N Nk N N N$ $ $ $ $[ $ $ $* * * * * * * *	 	 	 	 	[ 	 	 	DH DH DHNF F FD   H, , ,^[ [ [     r   