Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python在google cloud firestore中为时间戳创建条目_Python_Google Cloud Platform_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

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

如何使用python在google cloud firestore中为时间戳创建条目,python,google-cloud-platform,google-cloud-firestore,google-cloud-functions,Python,Google Cloud Platform,Google Cloud Firestore,Google Cloud Functions,我有一个基于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 eno

我有一个基于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。我并不真正关心何时调用云函数。我想知道第一台设备何时生成事件,这可能需要相当长的时间。

使用firebase时间戳功能,但它将按区域节省服务器时间

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

此方法适用于使用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)
    

Firestore以类型化的方式存储时间戳,它不像JavaScript的毫秒或UNIX纪元那样是整数。像这样:

这意味着无论哪种语言的软件包都应注意这一点(例如,为Firebase提供预期的日期/时间数据)。例如,在Kotlin Android应用程序中,我有一个
有趣的mapDateToTimestamp(date:date):Timestamp
函数,它将
java.util.date
转换为
com.google.firebase.Timestamp

在您的例子中,Python有一个包
GoogleCloudFireStore
,我假设您正在云函数中使用。我还没有将Firestore与Python一起使用,但是从示例代码来看,这个Python包应该能够获取常规Python
datetime
对象,并将其转换,并向Firestore提供所需的数据:

data = {
    u'stringExample': u'Hello, World!',
    u'booleanExample': True,
    u'numberExample': 3.14159265,
    u'dateExample': datetime.datetime.now(),
    u'arrayExample': [5, True, u'hello'],
    u'nullExample': None,
    u'objectExample': {
        u'a': 5,
        u'b': True
    }
}

db.collection(u'data').document(u'one').set(data)

现在,在您的情况下,您可能会通过MQTT协议从IoT边缘设备接收时间戳,这听起来像是在UNIX时代(?)。我将开始执行
datetime.fromtimestamp
datetime.utcfromtimestamp
路线。你需要处理时区问题。然后,一旦有了Python日期时间,理论上可以通过文档提供它作为Firestore时间戳,pip包将负责其余的转换。

是的,我看到了。我希望在python中指定时间,就像在JavaScript api文档中一样。这解决了我的问题。在我的测试中,
datetime.now()
发送本地时间,但Firestore认为这是UTC时间,而
Firestore.SERVER\u TIMESTAMP
设置正确的时间戳,该时间戳也在Firestore控制台中正确转换/显示。谢谢datetime.now()是执行代码时的当前时间。我昨天回答了这个问题,如下所示。您可以看到,我使用datetime.fromisoformat()从边缘设备发送的时间戳字符串中获取datetime对象。谢谢你的帮助。好吧,我没看到你的答案。基本上,您已经明白了我的解释:您只需提供Python本机
datetime
对象,其余的由
GoogleCloudFireStore
包内部处理。这个问题听起来像是你收到了日期,是的,你是对的。别担心。起初我以为你必须提供一秒钟的时间。在我最初的问题中,我在注释中有这样一个链接:它有一个构造函数,它从epoch开始用秒作为构造函数参数。我感谢你的帮助。我是firebase的新手,所以这让人困惑。另外,如果您注意到该网页包含除python之外的所有语言引用。为什么呢?