简体   繁体   English

如何显示 python time.time() 在 bigquery 中存储为浮点数作为日期时间?

[英]how to display python time.time() stored as a float in bigquery as a datetime?

I am a bigquery newbie我是一个大查询新手

we have a table that stores the output from time.time() as float.我们有一个表,将 time.time() 中的 output 存储为浮点数。 For example例如

1671057937.23425
1670884516.891432

How can I select these values such that they are formated/displayed as date and time stamp.我怎样才能 select 这些值,以便将它们格式化/显示为日期和时间戳。

I have tried casting to int64, using various date/time functions like DATE_FROM_UNIX_DATE and TIMESTAMP_MICROS我尝试使用各种日期/时间函数(如 DATE_FROM_UNIX_DATE 和 TIMESTAMP_MICROS)转换为 int64

Any suggestions would be greatly appreciated任何建议将不胜感激

In Python, the time() function returns the number of seconds passed since epoch - so you can use below simple approach在 Python 中,time() function 返回自纪元以来经过的秒数 - 因此您可以使用下面的简单方法

select timestamp_micros(cast(1670884516.891432 * 1000000 as int64))

to convert back to readable date time with python:使用 python 转换回可读的日期时间:

import datetime

stamp = "1670884516.891432"
readable = datetime.datetime.fromtimestamp(float(stamp))
print(readable)

result:结果:

2022-12-13 01:35:16.891432

Please, formulate the question more clearly.请更清楚地提出问题。

What do you mean with your words:你的话是什么意思:

How can I select these values...我怎样才能 select 这些值...

Do you mean to select with SQL query?你是说select用SQL查询吗? Then然后

SELECT convert(varchar, CURRENT_TIMESTAMP,21) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM