summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabien Loffredo <flo@atolcd.com>2016-12-23 14:30:50 (GMT)
committerFabien Loffredo <flo@atolcd.com>2016-12-23 14:30:50 (GMT)
commitf609b08adde6a522fc7de9c01a77a70802d668bb (patch)
treef6050815f23ba9b2029345ab6a254f7a0df75a58
parent1ba6bed4c8675d30480461473a72b1b0287b13e3 (diff)
downloadisodate-f609b08adde6a522fc7de9c01a77a70802d668bb.tar.gz
add ability to choose default month and day when incomplete date
-rw-r--r--src/isodate/isodates.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/isodate/isodates.py b/src/isodate/isodates.py
index 092712e..aab5684 100644
--- a/src/isodate/isodates.py
+++ b/src/isodate/isodates.py
@@ -126,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.
@@ -172,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:
@@ -188,14 +192,14 @@ def parse_date(datestring, yeardigits=4, expanded=False):
elif 'day' in groups: # ordinal date
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)