Convert UTC bigint to timestamp.

huangapple go评论71阅读模式
英文:

Convert UTC bigint to timestamp

问题

我有一个名为'event_timestamp'的字段,据说是以UTC时间表示的,但目前它是一个bigint字段而不是时间戳。有关该字段的文档如下:

当事件在客户端上记录时的时间(以微秒为单位,UTC时间)。

这是该字段的示例值:1681149457712377

我知道从对应的字段event_date,时间戳应该对应于日期2023-04-10

我需要计算事件之间的时间差,因此需要将bigint字段event_timestamp转换为实际的时间戳。我阅读了这篇SO帖子,并尝试了以下操作:

select to_date(1681149457712377::text, 'YYYYMMDD')
ERROR: value for "DD" in source string is out of range
  Detail: Value must be in the range -2147483648 to 2147483647.

然后尝试了:

select trunc(TIMESTAMP 'epoch' + 1681149457712377 / 1000 * INTERVAL '1 second')
55243-07-04 # 荒谬的日期

但我真正想要的是计算两个这些bigint时间戳之间的秒数差,例如,从我的事件表中的两个示例值1681149457712377和1681101230813726之间的时间差。我该如何做?

英文:

I have a field 'event_timestamp' that is supposedly in UTC time but is currently a bigint field not a timestamp. Documentation for this field:

> The time (in microseconds, UTC) when the event was logged on the client.

Here is an example value from this field: 1681149457712377

I know from a corresponding field event_date that the timestamp should correspond to date 2023-04-10

I need to calculate the time delta between events and so need to convert the bigint field event_timestamp to an actual timestamp. I read this SO post and tried the following:

select to_date(1681149457712377::text, 'YYYYMMDD')
ERROR: value for "DD" in source string is out of range
  Detail: Value must be in the range -2147483648 to 2147483647.

Then tried:

select trunc(TIMESTAMP 'epoch' + 1681149457712377 / 1000 * INTERVAL '1 second')
55243-07-04 # nonsensical date

But what I really want is to calculate the time difference in seconds between two of these bigint timestamps e.g. whats the time difference in seconds between 1681149457712377 and 1681101230813726, two example values from my event table. How can I do this?

答案1

得分: 1

首先,你需要从一个大整数中减去另一个大整数。这将给你它们的微秒差异。

一微秒等于1e-6秒。所以,然后你需要将这个差异乘以1e-6来将其转换为秒。

英文:

First, you need to subtract one bigint from the other. This will give you their difference in microseconds.

One microsecond is 1e-6 seconds. So, then you need to multiply that difference by 1e-6 to convert it to seconds.

答案2

得分: 0

注意这个时间戳是以微秒为单位(而不是毫秒),所以你需要除以 1,000,000 而不是 1,000。

我不知道你使用哪种 SQL,但其中许多都有 to_timestamp 函数可以为你执行这个转换。

例如,在 SnowflakeSQL 中,你可以使用:

select to_timestamp(1681149457712377, 6)

这里的 6 表示 "精度",在这里它表示微秒(10^-6)。

有些 SQL 可能没有内置此功能,或者只允许秒。那么你需要手动除以时间戳。

英文:

Take a note that this timestamp is in microseconds (not milliseconds), so you need to divide by 1,000,000 not by 1,000.

I don't know what SQL you use, but many of them have function to_timestamp that can do this conversion for you.

For example in snowflakeSQL you can use

select to_timestamp(1681149457712377, 6)

where 6 means "precision" - here it means microseconds (10^-6)

Some sqls may not have this function built in, or may only allow seconds. Then you need to manually divide the timestamp.

huangapple
  • 本文由 发表于 2023年4月13日 23:11:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定