python - Raspberry Pi - Firestore 时间戳问题
问题描述
所以在我的项目中,我试图将时间戳发送到我的 Firestore 数据库。当我在我的python代码中测试我的日期时间时,它会给我正确的日期和时间,在将数据发送到firestore之后,数据库中的时间戳数据总是比当前时间+8小时
从 2018-05-20 07:11:19.833275 到 2018-05-20 15:11:19.833275
其他一切都是正确的,即使是时区,只有小时除外。顺便说一句,我们的时区是 UTC+8。我不明白我做错了什么。
这是我的代码:
import firebase_admin
from firebase_admin import credentials
from google.cloud import firestore
from firebase_admin import firestore
import datetime, sys
now = datetime.datetime.now()
# Use a service account
cred = credentials.Certificate('*some key*')
firebase_admin.initialize_app(cred)
db = firestore.client()
path = db.collection("*somepath*")
doc_ref = path.document()
doc_ref.set({
'date': now,
'rate': 60,
'used': 1
})
print ("current: %s" %now)
解决方案
要获得更一致的时间戳,请考虑使用 FirestoreSERVER_TIMESTAMP
常量:
doc_ref.set({
'date': firestore.SERVER_TIMESTAMP,
'rate': 60,
'used': 1
})
https://google-cloud-python.readthedocs.io/en/latest/firestore/constants.html
然后,如果需要,您可以在客户端转换您的时间戳。
推荐阅读
- python - 在 Windows 上将 pytorch 和 matplotlib 与 MKL 一起使用的正确方法是什么?
- javascript - 在for循环中使用时如何为按钮标签提供动态ID
- python - 尝试在 Python 中发送发布请求以从网站下载 csv 文件
- c# - 图像压缩导致 C# 中某些图像的方向错误
- java - 资源泄漏:'yourName' 从未关闭
- sql-server - SSRS 13 (SQL 2016) 报告缓存问题
- python - 如何将搜索功能添加到“list_filter”的输入栏?
- visual-studio-code - 在 Visual Studio Code 中,一个包含文件的文件夹会在一段时间后被删除
- java - 为什么在 Java 应用程序中使用 JPA Hibernate 元模型类?
- dataframe - 计算所有列的方差并打印结果