o
    .j                     @  s   d Z ddlmZ ddlmZmZmZ ddlmZmZ er$ddl	m
Z
mZ dd
dZdddZdddZd ddZd!ddZd"ddZg dZdS )#z Utility functions for icalendar.    )annotations)datedatetimetzinfo)TYPE_CHECKINGcast)	TypeGuardTypeIsdtdate | datetimereturnboolc                 C  s   t | to
t | t S )a  Check if a value is a date but not a datetime.

    This function distinguishes between ``date`` and ``datetime`` objects,
    returning ``True`` only for pure ``date`` instances.

    Parameters:
        dt: The date or datetime object to check.

    Returns:
        ``True`` if the value is a ``date`` but not a ``datetime``,
        ``False`` otherwise.

    Example:
        .. code-block:: pycon

            >>> from datetime import date, datetime
            >>> from icalendar.tools import is_date
            >>> is_date(date(2024, 1, 15))
            True
            >>> is_date(datetime(2024, 1, 15, 10, 30))
            False
    )
isinstancer   r   r
    r   D/home/thesage/.local/lib/python3.10/site-packages/icalendar/tools.pyis_date   s   r   TypeIs[datetime]c                 C  s
   t | tS )a  Check if a value is a datetime.

    Parameters:
        dt: The date or datetime object to check.

    Returns:
        ``True`` if the value is a ``datetime``, ``False`` if it is
        only a ``date``.

    Example:
        .. code-block:: pycon

            >>> from datetime import date, datetime
            >>> from icalendar.tools import is_datetime
            >>> is_datetime(datetime(2024, 1, 15, 10, 30))
            True
            >>> is_datetime(date(2024, 1, 15))
            False
    )r   r   r   r   r   r   is_datetime&   s   
r   r   c                 C  s$   t | rt| j| j| jS td| S )a  Convert a date to a datetime.

    If the input is already a ``datetime``, it is returned unchanged.
    If the input is a ``date``, it is converted to a ``datetime`` at midnight.

    Parameters:
        dt: The date or datetime to convert.

    Returns:
        A ``datetime`` object. If the input was a ``date``, the time
        component will be set to midnight (00:00:00).

    Example:
        .. code-block:: pycon

            >>> from datetime import date, datetime
            >>> from icalendar.tools import to_datetime
            >>> to_datetime(date(2024, 1, 15))
            datetime.datetime(2024, 1, 15, 0, 0)
            >>> to_datetime(datetime(2024, 1, 15, 10, 30))
            datetime.datetime(2024, 1, 15, 10, 30)
    r   )r   r   yearmonthdayr   r   r   r   r   to_datetime=   s   
r   tzr   c                 C  s
   t | dS )ao  Check if a timezone is a pytz timezone.

    pytz timezones require special handling with ``localize()`` and
    ``normalize()`` methods for correct timezone calculations.

    Parameters:
        tz: The timezone info object to check.

    Returns:
        ``True`` if the timezone is a pytz timezone (has a ``localize``
        attribute), ``False`` otherwise.
    localize)hasattr)r   r   r   r   is_pytzY   s   
r   TypeGuard[datetime]c                 C  s   t | o| j }duot|S )av  Check if a datetime uses a pytz timezone.

    This function checks whether the datetime has a timezone attached
    and whether that timezone is a pytz timezone requiring special handling.

    Parameters:
        dt: The date or datetime object to check.

    Returns:
        ``True`` if the value is a ``datetime`` with a pytz timezone,
        ``False`` otherwise.
    N)r   r   r   )r
   r   r   r   r   
is_pytz_dti   s   r   c                 C  s   t | r
| j| S | S )a  Normalize a datetime after calculations when using pytz.

    pytz requires the ``normalize()`` function to be called after arithmetic
    operations to correctly adjust the timezone offset, especially around
    daylight saving time transitions.

    Parameters:
        dt: The date or datetime to normalize.

    Returns:
        The normalized datetime if it uses pytz, otherwise the input unchanged.
    )r   r   	normalizer   r   r   r   normalize_pytzy   s   r    )r   r   r   r   r    r   N)r
   r   r   r   )r
   r   r   r   )r
   r   r   r   )r   r   r   r   )r
   r   r   r   )r
   r   r   r   )__doc__
__future__r   r   r   r   typingr   r   icalendar.compatibilityr   r	   r   r   r   r   r   r    __all__r   r   r   r   <module>   s    





