gjhiggins/rdflib — Issue #45: toPython() doesn't convert datetimes with millis
Author: gjhiggins
Created: 2011-10-19 01:04:58+00:00
State: open

Originally filed by ed.summers on 2009-02-17T10:55:06

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