
    i                         d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
mZ ddlmZ dd	lmZ  ej        d
          Z G d de          ZdgZdS )z(DURATION property type from :rfc:`5545`.    N	timedelta)AnyClassVar)Self)InvalidCalendarJCalParsingError)
Parameters   )TimeBasezF([-+]?)P(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$c                   "   e Zd ZU dZdZee         ed<   eed<   dde	ee
f         dz  deez  fdZd Zed	             Zed
efd            Zed
ee         fd            ZddlmZ ded
efdZeded
efd            Zeded
efd            ZdS )	vDurationa	  Duration

    Value Name:
        DURATION

    Purpose:
        This value type is used to identify properties that contain
        a duration of time.

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

        .. code-block:: text

            dur-value  = (["+"] / "-") "P" (dur-date / dur-time / dur-week)

            dur-date   = dur-day [dur-time]
            dur-time   = "T" (dur-hour / dur-minute / dur-second)
            dur-week   = 1*DIGIT "W"
            dur-hour   = 1*DIGIT "H" [dur-minute]
            dur-minute = 1*DIGIT "M" [dur-second]
            dur-second = 1*DIGIT "S"
            dur-day    = 1*DIGIT "D"

    Description:
        If the property permits, multiple "duration" values are
        specified by a COMMA-separated list of values.  The format is
        based on the [ISO.8601.2004] complete representation basic format
        with designators for the duration of time.  The format can
        represent nominal durations (weeks and days) and accurate
        durations (hours, minutes, and seconds).  Note that unlike
        [ISO.8601.2004], this value type doesn't support the "Y" and "M"
        designators to specify durations in terms of years and months.
        The duration of a week or a day depends on its position in the
        calendar.  In the case of discontinuities in the time scale, such
        as the change from standard time to daylight time and back, the
        computation of the exact duration requires the subtraction or
        addition of the change of duration of the discontinuity.  Leap
        seconds MUST NOT be considered when computing an exact duration.
        When computing an exact duration, the greatest order time
        components MUST be added first, that is, the number of days MUST
        be added first, followed by the number of hours, number of
        minutes, and number of seconds.

    Example:
        A duration of 15 days, 5 hours, and 20 seconds would be:

        .. code-block:: ics

            P15DT5H0M20S

        A duration of 7 weeks would be:

        .. code-block:: ics

            P7W

        .. code-block:: pycon

            >>> from icalendar.prop import vDuration
            >>> duration = vDuration.from_ical('P15DT5H0M20S')
            >>> duration
            datetime.timedelta(days=15, seconds=18020)
            >>> duration = vDuration.from_ical('P7W')
            >>> duration
            datetime.timedelta(days=49)
    DURATIONdefault_valueparamsNtdc                    t          |t                    rt                              |          }t          |t                    st          d          || _        t          |          | _        d S )Nz"Value MUST be a timedelta instance)	
isinstancestrr   	from_icalr   	TypeErrorr   r
   r   )selfr   r   s      U/root/projects/butler/venv/lib/python3.11/site-packages/icalendar/prop/dt/duration.py__init__zvDuration.__init__Z   sc    b# 	)$$R((B"i(( 	B@AAA ((    c                 z   d}| j         }|j        dk     rd}| }d}|j        rEd}|j        dz  }|j        dz  dz  }|j        dz  }|r|| dz  }|s|r
|r|| dz  }|r|| d	z  }|j        dk    rJ|rHt          |                              d
          dz   t          |                              d
          z   S t          |                              d
          dz   t          t          |j                                                d
          z   dz   t          |                              d
          z   S )N r   -Ti  <   HMSzutf-8   P   D)r   dayssecondsr   encodeabs)r   signr   timeparthoursminutesr'   s          r   to_icalzvDuration.to_icalb   sp   W7Q;;DB: 
	*HJ$&Ej4'2-Gj2oG (uKKK' *5 *W *wMMM) *wMMM)7a<<H<t99##G,,t3c(mm6J6J76S6SSSIIW%%#bg,,&&w//0  (mm""7++	,	
r   c           	      h   t                               |           }|st          d|            |                                \  }}}}}}t	          t          |pd          t          |pd          t          |pd          t          |pd          t          |pd                    }|dk    r| }|S )NzInvalid iCalendar duration: r   )weeksr&   r,   r-   r'   r   )DURATION_REGEXmatchr   groupsr   int)	icalr2   r*   r0   r&   r,   r-   r'   values	            r   r   zvDuration.from_ical~   s    $$T** 	I!"G"G"GHHH5:\\^^2eT5'7ejq//TYQejq//1%%1%%
 
 
 3;;FEr   returnc                     | j         S )z!The time delta for compatibility.)r   )r   s    r   dtzvDuration.dt   s     wr   c                 6     | t          dd                    gS )zExamples of vDuration.r   c   r   )clss    r   exampleszvDuration.examples   s!     Ia$$%%&&r   r   )VALUEnamec                     || j                                         | j                                        |                                                                 gS )zBThe jCal representation of this property according to :rfc:`7265`.)r   to_jcalr>   lowerr.   decode)r   r?   s     r   rA   zvDuration.to_jcal   sJ     K!!JLLNN!!##	
 	
r   jcalc                     t          j        |t          |            	 |                     |          S # t          t
          f$ r}t          d| |          |d}~ww xY w)zParse a jCal string to a :py:class:`datetime.timedelta`.

        Raises:
            ~error.JCalParsingError: If it can't parse a duration.zCannot parse duration.)r6   N)r	   validate_value_typer   r   
ValueErrorr   )r<   rD   es      r   parse_jcal_valuezvDuration.parse_jcal_value   sr     	,T3<<<	U==&&&O, 	U 	U 	U"#;SMMMSTT	Us   2 AAAjcal_propertyc                     t          j        ||            t          j        d          5  |                     |d                   }ddd           n# 1 swxY w Y    | |t	          j        |                    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	   validate_propertyreraise_with_path_addedrI   r
   from_jcal_property)r<   rJ   durations      r   	from_jcalzvDuration.from_jcal   s     	*=#>>>5a88 	> 	>++M!,<==H	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>s)-88
 
 	
s   AAA)N)__name__
__module____qualname____doc__r   r   r   __annotations__r
   dictr   r   r   r.   staticmethodr   propertyr9   classmethodlistr   r=   icalendar.paramr>   rA   rI   rQ    r   r   r   r      s        B BH $.M8C=---) )tCH~7L )9s? ) ) ) )
 
 
8   \& I    X 'd ' ' ' [' &%%%%%
C 
D 
 
 
 
 	UC 	UI 	U 	U 	U [	U 
d 
t 
 
 
 [
 
 
r   r   )rU   redatetimer   typingr   r   icalendar.compatibilityr   icalendar.errorr   r	   icalendar.parserr
   baser   compiler1   r   __all__r]   r   r   <module>rg      s    . . 				                       ( ( ( ( ( ( = = = = = = = = ' ' ' ' ' '      M 
q
 q
 q
 q
 q
 q
 q
 q
h -r   