a 97a3@s|dZddlmZmZmZmZddlZddlmZddlm Z ddl Z ddl Z ddgZ dd Z Gd ddeZeZejZdS) z This module offers a parser for ISO-8601 strings It is intended to support all valid date, time and datetime formats per the ISO-8601 specification. ..versionadded:: 2.7.0 )datetime timedeltatimedateN)tzwrapsisoparse isoparsercstfdd}|S)Nc stdfddttjrjzdWn8tyh}z d}tt||WYd}~n d}~00|g|Ri|S)NreadcsSNr str_inr t/private/var/folders/s6/9n5zrl012gv99k63s4q6ccsd4s6mqz/T/pip-target-f5cq3f2q/lib/python/dateutil/parser/isoparser.pyz,_takes_ascii..func..asciiz5ISO-8601 strings should contain only ASCII characters)getattr isinstancesix text_typeencodeUnicodeEncodeError raise_from ValueError)selfrargskwargsemsgfrrfuncs &z_takes_ascii..funcr)r"r#r r!r _takes_asciisr$c@seZdZdddZeddZeddZedd Zedd d Zd Z dZ e dZ ddZddZddZddZddZdddZdS)r NcCsD|dur:t|dks(t|dks(|dvr0td|d}||_dS)z :param sep: A single character that separates date and time portions. If ``None``, the parser will accept any single character. For strict ISO-8601 adherence, pass ``'T'``. N 0123456789z7Separator must be a single, non-numeric ASCII characterr)lenordrr_sep)rsepr r r__init__+s   zisoparser.__init__cCs||\}}t||kr^|jdus:|||d|jkrV||||dd7}ntdt|dkr|ddkrd|d<t|tddSt|S)u+ Parse an ISO-8601 datetime string into a :class:`datetime.datetime`. An ISO-8601 datetime string consists of a date portion, followed optionally by a time portion - the date and time portions are separated by a single character separator, which is ``T`` in the official standard. Incomplete date formats (such as ``YYYY-MM``) may *not* be combined with a time portion. Supported date formats are: Common: - ``YYYY`` - ``YYYY-MM`` or ``YYYYMM`` - ``YYYY-MM-DD`` or ``YYYYMMDD`` Uncommon: - ``YYYY-Www`` or ``YYYYWww`` - ISO week (day defaults to 0) - ``YYYY-Www-D`` or ``YYYYWwwD`` - ISO week and day The ISO week and day numbering follows the same logic as :func:`datetime.date.isocalendar`. Supported time formats are: - ``hh`` - ``hh:mm`` or ``hhmm`` - ``hh:mm:ss`` or ``hhmmss`` - ``hh:mm:ss.ssssss`` (Up to 6 sub-second digits) Midnight is a special case for `hh`, as the standard supports both 00:00 and 24:00 as a representation. The decimal separator can be either a dot or a comma. .. caution:: Support for fractional components other than seconds is part of the ISO-8601 standard, but is not currently implemented in this parser. Supported time zone offset formats are: - `Z` (UTC) - `±HH:MM` - `±HHMM` - `±HH` Offsets will be represented as :class:`dateutil.tz.tzoffset` objects, with the exception of UTC, which will be represented as :class:`dateutil.tz.tzutc`. Time zone offsets equivalent to UTC (such as `+00:00`) will also be represented as :class:`dateutil.tz.tzutc`. :param dt_str: A string or stream containing only an ISO-8601 datetime string :return: Returns a :class:`datetime.datetime` representing the string. Unspecified components default to their lowest value. .. warning:: As of version 2.7.0, the strictness of the parser should not be considered a stable part of the contract. Any valid ISO-8601 string that parses correctly with the default settings will continue to parse correctly in future versions, but invalid strings that currently fail (e.g. ``2017-01-01T00:00+00:00:00``) are not guaranteed to continue failing in future versions if they encode a valid date. .. versionadded:: 2.7.0 Nr%z&String contains unknown ISO componentsrdays)_parse_isodater(r*_parse_isotimerrr)rdt_str componentsposr r rr ;sK  zisoparser.isoparsecCs:||\}}|t|kr2tdd|dt|S)z Parse the date portion of an ISO string. :param datestr: The string portion of an ISO string, without a separator :return: Returns a :class:`datetime.date` object zString contains unknown ISO zcomponents: {!r}r)r1r(rformatdecoder)rZdatestrr4r5r r r parse_isodates  zisoparser.parse_isodatecCs&||}|ddkrd|d<t|S)z Parse the time portion of an ISO string. :param timestr: The time portion of an ISO string, without a separator :return: Returns a :class:`datetime.time` object rr.)r2r)rtimestrr4r r r parse_isotimes  zisoparser.parse_isotimeTcCs|j||dS)a Parse a valid ISO time zone string. See :func:`isoparser.isoparse` for details on supported formats. :param tzstr: A string representing an ISO time zone offset :param zero_as_utc: Whether to return :class:`dateutil.tz.tzutc` for zero-offset zones :return: Returns :class:`dateutil.tz.tzoffset` for offsets and :class:`dateutil.tz.tzutc` for ``Z`` and (if ``zero_as_utc`` is specified) offsets equivalent to UTC. ) zero_as_utc) _parse_tzstr)rtzstrr;r r r parse_tzstrszisoparser.parse_tzstr-:s [\.,]([0-9]+)cCs.z ||WSty(||YS0dSr )_parse_isodate_commonr_parse_isodate_uncommon)rr3r r rr1s  zisoparser._parse_isodatecCst|}gd}|dkr tdt|dd|d<d}||krH||fS|||d|jk}|rj|d7}||dkr~tdt|||d|d<|d7}||kr|r||fStd|r|||d|jkrtd |d7}||dkrtd t|||d|d<||dfS) N)r%r%r%ISO string too shortrr%zInvalid common monthzInvalid ISO formatzInvalid separator in ISO stringzInvalid common day)r(rint _DATE_SEP)rr3len_strr4r5has_sepr r rrAs6  zisoparser._parse_isodate_commonc Cstt|dkrtdt|dd}|dd|jk}d|}|||ddkr|d7}t|||d}|d7}d}t||kr|||d|jk|krtd||7}t|||d}|d7}||||}nt||d krtd t|||d }|d 7}|dks.|d t|krBtd d ||t|ddt |dd }|j |j |j g} | |fS)NrCrDrr%WrEz"Inconsistent use of dash separatorr-zInvalid ordinal dayimz {} for year {}r/) r(rrFrG_calculate_weekdatecalendarisleapr6rryearmonthday) rr3rOrIr5ZweeknoZdaynoZ base_dateZ ordinal_dayr4r r rrBs8   z!isoparser._parse_isodate_uncommoncCsd|krdks$ntd|d|kr8dksHntd|t|dd}|t|ddd }|dd |d}|t|d S) a Calculate the day of corresponding to the ISO year-week-day calendar. This function is effectively the inverse of :func:`datetime.date.isocalendar`. :param year: The year in the ISO calendar :param week: The week in the ISO calendar - range is [1, 53] :param day: The day in the ISO calendar - range is [1 (MON), 7 (SUN)] :return: Returns a :class:`datetime.date` r6zInvalid week: {}zInvalid weekday: {}r%rCrEr/)rr6rr isocalendar)rrOweekrQZjan_4Zweek_1Z week_offsetr r rrL)s zisoparser._calculate_weekdatec Cst|}gd}d}d}|dkr(tdd}||krf|dkrf|d7}|||dd vrz|||d|d<|}qf|dkr|||d|jkrd }|d7}n2|dkr|r|||d|jkrtd |d7}|d krt|||d||<|d7}|d kr,|j||d}|s&q,|ddd }t|dd t|||<|t|7}q,||krxtd|ddkrtdd|ddDrtd|S)N)rrrrNrrEzISO time too shortFrJr%s-+ZzTz#Inconsistent use of colon separatorr- zUnused components in ISO stringr.css|]}|dkVqdS)rNr ).0 componentr r r zrz+isoparser._parse_isotime..rCz#Hour may only be 24 at 24:00:00.000) r(rr< _TIME_SEPrF_FRACTION_REGEXmatchgroupany) rr9rHr4r5comprIfracZus_strr r rr2JsH    zisoparser._parse_isotimecCs|dks|dkrtjSt|dvr*td|dddkr@d}n|ddd krVd}ntd t|dd }t|d krd}n&t||d d |jkrd nd d}|r|dkr|dkrtjS|d krtd|dkrtdtd||d|dSdS)NZz>r-rJrXz0Time zone offset must be 1, 3, 5 or 6 charactersrr%r?rW+zTime zone offset requires signr-rC;z#Invalid minutes in time zone offsetz!Invalid hours in time zone offset<)rUTCr(rrFr]tzoffset)rr=r;Zmulthoursminutesr r rr<s(  &zisoparser._parse_tzstr)N)T)T)__name__ __module__ __qualname__r,r$r r8r:r>rGr]recompiler^r1rArBrLr2r<r r r rr *s$  X    ),!5)__doc__rrrrrMZdateutilr functoolsrrqr__all__r$objectr ZDEFAULT_ISOPARSERr r r r rs  w