首页 > 解决方案 > 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)

标签: pythontimestampgoogle-cloud-firestoreraspberry-pi3

解决方案


要获得更一致的时间戳,请考虑使用 FirestoreSERVER_TIMESTAMP常量:

doc_ref.set({
    'date': firestore.SERVER_TIMESTAMP,
    'rate': 60,
    'used': 1
})

https://google-cloud-python.readthedocs.io/en/latest/firestore/constants.html

然后,如果需要,您可以在客户端转换您的时间戳。


推荐阅读