Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. import re
  2. .
  3. .
  4.  
  5.  
  6. def ISOdate(date):
  7. '''
  8. converts the following date string format to ISO (yyyy-mm-dd):
  9. 28-okt-1924 (dutch month abbreviations)
  10. 28 oct 1924 (english..)
  11. 9/nov/2012 (single digit)
  12. '''
  13.  
  14. shortmonths = [
  15. 'jan', 'feb', 'mrt', 'apr', 'mei', 'jun',
  16. 'jul', 'aug', 'sep', 'okt', 'nov', 'dec',
  17. 'jan', 'feb', 'mar', 'apr', 'may', 'jun',
  18. 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'
  19. ]
  20.  
  21. # Month abbrevs are only different march, may and october.
  22.  
  23. pat = r'(d{1,2})s?[-/]?s?(w{3})s?[-/]?s?(d{4})'
  24.  
  25. q = re.match(pat, date)
  26. if q:
  27. year = q.group(3)
  28. day = int(q.group(1))
  29. month = shortmonths.index(q.group(2).lower()) % 12 + 1
  30. return u'{}-{:02d}-{:02d}'.format(year, month, day)
  31. else:
  32. # just return input, date fields may be empty
  33. return date
  34.  
  35. year = int(q.group(3))
  36. day = int(q.group(1))
  37. month = shortmonths.index(q.group(2).lower()) % 12 + 1
  38. d = datetime.datetime(year, month, day)
  39. return u'{:%YY-%m-%d}'.format(d)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement