-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrfc3339.py
More file actions
30 lines (21 loc) · 806 Bytes
/
rfc3339.py
File metadata and controls
30 lines (21 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# An incomplete implementation of RFC3339 without support for timezones.
# Parses into standard datetime objects.
import datetime as dt
import re
re_date_str = r'(\d\d\d\d)-(\d\d)-(\d\d)'
re_time_str = r'(\d\d):(\d\d):(\d\d)(\.(\d+))?[zZ]'
def make_re(*parts):
return re.compile(r'^\s*' + ''.join(parts) + r'\s*$')
re_datetime = make_re(re_date_str, r'[ tT]', re_time_str)
def parse_datetime(s):
m = re_datetime.match(s)
if m:
y, m, d, hour, min, sec, i1, fsec = m.groups()
if fsec:
msec = int(float("0."+fsec) * 1000000)
else:
msec = 0
return dt.datetime(int(y), int(m), int(d),
int(hour), int(min), int(sec), msec)
if __name__ == '__main__':
print parse_datetime("2010-10-15T11:03:47.681Z")