o
    .j@"                     @   s   d Z ddlmZmZmZ ddlmZmZ ddlmZ ddl	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 G dd deZdgZdS )z&PERIOD property type from :rfc:`5545`.    )datedatetime	timedelta)AnyClassVar)Self)JCalParsingError)
Parameters)tzp)is_datetimenormalize_pytz   )TimeBase)	vDatetime)	vDurationc                   @   sL  e Zd ZU dZdZee ed< eed< e	ed< e
ed< e
ed< eed< 		d(d
ee
e
eB f deeef d	B fddZdd Zdd Zed(ddZdd Zedd Zedee
ee
B f fddZddlmZ edee fddZddlmZ dedefd d!Zed"eeB dee
e
f ee
ef B fd#d$Z ed%edefd&d'Z!d	S ))vPerioda 	  Period of Time

    Value Name:
        PERIOD

    Purpose:
        This value type is used to identify values that contain a
        precise period of time.

    Format Definition:
        This value type is defined by the following notation:

        .. code-block:: text

            period     = period-explicit / period-start

           period-explicit = date-time "/" date-time
           ; [ISO.8601.2004] complete representation basic format for a
           ; period of time consisting of a start and end.  The start MUST
           ; be before the end.

           period-start = date-time "/" dur-value
           ; [ISO.8601.2004] complete representation basic format for a
           ; period of time consisting of a start and positive duration
           ; of time.

    Description:
        If the property permits, multiple "period" values are
        specified by a COMMA-separated list of values.  There are two
        forms of a period of time.  First, a period of time is identified
        by its start and its end.  This format is based on the
        [ISO.8601.2004] complete representation, basic format for "DATE-
        TIME" start of the period, followed by a SOLIDUS character
        followed by the "DATE-TIME" of the end of the period.  The start
        of the period MUST be before the end of the period.  Second, a
        period of time can also be defined by a start and a positive
        duration of time.  The format is based on the [ISO.8601.2004]
        complete representation, basic format for the "DATE-TIME" start of
        the period, followed by a SOLIDUS character, followed by the
        [ISO.8601.2004] basic format for "DURATION" of the period.

    Example:
        The period starting at 18:00:00 UTC, on January 1, 1997 and
        ending at 07:00:00 UTC on January 2, 1997 would be:

        .. code-block:: ics

            19970101T180000Z/19970102T070000Z

        The period start at 18:00:00 on January 1, 1997 and lasting 5 hours
        and 30 minutes would be:

        .. code-block:: ics

            19970101T180000Z/PT5H30M

        .. code-block:: pycon

            >>> from icalendar.prop import vPeriod
            >>> period = vPeriod.from_ical('19970101T180000Z/19970102T070000Z')
            >>> period = vPeriod.from_ical('19970101T180000Z/PT5H30M')
    PERIODdefault_valueparamsby_durationstartenddurationNperc                 C   s   |\}}t |ttfstdt |tttfstdt |t}|r+|}t|| }n|}t|| }||kr;tdt|pAddi| _| j	| || _
|| _|| _|| _d S )Nz/Start value MUST be a datetime or date instancez>end_or_duration MUST be a datetime, date or timedelta instancez#Start time is greater than end timevaluer   )
isinstancer   r   	TypeErrorr   r   
ValueErrorr	   r   update_tzid_fromr   r   r   r   )selfr   r   r   end_or_durationr   r   r    r!   M/home/thesage/.local/lib/python3.10/site-packages/icalendar/prop/dt/period.py__init__X   s*   

zvPeriod.__init__c                 C   s4   | j |j kr|| S | j |j   ko| jk S   S N)r   overlapsr   )r   otherr!   r!   r"   r%   x   s   
zvPeriod.overlapsc                 C   sF   | j rt| j d t| j  S t| j d t| j  S )N   /)r   r   r   to_icalr   r   r   r   r!   r!   r"   r(   }   s    zvPeriod.to_icalc              
   C   sj   ddl m} z| d\}}|j||d}|j||d}W ||fS  ty4 } ztd|  |d }~ww )Nr   )	vDDDTypes/)timezonezExpected period format, got: )icalendar.prop.dt.typesr*   split	from_ical	Exceptionr   )icalr,   r*   r   r    er!   r!   r"   r/      s   zvPeriod.from_icalc                 C   s*   | j r	| j| jfn| j| jf}d|dS )NzvPeriod())r   r   r   r   )r   pr!   r!   r"   __repr__   s   zvPeriod.__repr__c                 C   s   | j | jr	| jfS | jfS )z-Make this cooperate with the other vDDDTypes.)r   r   r   r   r)   r!   r!   r"   dt   s   z
vPeriod.dtreturnc                 C   s   | j S )zv
        Returns the period as a tuple of its start datetime
        and either its end datetime or duration.
        )r6   r)   r!   r!   r"   
ical_value   s   zvPeriod.ical_valuer   )FBTYPEc              	   C   sD   t tdddddtdddft tdddddtdddd	d
fgS )zExamples of vPeriod.i     
      #   r      )hoursminutes      )r   r   r   )clsr!   r!   r"   examples   s   "zvPeriod.examples)VALUEnamec                 C   sn   t | j|d g}| jr|t| j|d  n|t | j|d  || jjdd| j	
 |gS )zBThe jCal representation of this property according to :rfc:`7265`.T)exclude_utc)r   r   to_jcalr   appendr   r   r   r   rE   lower)r   rF   r   r!   r!   r"   rI      s
   zvPeriod.to_jcaljcalc                 C   s4  t |trd|v r|d}t |trt|dkr td| |dtd t|d }W d   n1 s7w   Y  tdM t	|d t|  |d 
drZt|d }n z	t|d }W n  tyy } z
td	| |d d|d}~ww W d   ||fS W d   ||fS 1 sw   Y  ||fS )
zParse a jCal value.

        Raises:
            ~error.JCalParsingError: If the period is not a list with exactly two items,
                or it can't parse a date-time or duration.
        r+      z-A period must be a list with exactly 2 items.)r   r   Nr   )Pz-Pz+Pz#Cannot parse date-time or duration.)r   strr.   listlenr   reraise_with_path_addedr   parse_jcal_valuevalidate_value_type
startswithr   )rC   rL   r   r    r2   r!   r!   r"   rS      sB   



zvPeriod.parse_jcal_valuejcal_propertyc                 C   s   t ||  t d | |d \}}W d   n1 sw   Y  t|}|j}|r>t||}t	|r>t||}| ||f|dS )zParse jCal from :rfc:`7265`.

        Parameters:
            jcal_property: The jCal property to parse.

        Raises:
            ~error.JCalParsingError: If the provided jCal is invalid.
           N)r   )
r   validate_propertyrR   rS   r	   from_jcal_propertytzidr
   localizer   )rC   rV   r   r    r   rZ   r!   r!   r"   	from_jcal   s   

zvPeriod.from_jcalr$   )"__name__
__module____qualname____doc__r   r   rO   __annotations__r	   boolr   r   tupledictr   r#   r%   r(   staticmethodr/   r5   propertyr6   r8   icalendar.paramr9   classmethodrP   r   rD   rE   rI   rS   r\   r!   r!   r!   r"   r      sH   
 ?
 	
	!r   N)r`   r   r   r   typingr   r   icalendar.compatibilityr   icalendar.errorr   icalendar.parserr	   icalendar.timezoner
   icalendar.toolsr   r   baser   r   r   r   r   __all__r!   r!   r!   r"   <module>   s     
b