diff options
Diffstat (limited to 'src/isodate/isodates.py')
-rw-r--r-- | src/isodate/isodates.py | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/isodate/isodates.py b/src/isodate/isodates.py index 37d42f8..aab5684 100644 --- a/src/isodate/isodates.py +++ b/src/isodate/isodates.py @@ -108,6 +108,10 @@ def build_date_regexps(yeardigits=4, expanded=False): cache_entry.append(re.compile(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" r"-(?P<month>[0-9]{2})" % (sign, yeardigits))) + # YYYMM or +-YYYYYYMM ... basic incomplete month date format + cache_entry.append(re.compile(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" + r"(?P<month>[0-9]{2})" + % (sign, yeardigits))) # 6. year dates: # YYYY or +-YYYYYY ... reduced accuracy specific year cache_entry.append(re.compile(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" @@ -122,7 +126,9 @@ def build_date_regexps(yeardigits=4, expanded=False): return DATE_REGEX_CACHE[(yeardigits, expanded)] -def parse_date(datestring, yeardigits=4, expanded=False): +def parse_date( + datestring, + yeardigits=4, expanded=False, defaultmonth=1, defaultday=1): ''' Parse an ISO 8601 date string into a datetime.date object. @@ -143,6 +149,7 @@ def parse_date(datestring, yeardigits=4, expanded=False): YYYY-DDD +-YYYYYY-DDD extended ordinal date YYYYWww +-YYYYYYWww basic incomplete week date YYYY-Www +-YYYYYY-Www extended incomplete week date + YYYMM +-YYYYYYMM basic incomplete month date YYY-MM +-YYYYYY-MM incomplete month date YYYY +-YYYYYY incomplete year date YY +-YYYY incomplete century date @@ -167,7 +174,9 @@ def parse_date(datestring, yeardigits=4, expanded=False): # FIXME: negative dates not possible with python standard types sign = (groups['sign'] == '-' and -1) or 1 if 'century' in groups: - return date(sign * (int(groups['century']) * 100 + 1), 1, 1) + return date( + sign * (int(groups['century']) * 100 + 1), + defaultmonth, defaultday) if 'month' not in groups: # weekdate or ordinal date ret = date(sign * int(groups['year']), 1, 1) if 'week' in groups: @@ -181,16 +190,16 @@ def parse_date(datestring, yeardigits=4, expanded=False): (((isotuple[1] == 1) and 1) or 0), days=-isotuple[2] + days) elif 'day' in groups: # ordinal date - return ret + timedelta(days=int(groups['day'])-1) + return ret + timedelta(days=int(groups['day']) - 1) else: # year date - return ret + return ret.replace(month=defaultmonth, day=defaultday) # year-, month-, or complete date if 'day' not in groups or groups['day'] is None: - day = 1 + day = defaultday else: day = int(groups['day']) return date(sign * int(groups['year']), - int(groups['month']) or 1, day) + int(groups['month']) or defaultmonth, day) raise ISO8601Error('Unrecognised ISO 8601 date format: %r' % datestring) |