获取protobuf当前时间戳

Mei*_*imi 1 python timestamp protocol-buffers

设法创建一个脚本并运行它以通过 Google API(BigQuery 数据传输服务)安排作业,该脚本使用protobuf 消息时间戳类型来设置开始和结束日期。我无法将其更改为当前时间戳。

检查以下资源:

我尝试设置如下属性,但这会引发“请求包含无效参数”的错误。

now = time.time()
seconds = int(now)
start_time = Timestamp(seconds=seconds, nanos=0)
end_time = Timestamp(seconds=seconds, nanos=0)
Run Code Online (Sandbox Code Playgroud)

请参阅下面的工作示例:

#!/usr/bin/env python
from google.cloud import bigquery_datatransfer
from google.protobuf.timestamp_pb2 import Timestamp

client = bigquery_datatransfer.DataTransferServiceClient()

start_time = Timestamp()
end_time = Timestamp()

client.schedule_transfer_runs(client.get_transfer_config("projects/{project_id}/locations/europe/transferConfigs/{transfer_id}").name,
                              start_time=start_time,
                              end_time=end_time)
Run Code Online (Sandbox Code Playgroud)

这可行,但会向 API 发送开始时间和结束时间时间戳的请求1970-01-01T00:00:00Z- 我希望能够将其更改为当前时间戳。

Nic*_*man 5

Timestamp正如文档中所述,有几种方法可以做到这一点。如果您只想使用当前时间构建时间戳,则可以简单地使用timestamp_message.GetCurrentTime(). 如果您想用您的值填充时间戳,seconds那么您可以简单地使用timestamp_message.FromSeconds(seconds).

作为一个更完整的例子

start_time = Timestamp()
start_time.GetCurrentTime()  # Stores the current time in start_time.
end_time = Timestamp()
seconds = 12345
end_time.FromSeconds(seconds)  # Stores the number of seconds in end_time.
Run Code Online (Sandbox Code Playgroud)

对于您的具体实例,您应该能够执行以下操作

#!/usr/bin/env python
from google.cloud import bigquery_datatransfer
from google.protobuf.timestamp_pb2 import Timestamp

client = bigquery_datatransfer.DataTransferServiceClient()

start_time = Timestamp()
start_time.GetCurrentTime()
end_time = Timestamp()
end_time.GetCurrentTime()

client.schedule_transfer_runs(client.get_transfer_config("projects/{project_id}/locations/europe/transferConfigs/{transfer_id}").name,
                              start_time=start_time,
                              end_time=end_time)
Run Code Online (Sandbox Code Playgroud)