B #a1@sdZddlmZddlmZddlZddlmZddlm Z ddl m Z ddl m Z dd lmZmZed Zd d Zefd dZdS)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 Cst|tstd|t|}|s|drt|dd}|jdksR|jdkrxt |j |j |j |j |j|j|jd}nt|j |j |j |j |jd}|Std||}xx|D]l\}}|d kr|dkrd ||<|d krt||dd d d||<qt||dd d d||<qW|ddkr|ddkrt|d|d|d|d|dd}|ddkrtd|}nLt |d|d|d|d|d|d|dd}|ddkrt 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 %rPNr)daysseconds microsecondsminuteshoursmonthsyears)r r rrrz"Unable to parse duration string %r) separatorsignZ0n)rr,.rrr rrr weeks)r rrr rr-)rrr rrr r) isinstancer TypeErrorISO8601_PERIOD_REGEXmatch startswithryearmonthrdaysecond microsecondminutehourrr groupdictitemsrreplacefloat)Z datestringrZdurdtretgroupskeyvalr.:/tmp/pip-target-jgxl_w8r/lib/python/isodate/isoduration.pyparse_duration9sJ        "$      r0cCs^t|tr,|jdksB|jdksB|jtdksBt|trH|tdkrHd}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 tdurationformatr*r.r.r/duration_isoformats   r3)__doc__datetimerdecimalrresixrZisodate.durationrZisodate.isoerrorrZisodate.isodatetimerZisodate.isostrfrr compilerr0r3r.r.r.r/ s       L