gpt4 book ai didi

python - 在 rethinkdb 中插入 python datetime 的最佳方法是什么?

转载 作者:太空狗 更新时间:2023-10-30 01:01:43 25 4
gpt4 key购买 nike

RethinkDB是一个非常好用的 NoSQL 数据库引擎。我正在寻找插入 Python 日期时间对象的最佳方式。 RethinkDB 存储 UTC 时间戳,因此我找到了一种将我的日期时间对象转换为正确格式的解决方案。

我使用这个小函数以 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(GMT + 2 小时)这是将我的日期存储在 rethinkdb 中的好解决方案还是存在更好的解决方案?

谢谢你的帮助

最佳答案

Pytz 的例子:

from datetime import datetime
import pytz

import rethinkdb as r


# Init
r.connect('localhost', 28015).repl()
if 'test' in r.db_list().run():
r.db_drop('test').run()

r.db_create('test').run()
r.db('test').table_create('stories').run()

paris = pytz.timezone('Europe/Paris')

r.table('stories').insert({
'title': u'Foobar',
'published_at': paris.localize(datetime.strptime(
'2014-03-17 14:00', '%Y-%m-%d %H:%M'
), is_dst=False)
}).run()

for document in r.table("stories").run():
print(document['published_at'])
print(type(document['published_at']))

关于python - 在 rethinkdb 中插入 python datetime 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22931665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com