U
    c$                     @   s   d dl Z d dlmZ ddlmZ ddlmZmZmZm	Z	m
Z
 e Ze Zi fddZi fdd	Zd
d Zei fddZG dd deZeejdZdZdS )    Ntzinfo   )_compat)AmbiguousTimeErrorNonExistentTimeErrorPytzUsageWarningUnknownTimeZoneErrorget_exceptionc                 C   s   | | d}|dkr|t| dkr:|  dkr:|| t}nBzt| }W n tk
rf   tt	| Y nX || t
|| d}|S )a  Builds an IANA database time zone shim.

    This is the equivalent of ``pytz.timezone``.

    :param key:
        A valid key from the IANA time zone database.

    :raises UnknownTimeZoneError:
        If an unknown value is passed, this will raise an exception that can be
        caught by :exc:`pytz_deprecation_shim.UnknownTimeZoneError` or
        ``pytz.UnknownTimeZoneError``. Like
        :exc:`zoneinfo.ZoneInfoNotFoundError`, both of those are subclasses of
        :exc:`KeyError`.
    N   utckey)getlenlower
setdefaultUTCr   Zget_timezoneKeyErrorr
   r	   	wrap_zone)r   _cacheinstancezone r   ?/tmp/pip-unpacked-wheel-pe1vybbg/pytz_deprecation_shim/_impl.pytimezone   s    r   c                 C   sl   d|   k rdk s n t d| || d}|dkrh| dkrJ|| t}nt| }|| t|dd}|S )a  Builds a fixed offset time zone shim.

    This is the equivalent of ``pytz.FixedOffset``. An alias is available as
    ``pytz_deprecation_shim.FixedOffset`` as well.

    :param offset:
        A fixed offset from UTC, in minutes. This must be in the range ``-1439
        <= offset <= 1439``.

    :raises ValueError:
        For offsets whose absolute value is greater than or equal to 24 hours.

    :return:
        A shim time zone.
    i`i  zabsolute offset is too largeNr   r   )
ValueErrorr   r   r   r   Zget_fixed_offset_zoner   )offsetr   r   r   r   r   r   fixed_offset_timezone/   s    

r   c                 C   s   t |}t|| dS )aT  Builds a shim object from a TZif file.

    This is a shim for ``pytz.build_tzinfo``. Given a value to use as the zone
    IANA key and a file-like object containing a valid TZif file (i.e.
    conforming to :rfc:`8536`), this builds a time zone object and wraps it in
    a shim class.

    The argument names are chosen to match those in ``pytz.build_tzinfo``.

    :param zone:
        A string to be used as the time zone object's IANA key.

    :param fp:
        A readable file-like object emitting bytes, pointing to a valid TZif
        file.

    :return:
        A shim time zone.
    r   )r   Zget_timezone_filer   )r   fpZ	zone_filer   r   r   build_tzinfoM   s    
r    c                 C   s^   |t krt| dt }|t kr$td|t| |fd}|dkrZ|t| |ft| |}|S )a  Wrap an existing time zone object in a shim class.

    This is likely to be useful if you would like to work internally with
    non-``pytz`` zones, but you expose an interface to callers relying on
    ``pytz``'s interface. It may also be useful for passing non-``pytz`` zones
    to libraries expecting to use ``pytz``'s interface.

    :param tz:
        A :pep:`495`-compatible time zone, such as those provided by
        :mod:`dateutil.tz` or :mod:`zoneinfo`.

    :param key:
        The value for the IANA time zone key. This is optional for ``zoneinfo``
        zones, but required for ``dateutil.tz`` zones.

    :return:
        A shim time zone.
    r   zVThe `key` argument is required when wrapping zones that do not have a `key` attribute.N)KEY_SENTINELgetattr	TypeErrorr   idr   _PytzShimTimezone)tzr   r   r   r   r   r   r   f   s    r   c                   @   s   e Zd ZdZdZdd Zdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd Zedd ZefddZdd Zdd ZdddZdd ZdS )r%   Nc                 C   s   || _ || _d S N)_key_zone)selfr   r   r   r   r   __init__   s    z_PytzShimTimezone.__init__c                 C   s   | j |S r'   )r)   	utcoffsetr*   dtr   r   r   r,      s    z_PytzShimTimezone.utcoffsetc                 C   s   | j |S r'   )r)   dstr-   r   r   r   r/      s    z_PytzShimTimezone.dstc                 C   s   | j |S r'   )r)   tznamer-   r   r   r   r0      s    z_PytzShimTimezone.tznamec                 C   s&   |j | jd}| j|}|j | dS )Nr   )replacer)   fromutc)r*   r.   Zdt_basedt_outr   r   r   r2      s    z_PytzShimTimezone.fromutcc                 C   s    | j d k	rt| j S t| S d S r'   )r(   strreprr*   r   r   r   __str__   s    

z_PytzShimTimezone.__str__c                 C   s   d| j jt| jt| jf S )Nz
%s(%s, %s))	__class____name__r5   r)   r(   r6   r   r   r   __repr__   s
    z_PytzShimTimezone.__repr__c                 C   s   | j S )a  Returns the underlying class that the shim is a wrapper for.

        This is a shim-specific method equivalent to
        :func:`pytz_deprecation_shim.helpers.upgrade_tzinfo`. It is provided as
        a method to allow end-users to upgrade shim timezones without requiring
        an explicit dependency on ``pytz_deprecation_shim``, e.g.:

        .. code-block:: python

            if getattr(tz, "unwrap_shim", None) is None:
                tz = tz.unwrap_shim()
        )r)   r6   r   r   r   unwrap_shim   s    z_PytzShimTimezone.unwrap_shimc                 C   s   t jddt  tdd | jS )Nz`The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. z(For more details on how to do so, see %s   
stacklevel)warningswarnPYTZ_MIGRATION_GUIDE_URLr   r(   r6   r   r   r   r      s    	z_PytzShimTimezone.zonec                 C   s  t jddt  tdd |jd k	r*td|j| d}|tkrB|S t	|}|sZt
|nd}|d kr|r|tt|jd d|rtt|jd dnp|s|rt|r|}tj|dd	}ntj|d
d	}t| }t| |kr| | k}||kr|}|S )NzThe localize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see %sr<   r=   z*Not naive datetime (tzinfo is already set)r   Fr   )foldr   )r?   r@   rA   r   r   r   r1   IS_DST_SENTINELr   Zis_ambiguousZis_imaginaryr
   r   r   Zget_foldZenfoldboolr/   r,   )r*   r.   Zis_dstr3   Zdt_ambiguousZdt_imaginaryZdt_enfoldedZenfolded_dstr   r   r   localize   sD    	

 



z_PytzShimTimezone.localizec                 C   sB   t jddt  tdd |jd kr*td|j| kr8|S || S )NzThe normalize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant rB   r<   r=   zNaive time - no tzinfo set)r?   r@   rA   r   r   r   
astimezoner-   r   r   r   	normalize	  s    	

z_PytzShimTimezone.normalizec                 C   s   | S r'   r   r6   r   r   r   __copy__  s    z_PytzShimTimezone.__copy__c                 C   s   | S r'   r   )r*   memor   r   r   __deepcopy__  s    z_PytzShimTimezone.__deepcopy__c                 C   s   t | j| jffS r'   )r   r)   r(   r6   r   r   r   
__reduce__!  s    z_PytzShimTimezone.__reduce__)N)r9   
__module____qualname__r)   r(   r+   r,   r/   r0   r2   r7   r:   r;   propertyr   rD   rF   rH   rI   rK   rL   r   r   r   r   r%      s"   
<
r%   r   zEhttps://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html)r?   datetimer    r   _exceptionsr   r   r   r	   r
   objectrD   r!   r   r   r    r   r%   r   rA   r   r   r   r   <module>   s   # 