在 BOLT neo4j 驱动程序中使用 python 日期时间类型会引发错误。可用的解决方法?

using python datetime type in BOLT neo4j-driver throws an error. workaround available?

这是一个简化的代码示例,我想在 BOLT 下 运行 并且我能够执行 uner py2neo

timeCreated = datetime.datetime(year=2016, month=6, day=15, hour=23)
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
session = driver.session()
stmt = 'CREATE (y:Year) SET y.timeCreated = {time}'
session.run(stmt, {"time": timeCreated})

timeCreated 是 python 日期时间对象。我收到无法使用日期时间对象的错误消息:

 ValueError: Values of type <class 'datetime.datetime'> are not supported). 

有解决办法吗?以后会支持吗?我想使用 BOLT 驱动程序并减少对 py2Neo 的依赖,但似乎并非所有功能都可以转移。

Node4j 没有将日期或时间作为对象属性的概念;所以我怀疑是否会支持 datetime 类型。您应该改为存储纪元值:

import calendar
import time

epoch = calendar.timegm(time.gmtime())

Neo4j 本身不支持日期时间,我相信 py2neo 在以前的版本中会默默地将日期时间转换为字符串。

我的解决方法是调用 datetime 对象的 .isoformat() 方法,将它们作为字符串插入数据库。