B ¨#a1ã@s†dZddlmZddlmZddlZddlmZddlm Z ddl m Z ddl m Z dd lmZmZe d ¡Zd d „Zefd d„ZdS)zà This module provides an ISO 8601:2004 duration parser. It also provides a wrapper to strftime. This wrapper makes it easier to format timedelta or Duration instances as ISO conforming strings. é)Ú timedelta)ÚDecimalN)Ú string_types)ÚDuration)Ú ISO8601Error)Úparse_datetime)ÚstrftimeÚ D_DEFAULTa^(?P[+-])?P(?!\b)(?P[0-9]+([,.][0-9]+)?Y)?(?P[0-9]+([,.][0-9]+)?M)?(?P[0-9]+([,.][0-9]+)?W)?(?P[0-9]+([,.][0-9]+)?D)?((?PT)(?P[0-9]+([,.][0-9]+)?H)?(?P[0-9]+([,.][0-9]+)?M)?(?P[0-9]+([,.][0-9]+)?S)?)?$c CsÔt|tƒstd|ƒ‚t |¡}|s¤| d¡r˜t|dd…ƒ}|jdksR|jdkrxt |j |j |j |j |j|j|jd�}nt|j |j |j |j |jd�}|Std|ƒ‚| ¡}xx| ¡D]l\}}|d kr¶|dkrÖd ||<|d k�rt||dd … d d¡ƒ||<q¶t||dd … d d¡ƒ||<q¶W|ddk�r„|ddk�r„t|d|d|d|d|dd�}|ddk�rÐtdƒ|}nLt |d|d|d|d|d|d|dd�}|ddk�rÐt dƒ|}|S)aÔ Parses an ISO 8601 durations into datetime.timedelta or Duration objects. If the ISO date string does not contain years or months, a timedelta instance is returned, else a Duration instance is returned. The following duration formats are supported: -PnnW duration in weeks -PnnYnnMnnDTnnHnnMnnS complete duration specification -PYYYYMMDDThhmmss basic alternative complete date format -PYYYY-MM-DDThh:mm:ss extended alternative complete date format -PYYYYDDDThhmmss basic alternative ordinal date format -PYYYY-DDDThh:mm:ss extended alternative ordinal date format The '-' is optional. Limitations: ISO standard defines some restrictions about where to use fractional numbers and which component and format combinations are allowed. This parser implementation ignores all those restrictions and returns something when it is able to find all necessary components. In detail: it does not check, whether only the last component has fractions. it allows weeks specified with all other combinations The alternative format does not support durations with years, months or days set to 0. zExpecting a string %rÚPéNr)ÚdaysÚsecondsÚ microsecondsÚminutesÚhoursÚmonthsÚyears)r r rrrz"Unable to parse duration string %r)Ú separatorÚsignZ0n)rréÿÿÿÿú,Ú.rrr rrr Úweeks)r rrr rrú-)rrr rrr r)Ú isinstancerÚ TypeErrorÚISO8601_PERIOD_REGEXÚmatchÚ startswithrÚyearÚmonthrÚdayÚsecondÚ microsecondÚminuteÚhourrrÚ groupdictÚitemsrÚreplaceÚfloat)Z datestringrZdurdtÚretÚgroupsÚkeyÚval©r.ú:/tmp/pip-target-jgxl_w8r/lib/python/isodate/isoduration.pyÚparse_duration9sJ        "$      r0cCs^t|tƒr,|jdksB|jdksB|jtdƒksBt|tƒrH|tdƒkrHd}nd}|t||ƒ7}|S)z• Format duration strings. This method is just a wrapper around isodate.isostrf.strftime and uses P%P (D_DEFAULT) as default format. rrÚ)rrrrZtdeltarr)Z tdurationÚformatr*r.r.r/Úduration_isoformat…s   r3)Ú__doc__ÚdatetimerÚdecimalrÚreÚsixrZisodate.durationrZisodate.isoerrorrZisodate.isodatetimerZisodate.isostrfrr Úcompilerr0r3r.r.r.r/Ú s       L