我有一个基于python 3.7的云功能。自纪元以来,我有一个秒数的值。我可以为文档添加一系列条目,例如字符串,整数等。但是我无法添加时间戳。
这是一个简单的代码片段。
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)
到目前为止,与示例代码相比,我唯一看到的就是添加一个时间戳,该时间戳似乎是服务器时间。在这种情况下,我有一个将其信息中继到第二个设备的设备,第二个设备又将其更新为pub / sub。我真的不在乎何时调用云函数。我想知道第一台设备何时生成了事件,而这可能要花几分钟的时间。
此方法适用于使用python 3.7的云功能。
doit():
# stuff snipped for brevity
# unpack timestamp as string from buffer
mqtt_timestamp_str = mqtt_timestamp_bytes.decode("utf-8")
# generate timestamp from string
mqtt_timestamp = datetime.fromisoformat(mqtt_timestamp_str)
# create as RFC339 string
mqtt_timestamp_rfc3339 = rfc3339(mqtt_timestamp, utc=True, use_system_timezone=False)
# stuff snipped. event is a dictionary with stuff to put in firebase
# add the timestamp as RFC339 String
event['mqtt_timestamp_rfc3339'] = mqtt_timestamp_rfc3339
event['timestamp'] = mqtt_timestamp
# update firebase
doc_ref = db.collection(u'sensor-data').add(event)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句