Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- .
- .
- def ISOdate(date):
- '''
- converts the following date string format to ISO (yyyy-mm-dd):
- 28-okt-1924 (dutch month abbreviations)
- 28 oct 1924 (english..)
- 9/nov/2012 (single digit)
- '''
- shortmonths = [
- 'jan', 'feb', 'mrt', 'apr', 'mei', 'jun',
- 'jul', 'aug', 'sep', 'okt', 'nov', 'dec',
- 'jan', 'feb', 'mar', 'apr', 'may', 'jun',
- 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'
- ]
- # Month abbrevs are only different march, may and october.
- pat = r'(d{1,2})s?[-/]?s?(w{3})s?[-/]?s?(d{4})'
- q = re.match(pat, date)
- if q:
- year = q.group(3)
- day = int(q.group(1))
- month = shortmonths.index(q.group(2).lower()) % 12 + 1
- return u'{}-{:02d}-{:02d}'.format(year, month, day)
- else:
- # just return input, date fields may be empty
- return date
- year = int(q.group(3))
- day = int(q.group(1))
- month = shortmonths.index(q.group(2).lower()) % 12 + 1
- d = datetime.datetime(year, month, day)
- return u'{:%YY-%m-%d}'.format(d)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement