summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel <g2p.code@gmail.com>2015-01-20 17:52:55 +0100
committerGabriel <g2p.code@gmail.com>2015-01-20 18:00:29 +0100
commit2e68c720d50806b91b3d025eb5cbafd3a0957b44 (patch)
treebc5a2aa1114c01c6e40151654bdfa0e78a1bc4a5
parentb9917aff3319ec112cf59c1f5ca84fa1bd3a7538 (diff)
downloadisodate-2e68c720d50806b91b3d025eb5cbafd3a0957b44.tar.gz
Don't match garbage characters at the end of date strings
Addresses #15.
-rw-r--r--src/isodate/isodates.py54
1 files changed, 28 insertions, 26 deletions
diff --git a/src/isodate/isodates.py b/src/isodate/isodates.py
index 6660245..a58a2c0 100644
--- a/src/isodate/isodates.py
+++ b/src/isodate/isodates.py
@@ -67,50 +67,52 @@ def build_date_regexps(yeardigits=4, expanded=False):
sign = 1
else:
sign = 0
+ def add_re(regex_text):
+ cache_entry.append(re.compile('\A' + regex_text + '\Z'))
# 1. complete dates:
# YYYY-MM-DD or +- YYYYYY-MM-DD... extended date format
- cache_entry.append(re.compile(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})"
- r"-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})"
- % (sign, yeardigits)))
+ add_re(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})"
+ r"-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})"
+ % (sign, yeardigits))
# YYYYMMDD or +- YYYYYYMMDD... basic date format
- cache_entry.append(re.compile(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})"
- r"(?P<month>[0-9]{2})(?P<day>[0-9]{2})"
- % (sign, yeardigits)))
+ add_re(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})"
+ r"(?P<month>[0-9]{2})(?P<day>[0-9]{2})"
+ % (sign, yeardigits))
# 2. complete week dates:
# YYYY-Www-D or +-YYYYYY-Www-D ... extended week date
- cache_entry.append(re.compile(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})"
- r"-W(?P<week>[0-9]{2})-(?P<day>[0-9]{1})"
- % (sign, yeardigits)))
+ add_re(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})"
+ r"-W(?P<week>[0-9]{2})-(?P<day>[0-9]{1})"
+ % (sign, yeardigits))
# YYYYWwwD or +-YYYYYYWwwD ... basic week date
- cache_entry.append(re.compile(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})W"
- r"(?P<week>[0-9]{2})(?P<day>[0-9]{1})"
- % (sign, yeardigits)))
+ add_re(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})W"
+ r"(?P<week>[0-9]{2})(?P<day>[0-9]{1})"
+ % (sign, yeardigits))
# 3. ordinal dates:
# YYYY-DDD or +-YYYYYY-DDD ... extended format
# YYYYDDD or +-YYYYYYDDD ... basic format
- cache_entry.append(re.compile(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})"
- r"-?(?P<day>[0-9]{3})"
- % (sign, yeardigits)))
+ add_re(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})"
+ r"-?(?P<day>[0-9]{3})"
+ % (sign, yeardigits))
# 4. week dates:
# YYYY-Www or +-YYYYYY-Www ... extended reduced accuracy week date
# YYYYWww or +-YYYYYYWww ... basic reduced accuracy week date
- cache_entry.append(re.compile(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})"
- r"-?W(?P<week>[0-9]{2})"
- % (sign, yeardigits)))
+ add_re(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})"
+ r"-?W(?P<week>[0-9]{2})"
+ % (sign, yeardigits))
# 5. month dates:
# YYY-MM or +-YYYYYY-MM ... reduced accuracy specific month
- cache_entry.append(re.compile(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})"
- r"-(?P<month>[0-9]{2})"
- % (sign, yeardigits)))
+ add_re(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})"
- % (sign, yeardigits)))
+ add_re(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})"
+ % (sign, yeardigits))
# 7. century dates:
# YY or +-YYYY ... reduced accuracy specific century
- cache_entry.append(re.compile(r"(?P<sign>[+-]){%d}"
- r"(?P<century>[0-9]{%d})"
- % (sign, yeardigits - 2)))
+ add_re(r"(?P<sign>[+-]){%d}"
+ r"(?P<century>[0-9]{%d})"
+ % (sign, yeardigits - 2))
DATE_REGEX_CACHE[(yeardigits, expanded)] = cache_entry
return DATE_REGEX_CACHE[(yeardigits, expanded)]