Closed GoogleCodeExporter closed 8 years ago
pysimplesoap can not parses the most common forms of ISO 8601 date strings
(e.g. 2012-02-10T00:00:00.0000000+08:00) into datetime objects.
import time
import iso8601
dt = iso8601.parse_date('2012-02-10T00:00:00.0000000+08:00')
timestamp = time.mktime(dt.timetuple())
Original comment by jinyinq...@gmail.com
on 9 Feb 2012 at 12:14
import datetime
# Functions to serialize/unserialize special immutable types:
def datetime_u(s):
fmt = "%Y-%m-%dT%H:%M:%S"
try:
return datetime.datetime.strptime(s, fmt)
except ValueError:
try:
# strip utc offset
if s[-3] == ":" and s[-6] in (' ', '-', '+'):
s = s[:-6]
# parse microseconds
return datetime.datetime.strptime(s, fmt + ".%f")
except ValueError:
# strip microseconds (not supported in this platform)
if s[-4] == ".":
s = s[:-4]
return datetime.datetime.strptime(s, fmt)
datetimestring1 = '2010-11-21T00:00:00+08:00'
datetimestring2 = '2012-02-10T00:00:00.0000000+08:00'
print datetime_u(datetimestring1)
print datetime_u(datetimestring2)
--------------------------------------------------------------
2010-11-21 00:00:00
Traceback (most recent call last):
File "1.py", line 24, in <module>
print datetime_u(datetimestring2)
File "1.py", line 19, in datetime_u
return datetime.datetime.strptime(s, fmt)
File "D:\Python25\lib\_strptime.py", line 333, in strptime
data_string[found.end():])
ValueError: unconverted data remains: .0000000
Original comment by jinyinq...@gmail.com
on 9 Feb 2012 at 1:20
You can use iso8601 module to parse the ISO 8601 date format.
datetimestring1 = '2010-11-21T00:00:00+08:00'
datetimestring2 = '2012-02-10T00:00:00.0000000+08:00'
import iso8601
datetime_u = iso8601.parse_date
print datetime_u(datetimestring1)
print datetime_u(datetimestring2)
---------------------------------------
2010-11-21 00:00:00+08:00
2012-02-10 00:00:00+08:00
Original comment by jinyinq...@gmail.com
on 9 Feb 2012 at 1:26
iso8601 seems not to be an stdlib package.
Do you want to make a patch to both support datetime and iso8601?
I've given you commit access if you want to patch it and close this ticket ;-)
Original comment by reingart@gmail.com
on 10 Feb 2012 at 10:52
I use the trunk version to strip microseconds now.
Original comment by jinyinq...@gmail.com
on 18 Feb 2012 at 5:29
Original issue reported on code.google.com by
jinyinq...@gmail.com
on 9 Feb 2012 at 10:15