RethinkDB是一个很棒且非常方便的 NoSQL 数据库引擎。我正在寻找插入 Python 日期时间对象的最佳方法。RethinkDB 会存储 UTC 时间戳,因此我找到了一种解决方案,可以将我的 datetime 对象转换为正确的格式。
我使用这个小函数将我的 datetime 对象转换为 somethink RethinkDB 理解:
import calendar
from datetime import datetime
import rethinkdb as r
def datetime_to_epoch_time(dt):
timestamp = calendar.timegm(dt.utctimetuple())
return r.epoch_time(timestamp)
title = u'foobar'
published_at = '2014-03-17 14:00'
# firts I convert 2014-03-17 14:00 to datetime
dt = datetime.strptime(published_at, '%Y-%m-%d %H:%M')
# then I store the result
r.table('stories').insert({
'title': title,
'published_at': datetime_to_epoch_time(dt),
}).run()
我当前的时区是 CET(格林威治标准时间 + 2 小时) 这是将我的日期存储在 rethinkdb 中的好解决方案还是存在更好的解决方案?
谢谢你的帮助