SHOW:
|
|
- or go back to the newest paste.
| 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')
|
| 10 | + | |
| 11 | print(s) | |
| 12 | m = rdate.match(s) | |
| 13 | if not m: | |
| 14 | return s, end | |
| 15 | ts = int(m.group(1)) / 1000 | |
| 16 | hroff = int(m.group(2)) | |
| 17 | minoff = int(m.group(3)) | |
| 18 | off = datetime.timedelta(hours=hroff, minutes=minoff) | |
| 19 | tz = datetime.timezone(off) | |
| 20 | return datetime.datetime.fromtimestamp(ts).replace(tzinfo=tz), end | |
| 21 | decoder.parse_string = parse_date_or_string | |
| 22 | decoder.scan_once = json.scanner.py_make_scanner(decoder) | |
| 23 | ||
| 24 | print(decoder.decode("""{"timestamp": "/Date(1405961743000+0100)/"}""")) |