RDFLib/rdflib — Issue #44: toPython() doesn't convert datetimes with millis
Author: gjhiggins
Created: 2012-02-20 08:31:18+00:00
State: closed

ed.summers, 2009-02-17T10:55:06.000Z

It's nice that toPython() will convert dateTime XSD datatypes to datetime python objects:

>>> from rdflib import Literal , Namespace
>>> from datetime import datetime
>>> XSD_NS = Namespace(u'http://www.w3.org/2001/XMLSchema#')
>>> Literal('2008-02-09T10:46:29', datatype=XSD_NS.dateTime).toPython()
datetime.datetime(2008, 2, 9, 10, 46, 29)

But if you add milliseconds it doesn't seem to work:

>>> Literal('2009-02-17T05:48:53.203307', datatype=XSD_NS.dateTime).toPython()
rdflib.Literal('2009-02-17T05:48:53.203307', language=None,
datatype=rdflib.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))

This bit me recently when I couldn't figure out why adding triples with datetime.now() wasn't
working as expected:

>>> Literal(datetime.now()).toPython()
rdflib.Literal('2009-02-17T05:52:13.612998', language=None,
datatype=rdflib.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))

It appears that RDFAlchemy had to hack around this problem:
http://www.openvest.com/trac/wiki/CustomizingLiterals

Comment 1 by eik...@gmail.com

Comment 2 by eik...@gmail.com
Ed, Do you already have a patch for this issue?

Comment 3 by ed.summers
Yeah, looks like I committed as part of r1690?

Comment 4 by ed.summers
Actually that was the improved test, this was the fix r1689.

Comment 5 by ed.summers

Comment 6 by ed.summers
This issue was closed by revision 7071b586b8f8.

Comment 7 by ed.summers
This issue was updated by revision 171938e58cad.

Improved testing of what happens to datetime's with microseconds when
Python version < 2.6.0