Guest User

Untitled

a guest
Jun 10th, 2014
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. iso_regex = re.compile(
  2.   '([0-9]{4})' #year
  3.   '-?'
  4.   '([0-9]{1,2})' #month
  5.   '(?:'
  6.     '-?'
  7.     '('
  8.       '[0-9]{1,2}' #day?
  9.     ')'
  10.     '(?:'
  11.       '[T ]'
  12.       '([0-9]{1,2})' #hour??
  13.       '(?:'
  14.         ':?'
  15.         '([0-9]{1,2})' #minute???
  16.         '(?:'
  17.           ':?'
  18.           '(?:'
  19.             '([0-9]{1,2})' #second????
  20.             '(?:'
  21.               '\\.?([0-9]+)' #ms?????
  22.             ')?'
  23.           ')'
  24.         ')?'
  25.         '(?:'
  26.           '(Z)|([+-][0-9]{1,2})' #
  27.           ':?'
  28.           '([0-9]{1,2})' #TZ???
  29.         ')'
  30.       ')?'
  31.     ')?'
  32.   ')?'
  33. )
  34.  
  35. def regex_parse_datetime(s):
  36.   match = iso_regex.match(s)
  37.   if not match:
  38.     return
  39.   groups = list(itertools.takewhile(lambda e: e is not None, match.groups()))
  40.   args = [int(g) for g in groups[:7]]
  41.   # No tzinfo
  42.   return datetime.datetime(*args)
Advertisement
Add Comment
Please, Sign In to add comment