Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- import json
- import re
- rdate = re.compile(r'/Date\((\d+)([+-]\d\d)(\d\d)\)/')
- decoder = json.JSONDecoder()
- scanstring = decoder.parse_string
- def parse_date_or_string(*args):
- s, end = scanstring(*args)
- print(s)
- m = rdate.match(s)
- if not m:
- return s, end
- ts = int(m.group(1)) / 1000
- hroff = int(m.group(2))
- minoff = int(m.group(3))
- off = datetime.timedelta(hours=hroff, minutes=minoff)
- tz = datetime.timezone(off)
- return datetime.datetime.fromtimestamp(ts).replace(tzinfo=tz), end
- decoder.parse_string = parse_date_or_string
- decoder.scan_once = json.scanner.py_make_scanner(decoder)
- print(decoder.decode("""{"timestamp": "/Date(1405961743000+0100)/"}"""))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement