Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. import datetime
  2. import json
  3. import re
  4.  
  5. rdate = re.compile(r'/Date\((\d+)([+-]\d\d)(\d\d)\)/')
  6.  
  7. decoder = json.JSONDecoder()
  8. scanstring = decoder.parse_string
  9. def parse_date_or_string(*args):
  10.     print('here')
  11.     s, end = scanstring(*args)
  12.     print(s)
  13.     m = rdate.match(s)
  14.     if not m:
  15.         return s, end
  16.     ts = int(m.group(1)) / 1000
  17.     hroff = int(m.group(2))
  18.     minoff = int(m.group(3))
  19.     off = datetime.timedelta(hours=hroff, minutes=minoff)
  20.     tz = datetime.timezone(off)
  21.     return datetime.datetime.fromtimestamp(ts).replace(tzinfo=tz), end
  22. decoder.parse_string = parse_date_or_string
  23. decoder.scan_once = json.scanner.py_make_scanner(decoder)
  24.  
  25. print(decoder.decode("""{"timestamp": "/Date(1405961743000+0100)/"}"""))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement