python - 如何在 python 中的 google cloud firestore 中为时间戳创建条目

标签 python google-cloud-platform google-cloud-firestore google-cloud-functions

我有一个基于 python 3.7 的云函数。自纪元以来,我有秒的值(value)。我可以为文档添加一系列条目,如字符串、整数等。但我无法添加时间戳。

这是一个简单的代码片段。

db = firestore.Client(project=project_id)

# the event dictionary already has some values in it.  Add a timestamp.
#

# This is a reference to javascript api.  Oddly enough this page does not have
# python listed.
# https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp
#
# This will add a timestamp as a string value.
event['mqtt_timestamp_rfc3339'] = mqtt_timestamp_rfc3339 # add the timestamp as a string

# this adds a the value as a number of seconds since epoch
event['timestamp_secs'] = mqtt_timestamp  # add the timestamp as timestamp class

# This fails.
# firestore.types.Value.timestamp_value(seconds=mqtt_timestamp.timestamp())  

# This actually updates the collection
doc_ref = db.collection(u'sensor-data').add(event)

到目前为止,我从示例代码中看到的唯一一件事是添加了一个似乎是服务器时间的时间戳。在这种情况下,我有一个设备将其信息中继到第二个设备,该设备又更新到发布/订阅。我真的不关心什么时候调用云函数。我想知道第一台设备何时生成事件,这可能是在相当长的几分钟之前。

最佳答案

使用 firebase 时间戳函数,但它会按区域节省服务器时间..

client.collection('sensor-data').document('mydoc').set({
    'mytime':firestore.SERVER_TIMESTAMP
})

关于python - 如何在 python 中的 google cloud firestore 中为时间戳创建条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63142674/

相关文章:

python - 为什么 `print("Hello, World !")` 和 `print("Hello", "World!")` 产生不同的输出?

Python:无法通过键盘中断杀死进程?

python - Maya - 查询以前的渲染信息

google-cloud-platform - 看不到 CloudRun 监控指标 - 缺少什么 IAM 策略?

google-app-engine - 无法在 Google Cloud 上创建实例 - "zone..."没有足够的资源

javascript - 访问 GeoFirestore 实用函数 (calculateDistance)

javascript - Firestore 时间戳属性在管理 SDK 上带有下划线前缀,但在客户端 SDK 中则没有

python - 使用 matplotlib.pyplot 指定 x 轴的值?

apache-spark - 如何将数据从 Cassandra 导出到 BigQuery

javascript - 如何将Firebase上传图像的图像downloadURL转换为base64