首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取原型当前时间戳

获取原型当前时间戳
EN

Stack Overflow用户
提问于 2019-03-25 16:08:34
回答 1查看 5.6K关注 0票数 1

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

检查以下资源:

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

代码语言:javascript
复制
now = time.time()
seconds = int(now)
start_time = Timestamp(seconds=seconds, nanos=0)
end_time = Timestamp(seconds=seconds, nanos=0)

见下面的工作示例:

代码语言:javascript
复制
#!/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)

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

EN

回答 1

Stack Overflow用户

发布于 2019-03-25 18:44:01

正如在 docs中所指出的,有几种方法可以做到这一点。如果您只想用当前时间构建时间戳,只需使用timestamp_message.GetCurrentTime()即可。如果希望使用seconds值填充时间戳,则只需使用timestamp_message.FromSeconds(seconds)即可。

作为一个更完整的例子

代码语言:javascript
复制
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.

对于您的具体实例,您应该能够做到

代码语言:javascript
复制
#!/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)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55341989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档