|
Revision 17
(checked in by fumanchu, 6 years ago)
|
Set eol-style:native on all .py files.
|
- Property svn:eol-style set to
native
|
| Line | |
|---|
| 1 |
import datetime |
|---|
| 2 |
import re |
|---|
| 3 |
|
|---|
| 4 |
def sane_year(yearAsString, lookahead=20): |
|---|
| 5 |
"""Sanity-check years entered as less than four digits. |
|---|
| 6 |
|
|---|
| 7 |
If users want to enter years between 0 A.D. and 99 A.D., they need to |
|---|
| 8 |
enter it as a four-digit year (0000-0099). Dates entered as less than |
|---|
| 9 |
four digits take the current century, unless that places them past the |
|---|
| 10 |
current year by (lookahead) years or more, in which case they take the |
|---|
| 11 |
previous century.""" |
|---|
| 12 |
yearAsInt = int(yearAsString) |
|---|
| 13 |
if yearAsInt < 100 and len(yearAsString) < 4: |
|---|
| 14 |
currentYear = datetime.date.today().year |
|---|
| 15 |
currentCentury, rem = divmod(currentYear, 100) |
|---|
| 16 |
yearAsInt += (currentCentury * 100) |
|---|
| 17 |
if yearAsInt >= (currentYear + lookahead): |
|---|
| 18 |
yearAsInt -= 100 |
|---|
| 19 |
return yearAsInt |
|---|
| 20 |
|
|---|
| 21 |
def parse_date(dateAsString): |
|---|
| 22 |
atoms = re.split(r"\D", dateAsString) |
|---|
| 23 |
if len(atoms) != 3: |
|---|
| 24 |
raise ValueError("Date values must be in the form: mm dd yy.") |
|---|
| 25 |
atoms = sane_year(atoms[2]), int(atoms[0]), int(atoms[1]) |
|---|
| 26 |
return atoms |
|---|